Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for @composeDirective #253

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def merge_directives(node, type)

def directive_name(directive)
if schema.federation_2? && !Schema::IMPORTED_DIRECTIVES.include?(directive[:name])
"#{schema.link_namespace}__#{directive[:name]}"
"#{schema.default_link_namespace}__#{directive[:name]}"
else
directive[:name]
end
Expand Down
55 changes: 45 additions & 10 deletions lib/apollo-federation/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

module ApolloFederation
module Schema
IMPORTED_DIRECTIVES = ['inaccessible', 'tag'].freeze
IMPORTED_DIRECTIVES = ['inaccessible', 'tag', 'composeDirective'].freeze

def self.included(klass)
klass.extend(CommonMethods)
Expand All @@ -16,9 +16,11 @@ def self.included(klass)
module CommonMethods
DEFAULT_LINK_NAMESPACE = 'federation'

def federation(version: '1.0', link: {})
def federation(version: '1.0', default_link_namespace: nil, links: [], compose_directives: [])
@federation_version = version
@link = { as: DEFAULT_LINK_NAMESPACE }.merge(link)
@default_link_namespace = default_link_namespace
@links = links
@compose_directives = compose_directives
end

def federation_version
Expand All @@ -37,8 +39,8 @@ def federation_sdl(context: nil)
output
end

def link_namespace
@link ? @link[:as] : find_inherited_value(:link_namespace)
def default_link_namespace
@default_link_namespace || find_inherited_value(:default_link_namespace, DEFAULT_LINK_NAMESPACE)
end

def query(new_query_object = nil)
Expand All @@ -62,14 +64,47 @@ def original_query
@orig_query_object || find_inherited_value(:original_query)
end

def compose_directives
@compose_directives || find_inherited_value(:compose_directives, [])
end

def links
@links || find_inherited_value(:links, [])
end

def all_links
imported_directives = IMPORTED_DIRECTIVES
default_link = {
url: 'https://specs.apollo.dev/federation/v2.3',
import: imported_directives,
}
default_link[:as] = default_link_namespace if default_link_namespace != DEFAULT_LINK_NAMESPACE
[default_link, *links]
end

def federation_2_prefix
federation_namespace = ", as: \"#{link_namespace}\"" if link_namespace != DEFAULT_LINK_NAMESPACE
schema = ['extend schema']

all_links.each do |link|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This printing logic is fairly complex. Can we make it easier to read?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to do make it easier to read but not a ruby expert, please tell me if there's a better way :)

link_str = " @link("
link_str += "url: \"#{link[:url]}\""
link_str += ", as: \"#{link[:as]}\"" if link[:as]
if link[:import]
imported_directives = link[:import].map { |d| "\"@#{d}\"" }.join(', ')
link_str += ", import: [#{imported_directives}]"
end
link_str += ')'
schema << link_str
end

compose_directives.each do |directive|
schema << " @composeDirective(name: \"@#{directive}\")"
end

<<~SCHEMA
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.3"#{federation_namespace}, import: [#{(IMPORTED_DIRECTIVES.map { |directive| "\"@#{directive}\"" }).join(', ')}])
schema << ''
schema << ''

SCHEMA
schema.join("\n")
end

def schema_entities
Expand Down
Loading