@@ -23,6 +23,7 @@ import com.expediagroup.graphql.generator.federation.data.queries.simple.SimpleQ
23
23
import com.expediagroup.graphql.generator.federation.directives.KEY_DIRECTIVE_NAME
24
24
import com.expediagroup.graphql.generator.federation.types.ENTITY_UNION_NAME
25
25
import graphql.schema.GraphQLUnionType
26
+ import org.junit.jupiter.api.Assertions
26
27
import org.junit.jupiter.api.Assertions.assertEquals
27
28
import org.junit.jupiter.api.Test
28
29
import kotlin.test.assertNotNull
@@ -33,7 +34,7 @@ class FederatedSchemaGeneratorTest {
33
34
fun `verify can generate federated schema` () {
34
35
val expectedSchema =
35
36
"""
36
- schema {
37
+ schema @link(import : ["@external", "@key", "@provides", "@requires", "FieldSet"], url : "https://specs.apollo.dev/federation/v2.6") {
37
38
query: Query
38
39
}
39
40
@@ -45,11 +46,8 @@ class FederatedSchemaGeneratorTest {
45
46
reason: String = "No longer supported"
46
47
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
47
48
48
- "Marks target object as extending part of the federated schema"
49
- directive @extends on OBJECT | INTERFACE
50
-
51
49
"Marks target field as external meaning it will be resolved by federated schema"
52
- directive @external on FIELD_DEFINITION
50
+ directive @external on OBJECT | FIELD_DEFINITION
53
51
54
52
"Directs the executor to include this field or fragment only when the `if` argument is true"
55
53
directive @include(
@@ -58,16 +56,19 @@ class FederatedSchemaGeneratorTest {
58
56
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
59
57
60
58
"Space separated list of primary keys needed to access federated object"
61
- directive @key(fields: _FieldSet!) repeatable on OBJECT | INTERFACE
59
+ directive @key(fields: FieldSet!, resolvable: Boolean = true) repeatable on OBJECT | INTERFACE
60
+
61
+ "Links definitions within the document to external schemas."
62
+ directive @link(as: String, import: [link__Import], url: String!) repeatable on SCHEMA
62
63
63
64
"Indicates an Input Object is a OneOf Input Object."
64
65
directive @oneOf on INPUT_OBJECT
65
66
66
67
"Specifies the base type field set that will be selectable by the gateway"
67
- directive @provides(fields: _FieldSet !) on FIELD_DEFINITION
68
+ directive @provides(fields: FieldSet !) on FIELD_DEFINITION
68
69
69
70
"Specifies required input field set from the base type for a resolver"
70
- directive @requires(fields: _FieldSet !) on FIELD_DEFINITION
71
+ directive @requires(fields: FieldSet !) on FIELD_DEFINITION
71
72
72
73
"Directs the executor to skip this field or fragment when the `if` argument is true."
73
74
directive @skip(
@@ -81,33 +82,33 @@ class FederatedSchemaGeneratorTest {
81
82
url: String!
82
83
) on SCALAR
83
84
84
- interface Product @extends @ key(fields : "id") @key(fields : "upc") {
85
- id: String! @external
85
+ interface Product @key(fields : "id", resolvable : true ) @key(fields : "upc", resolvable : true ) {
86
+ id: String!
86
87
reviews: [Review!]!
87
- upc: String! @external
88
+ upc: String!
88
89
}
89
90
90
91
union _Entity = Author | Book | User
91
92
92
- type Author @extends @ key(fields : "authorId") {
93
- authorId: Int! @external
94
- name: String! @external
93
+ type Author @key(fields : "authorId", resolvable : true ) {
94
+ authorId: Int!
95
+ name: String!
95
96
}
96
97
97
- type Book implements Product @extends @ key(fields : "id") @key(fields : "upc") {
98
+ type Book implements Product @key(fields : "id", resolvable : true ) @key(fields : "upc", resolvable : true ) {
98
99
author: User! @provides(fields : "name")
99
- id: String! @external
100
+ id: String!
100
101
reviews: [Review!]!
101
102
shippingCost: String! @requires(fields : "weight")
102
- upc: String! @external
103
+ upc: String!
103
104
weight: Float! @external
104
105
}
105
106
106
107
type CustomScalar {
107
108
value: String!
108
109
}
109
110
110
- type Query @extends {
111
+ type Query {
111
112
"Union of all types that use the @key directive, including both types native to the schema and extended types"
112
113
_entities(representations: [_Any!]!): [_Entity]!
113
114
_service: _Service!
@@ -120,30 +121,31 @@ class FederatedSchemaGeneratorTest {
120
121
id: String!
121
122
}
122
123
123
- type User @extends @ key(fields : "userId") {
124
- name: String! @external
125
- userId: Int! @external
124
+ type User @key(fields : "userId", resolvable : true ) {
125
+ name: String!
126
+ userId: Int!
126
127
}
127
128
128
129
type _Service {
129
130
sdl: String!
130
131
}
131
132
133
+ "Federation type representing set of fields"
134
+ scalar FieldSet
135
+
132
136
"Federation scalar type used to represent any external entities passed to _entities query."
133
137
scalar _Any
134
138
135
- "Federation type representing set of fields"
136
- scalar _FieldSet
139
+ scalar link__Import
137
140
""" .trimIndent()
138
141
139
142
val config = FederatedSchemaGeneratorConfig (
140
- supportedPackages = listOf (" com.expediagroup.graphql.generator.federation.data.queries.federated.v1 " ),
141
- hooks = FederatedSchemaGeneratorHooks (emptyList(), optInFederationV2 = false )
143
+ supportedPackages = listOf (" com.expediagroup.graphql.generator.federation.data.queries.federated" ),
144
+ hooks = FederatedSchemaGeneratorHooks (emptyList())
142
145
)
143
146
144
147
val schema = toFederatedSchema(config = config)
145
-
146
- assertEquals(expectedSchema, schema.print ().trim())
148
+ Assertions .assertEquals(expectedSchema, schema.print ().trim())
147
149
val productType = schema.getObjectType(" Book" )
148
150
assertNotNull(productType)
149
151
assertNotNull(productType.hasAppliedDirective(KEY_DIRECTIVE_NAME ))
@@ -157,7 +159,7 @@ class FederatedSchemaGeneratorTest {
157
159
fun `verify generator does not add federation queries for non-federated schemas` () {
158
160
val expectedSchema =
159
161
"""
160
- schema {
162
+ schema @link(url : "https://specs.apollo.dev/federation/v2.6") {
161
163
query: Query
162
164
}
163
165
@@ -167,30 +169,18 @@ class FederatedSchemaGeneratorTest {
167
169
reason: String = "No longer supported"
168
170
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
169
171
170
- "Marks target object as extending part of the federated schema"
171
- directive @extends on OBJECT | INTERFACE
172
-
173
- "Marks target field as external meaning it will be resolved by federated schema"
174
- directive @external on FIELD_DEFINITION
175
-
176
172
"Directs the executor to include this field or fragment only when the `if` argument is true"
177
173
directive @include(
178
174
"Included when true."
179
175
if: Boolean!
180
176
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
181
177
182
- "Space separated list of primary keys needed to access federated object "
183
- directive @key(fields: _FieldSet !) repeatable on OBJECT | INTERFACE
178
+ "Links definitions within the document to external schemas. "
179
+ directive @link(as: String, import: [link__Import], url: String !) repeatable on SCHEMA
184
180
185
181
"Indicates an Input Object is a OneOf Input Object."
186
182
directive @oneOf on INPUT_OBJECT
187
183
188
- "Specifies the base type field set that will be selectable by the gateway"
189
- directive @provides(fields: _FieldSet!) on FIELD_DEFINITION
190
-
191
- "Specifies required input field set from the base type for a resolver"
192
- directive @requires(fields: _FieldSet!) on FIELD_DEFINITION
193
-
194
184
"Directs the executor to skip this field or fragment when the `if` argument is true."
195
185
directive @skip(
196
186
"Skipped when true."
@@ -203,7 +193,7 @@ class FederatedSchemaGeneratorTest {
203
193
url: String!
204
194
) on SCALAR
205
195
206
- type Query @extends {
196
+ type Query {
207
197
_service: _Service!
208
198
hello(name: String!): String!
209
199
}
@@ -212,27 +202,56 @@ class FederatedSchemaGeneratorTest {
212
202
sdl: String!
213
203
}
214
204
215
- "Federation type representing set of fields"
216
- scalar _FieldSet
205
+ scalar link__Import
217
206
""" .trimIndent()
218
207
219
208
val config = FederatedSchemaGeneratorConfig (
220
209
supportedPackages = listOf (" com.expediagroup.graphql.generator.federation.data.queries.simple" ),
221
- hooks = FederatedSchemaGeneratorHooks (emptyList(), optInFederationV2 = false )
210
+ hooks = FederatedSchemaGeneratorHooks (emptyList())
222
211
)
223
212
224
213
val schema = toFederatedSchema(config, listOf (TopLevelObject (SimpleQuery ())))
225
214
assertEquals(expectedSchema, schema.print ().trim())
226
215
}
227
216
228
217
@Test
229
- fun `verify a nested federated schema still works` () {
218
+ fun `verify a schema with self nested query still works` () {
230
219
val expectedSchema =
231
220
"""
232
- schema {
221
+ schema @link(url : "https://specs.apollo.dev/federation/v2.6") {
233
222
query: Query
234
223
}
235
224
225
+ "Marks the field, argument, input field or enum value as deprecated"
226
+ directive @deprecated(
227
+ "The reason for the deprecation"
228
+ reason: String = "No longer supported"
229
+ ) on FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE | INPUT_FIELD_DEFINITION
230
+
231
+ "Directs the executor to include this field or fragment only when the `if` argument is true"
232
+ directive @include(
233
+ "Included when true."
234
+ if: Boolean!
235
+ ) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
236
+
237
+ "Links definitions within the document to external schemas."
238
+ directive @link(as: String, import: [link__Import], url: String!) repeatable on SCHEMA
239
+
240
+ "Indicates an Input Object is a OneOf Input Object."
241
+ directive @oneOf on INPUT_OBJECT
242
+
243
+ "Directs the executor to skip this field or fragment when the `if` argument is true."
244
+ directive @skip(
245
+ "Skipped when true."
246
+ if: Boolean!
247
+ ) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
248
+
249
+ "Exposes a URL that specifies the behaviour of this scalar."
250
+ directive @specifiedBy(
251
+ "The URL that specifies the behaviour of this scalar."
252
+ url: String!
253
+ ) on SCALAR
254
+
236
255
type Query {
237
256
_service: _Service!
238
257
getSimpleNestedObject: [SelfReferenceObject]!
@@ -248,16 +267,15 @@ class FederatedSchemaGeneratorTest {
248
267
sdl: String!
249
268
}
250
269
251
- "Federation type representing set of fields"
252
- scalar _FieldSet
270
+ scalar link__Import
253
271
""" .trimIndent()
254
272
255
273
val config = FederatedSchemaGeneratorConfig (
256
274
supportedPackages = listOf (" com.expediagroup.graphql.generator.federation.data.queries.simple" ),
257
- hooks = FederatedSchemaGeneratorHooks (emptyList(), optInFederationV2 = false )
275
+ hooks = FederatedSchemaGeneratorHooks (emptyList())
258
276
)
259
277
260
278
val schema = toFederatedSchema(config, listOf (TopLevelObject (NestedQuery ())))
261
- assertEquals(expectedSchema, schema.print (includeDirectives = false ).trim())
279
+ assertEquals(expectedSchema, schema.print (includeDirectives = true ).trim())
262
280
}
263
281
}
0 commit comments