Skip to content

Commit 0e717ea

Browse files
committed
Respect Federation version in @link directive
1 parent fc541ee commit 0e717ea

File tree

3 files changed

+85
-32
lines changed

3 files changed

+85
-32
lines changed

lib/apollo-federation/schema.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ def self.included(klass)
1212
end
1313

1414
module CommonMethods
15-
FEDERATION_2_PREFIX = <<~SCHEMA
16-
extend schema
17-
@link(url: "https://specs.apollo.dev/federation/v2.0")
18-
19-
SCHEMA
20-
2115
def federation(version: '1.0', link: {})
2216
@federation_version = version
2317
@link = { as: 'federation' }.merge(link)
@@ -35,7 +29,7 @@ def federation_sdl(context: nil)
3529
document_from_schema = FederatedDocumentFromSchemaDefinition.new(self, context: context)
3630

3731
output = GraphQL::Language::Printer.new.print(document_from_schema.document)
38-
output.prepend(FEDERATION_2_PREFIX) if federation_2?
32+
output.prepend(federation_preamble) if federation_2?
3933
output
4034
end
4135

@@ -60,10 +54,10 @@ def query(new_query_object = nil)
6054

6155
private
6256

63-
def federation_2_prefix
57+
def federation_preamble
6458
<<~SCHEMA
6559
extend schema
66-
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "#{link_namespace}")
60+
@link(url: "https://specs.apollo.dev/federation/v#{federation_version}", as: "#{link_namespace}")
6761
6862
SCHEMA
6963
end

spec/apollo-federation/schema_spec.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,63 @@
7070
expect(schema.federation_2?).to be(true)
7171
end
7272
end
73+
74+
describe '.federation_sdl' do
75+
let(:base_object) do
76+
base_field = Class.new(GraphQL::Schema::Field) do
77+
include ApolloFederation::Field
78+
end
79+
80+
Class.new(GraphQL::Schema::Object) do
81+
include ApolloFederation::Object
82+
field_class base_field
83+
end
84+
end
85+
86+
let(:query_type) {
87+
Class.new(base_object) do
88+
graphql_name 'Query'
89+
90+
field :test, String, null: false
91+
end
92+
}
93+
94+
it 'returns a federation 1 schema by default' do
95+
# can't use a let-defined identifier inside the below class definition?
96+
qt = query_type
97+
schema = Class.new(GraphQL::Schema) do
98+
include ApolloFederation::Schema
99+
query qt
100+
end
101+
102+
expect(schema.federation_sdl).to match_sdl(
103+
<<~GRAPHQL
104+
type Query {
105+
test: String!
106+
}
107+
GRAPHQL
108+
)
109+
end
110+
111+
it 'returns a schema directive with the given federation version if specified' do
112+
qt = query_type
113+
schema = Class.new(GraphQL::Schema) do
114+
include ApolloFederation::Schema
115+
query qt
116+
federation version: '2.1'
117+
end
118+
119+
expect(schema.federation_sdl).to match_sdl(
120+
<<~GRAPHQL
121+
extend schema
122+
@link(url: "https://specs.apollo.dev/federation/v2.1", as: "federation")
123+
124+
type Query {
125+
test: String!
126+
}
127+
GRAPHQL
128+
)
129+
130+
end
131+
end
73132
end

spec/apollo-federation/service_field_v2_spec.rb

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def execute_sdl(schema)
109109
expect(execute_sdl(schema)).to match_sdl(
110110
<<~GRAPHQL,
111111
extend schema
112-
@link(url: "https://specs.apollo.dev/federation/v2.0")
112+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "federation")
113113
114114
type Product {
115115
upc: String!
@@ -144,7 +144,7 @@ def execute_sdl(schema)
144144
expect(execute_sdl(schema)).to match_sdl(
145145
<<~GRAPHQL,
146146
extend schema
147-
@link(url: "https://specs.apollo.dev/federation/v2.0")
147+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "federation")
148148
149149
type Product @federation__extends {
150150
upc: String!
@@ -180,7 +180,7 @@ def execute_sdl(schema)
180180
expect(execute_sdl(schema)).to match_sdl(
181181
<<~GRAPHQL,
182182
extend schema
183-
@link(url: "https://specs.apollo.dev/federation/v2.0")
183+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "federation")
184184
185185
type Position @federation__shareable {
186186
x: Int!
@@ -217,7 +217,7 @@ def execute_sdl(schema)
217217
expect(execute_sdl(schema)).to match_sdl(
218218
<<~GRAPHQL,
219219
extend schema
220-
@link(url: "https://specs.apollo.dev/federation/v2.0")
220+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "federation")
221221
222222
type Position @federation__inaccessible {
223223
x: Int!
@@ -254,7 +254,7 @@ def execute_sdl(schema)
254254
expect(execute_sdl(schema)).to match_sdl(
255255
<<~GRAPHQL,
256256
extend schema
257-
@link(url: "https://specs.apollo.dev/federation/v2.0")
257+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "fed2")
258258
259259
type Product @fed2__extends {
260260
upc: String!
@@ -290,7 +290,7 @@ def execute_sdl(schema)
290290
expect(execute_sdl(schema)).to match_sdl(
291291
<<~GRAPHQL,
292292
extend schema
293-
@link(url: "https://specs.apollo.dev/federation/v2.0")
293+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "fed2")
294294
295295
type Position @fed2__shareable {
296296
x: Int!
@@ -327,7 +327,7 @@ def execute_sdl(schema)
327327
expect(execute_sdl(schema)).to match_sdl(
328328
<<~GRAPHQL,
329329
extend schema
330-
@link(url: "https://specs.apollo.dev/federation/v2.0")
330+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "fed2")
331331
332332
type Position @fed2__inaccessible {
333333
x: Int!
@@ -363,7 +363,7 @@ def execute_sdl(schema)
363363
expect(execute_sdl(schema)).to match_sdl(
364364
<<~GRAPHQL,
365365
extend schema
366-
@link(url: "https://specs.apollo.dev/federation/v2.0")
366+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "fed2")
367367
368368
type Product @fed2__key(fields: "upc") {
369369
upc: String!
@@ -394,7 +394,7 @@ def execute_sdl(schema)
394394
expect(execute_sdl(schema)).to match_sdl(
395395
<<~GRAPHQL,
396396
extend schema
397-
@link(url: "https://specs.apollo.dev/federation/v2.0")
397+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "fed2")
398398
399399
type Product @fed2__extends @fed2__key(fields: "upc") {
400400
price: Int
@@ -444,7 +444,7 @@ def execute_sdl(schema)
444444
expect(execute_sdl(schema)).to match_sdl(
445445
<<~GRAPHQL,
446446
extend schema
447-
@link(url: "https://specs.apollo.dev/federation/v2.0")
447+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "federation")
448448
449449
type Book implements Product {
450450
upc: String!
@@ -507,7 +507,7 @@ def execute_sdl(schema)
507507
expect(execute_sdl(schema)).to match_sdl(
508508
<<~GRAPHQL,
509509
extend schema
510-
@link(url: "https://specs.apollo.dev/federation/v2.0")
510+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "federation")
511511
512512
type Book implements Product @federation__extends @federation__key(fields: "upc") {
513513
upc: String! @federation__external
@@ -547,7 +547,7 @@ def execute_sdl(schema)
547547
expect(execute_sdl(schema)).to match_sdl(
548548
<<~GRAPHQL,
549549
extend schema
550-
@link(url: "https://specs.apollo.dev/federation/v2.0")
550+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "federation")
551551
552552
type Product @federation__key(fields: "upc") {
553553
upc: String!
@@ -578,7 +578,7 @@ def execute_sdl(schema)
578578
expect(execute_sdl(schema)).to match_sdl(
579579
<<~GRAPHQL,
580580
extend schema
581-
@link(url: "https://specs.apollo.dev/federation/v2.0")
581+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "federation")
582582
583583
type Product @federation__key(fields: "upc") {
584584
upc: String!
@@ -606,7 +606,7 @@ def execute_sdl(schema)
606606
expect(execute_sdl(schema)).to match_sdl(
607607
<<~GRAPHQL,
608608
extend schema
609-
@link(url: "https://specs.apollo.dev/federation/v2.0")
609+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "federation")
610610
611611
type Product @federation__key(fields: "upc") @federation__key(fields: "name") {
612612
name: String
@@ -634,7 +634,7 @@ def execute_sdl(schema)
634634
expect(execute_sdl(schema)).to match_sdl(
635635
<<~GRAPHQL,
636636
extend schema
637-
@link(url: "https://specs.apollo.dev/federation/v2.0")
637+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "federation")
638638
639639
type Product @federation__extends @federation__key(fields: "upc") {
640640
price: Int
@@ -666,7 +666,7 @@ def execute_sdl(schema)
666666
expect(execute_sdl(schema)).to match_sdl(
667667
<<~GRAPHQL,
668668
extend schema
669-
@link(url: "https://specs.apollo.dev/federation/v2.0")
669+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "federation")
670670
671671
type Position {
672672
x: Int! @federation__shareable
@@ -702,7 +702,7 @@ def execute_sdl(schema)
702702
expect(execute_sdl(schema)).to match_sdl(
703703
<<~GRAPHQL,
704704
extend schema
705-
@link(url: "https://specs.apollo.dev/federation/v2.0")
705+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "federation")
706706
707707
type Position {
708708
x: Int! @federation__inaccessible
@@ -734,7 +734,7 @@ def execute_sdl(schema)
734734
expect(execute_sdl(schema)).to match_sdl(
735735
<<~GRAPHQL,
736736
extend schema
737-
@link(url: "https://specs.apollo.dev/federation/v2.0")
737+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "federation")
738738
739739
type Product @federation__extends @federation__key(fields: "id") {
740740
id: ID!
@@ -770,7 +770,7 @@ def execute_sdl(schema)
770770
expect(execute_sdl(schema)).to match_sdl(
771771
<<~GRAPHQL,
772772
extend schema
773-
@link(url: "https://specs.apollo.dev/federation/v2.0")
773+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "federation")
774774
775775
type Product @federation__extends @federation__key(fields: "upc") {
776776
price: Int
@@ -805,7 +805,7 @@ def execute_sdl(schema)
805805
expect(execute_sdl(schema)).to match_sdl(
806806
<<~GRAPHQL,
807807
extend schema
808-
@link(url: "https://specs.apollo.dev/federation/v2.0")
808+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "federation")
809809
810810
type Product @federation__extends @federation__key(fields: "upc") {
811811
price: Int @federation__external
@@ -834,7 +834,7 @@ def execute_sdl(schema)
834834
expect(execute_sdl(schema)).to match_sdl(
835835
<<~GRAPHQL,
836836
extend schema
837-
@link(url: "https://specs.apollo.dev/federation/v2.0")
837+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "federation")
838838
839839
type Product @federation__key(fields: "productId") {
840840
productId: String!
@@ -862,7 +862,7 @@ def execute_sdl(schema)
862862
expect(execute_sdl(schema)).to match_sdl(
863863
<<~GRAPHQL,
864864
extend schema
865-
@link(url: "https://specs.apollo.dev/federation/v2.0")
865+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "federation")
866866
867867
type Product @federation__extends @federation__key(fields: "product_id") {
868868
options: [String!]! @federation__requires(fields: "my_id")
@@ -895,7 +895,7 @@ def self.visible?(context)
895895
expect(execute_sdl(schema)).to match_sdl(
896896
<<~GRAPHQL,
897897
extend schema
898-
@link(url: "https://specs.apollo.dev/federation/v2.0")
898+
@link(url: "https://specs.apollo.dev/federation/v2.0", as: "federation")
899899
900900
type Product @federation__extends @federation__key(fields: "upc") {
901901
upc: String! @federation__external

0 commit comments

Comments
 (0)