1
1
package graphql.kickstart.servlet
2
2
3
3
import graphql.AssertException
4
- import graphql.Scalars
5
4
import graphql.annotations.annotationTypes.GraphQLField
6
5
import graphql.annotations.annotationTypes.GraphQLName
7
6
import graphql.annotations.processor.GraphQLAnnotations
8
- import graphql.kickstart.servlet.osgi.GraphQLCodeRegistryProvider
9
- import graphql.kickstart.servlet.osgi.GraphQLMutationProvider
10
- import graphql.kickstart.servlet.osgi.GraphQLQueryProvider
11
- import graphql.kickstart.servlet.osgi.GraphQLSubscriptionProvider
12
- import graphql.schema.GraphQLCodeRegistry
13
- import graphql.schema.GraphQLFieldDefinition
14
- import graphql.schema.GraphQLInterfaceType
7
+ import graphql.kickstart.execution.GraphQLRequest
8
+ import graphql.kickstart.execution.config.ExecutionStrategyProvider
9
+ import graphql.kickstart.execution.context.DefaultGraphQLContext
10
+ import graphql.kickstart.execution.context.GraphQLContext
11
+ import graphql.kickstart.servlet.context.GraphQLServletContextBuilder
12
+ import graphql.kickstart.servlet.core.GraphQLServletListener
13
+ import graphql.kickstart.servlet.core.GraphQLServletRootObjectBuilder
14
+ import graphql.kickstart.servlet.osgi.*
15
+ import graphql.schema.*
16
+ import org.dataloader.DataLoaderRegistry
15
17
import spock.lang.Specification
16
18
17
- import java.lang.annotation.Annotation
18
-
19
19
import static graphql.Scalars.GraphQLInt
20
20
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition
21
21
@@ -82,12 +82,23 @@ class OsgiGraphQLHttpServletSpec extends Specification {
82
82
servlet. bindMutationProvider(mutationProvider)
83
83
then :
84
84
servlet. getConfiguration(). getInvocationInputFactory(). getSchemaProvider(). getSchema(). getMutationType(). getFieldDefinition(" int" ). getType() == GraphQLInt
85
- servlet. getConfiguration(). getInvocationInputFactory(). getSchemaProvider(). getReadOnlySchema(null ). getMutationType() == null
85
+ servlet. getConfiguration(). getInvocationInputFactory(). getSchemaProvider(). getReadOnlySchema(). getMutationType() == null
86
86
87
87
when :
88
88
servlet. unbindMutationProvider(mutationProvider)
89
89
then :
90
90
servlet. getConfiguration(). getInvocationInputFactory(). getSchemaProvider(). getSchema(). getMutationType() == null
91
+
92
+ when :
93
+ servlet. bindProvider(mutationProvider)
94
+ then :
95
+ servlet. getConfiguration(). getInvocationInputFactory(). getSchemaProvider(). getSchema(). getMutationType(). getFieldDefinition(" int" ). getType() == GraphQLInt
96
+ servlet. getConfiguration(). getInvocationInputFactory(). getSchemaProvider(). getReadOnlySchema(). getMutationType() == null
97
+
98
+ when :
99
+ servlet. unbindProvider(mutationProvider)
100
+ then :
101
+ servlet. getConfiguration(). getInvocationInputFactory(). getSchemaProvider(). getSchema(). getMutationType() == null
91
102
}
92
103
93
104
static class TestSubscriptionProvider implements GraphQLSubscriptionProvider {
@@ -124,6 +135,17 @@ class OsgiGraphQLHttpServletSpec extends Specification {
124
135
servlet. unbindSubscriptionProvider(subscriptionProvider)
125
136
then :
126
137
servlet. getConfiguration(). getInvocationInputFactory(). getSchemaProvider(). getSchema(). getSubscriptionType() == null
138
+
139
+ when :
140
+ servlet. bindProvider(subscriptionProvider)
141
+ then :
142
+ def subscription2 = servlet. getConfiguration(). getInvocationInputFactory(). getSchemaProvider(). getReadOnlySchema(). getSubscriptionType(). getFieldDefinition(" subscription" )
143
+ subscription2. getType(). getName() == " subscription"
144
+
145
+ when :
146
+ servlet. unbindProvider(subscriptionProvider)
147
+ then :
148
+ servlet. getConfiguration(). getInvocationInputFactory(). getSchemaProvider(). getSchema(). getSubscriptionType() == null
127
149
}
128
150
129
151
static class TestCodeRegistryProvider implements GraphQLCodeRegistryProvider {
@@ -149,6 +171,18 @@ class OsgiGraphQLHttpServletSpec extends Specification {
149
171
servlet. getConfiguration(). getInvocationInputFactory(). getSchemaProvider(). getSchema(). getCodeRegistry(). getTypeResolver(GraphQLInterfaceType . newInterface(). name(" Type" ). build())
150
172
then :
151
173
thrown AssertException
174
+
175
+ when :
176
+ servlet. bindProvider(codeRegistryProvider)
177
+ servlet. getConfiguration(). getInvocationInputFactory(). getSchemaProvider(). getSchema(). getCodeRegistry(). getTypeResolver(GraphQLInterfaceType . newInterface(). name(" Type" ). build())
178
+ then :
179
+ notThrown AssertException
180
+
181
+ when :
182
+ servlet. unbindProvider(codeRegistryProvider)
183
+ servlet. getConfiguration(). getInvocationInputFactory(). getSchemaProvider(). getSchema(). getCodeRegistry(). getTypeResolver(GraphQLInterfaceType . newInterface(). name(" Type" ). build())
184
+ then :
185
+ thrown AssertException
152
186
}
153
187
154
188
def " schema update delay throws no exception" () {
@@ -181,9 +215,130 @@ class OsgiGraphQLHttpServletSpec extends Specification {
181
215
query. getType(). name == " query"
182
216
183
217
when :
184
- query = servlet. getConfiguration(). getInvocationInputFactory(). getSchemaProvider(). getReadOnlySchema(null ). getQueryType(). getFieldDefinition(" query" )
218
+ query = servlet. getConfiguration(). getInvocationInputFactory(). getSchemaProvider(). getReadOnlySchema(). getQueryType(). getFieldDefinition(" query" )
185
219
186
220
then :
187
221
query. getType(). name == " query"
222
+
223
+ when :
224
+ servlet. unbindProvider(queryProvider)
225
+ then :
226
+ null != servlet. getConfiguration(). getInvocationInputFactory(). getSchemaProvider(). getSchema(). getQueryType(). getFieldDefinition(" _empty" )
227
+ }
228
+
229
+ def " type provider adds types" () {
230
+ setup :
231
+ def servlet = new OsgiGraphQLHttpServlet ()
232
+ def typesProvider = Mock (GraphQLTypesProvider )
233
+ def coercing = Mock (Coercing )
234
+ typesProvider. types >> [GraphQLScalarType . newScalar(). name(" Upload" ). coercing(coercing). build()]
235
+
236
+ when :
237
+ servlet. bindTypesProvider(typesProvider)
238
+
239
+ then :
240
+ def type = servlet. configuration. invocationInputFactory. schemaProvider. schema. getType(" Upload" )
241
+ type != null
242
+ type. name == " Upload"
243
+ type instanceof GraphQLScalarType
244
+ def scalarType = (GraphQLScalarType ) type
245
+ scalarType. coercing == coercing
246
+
247
+ when :
248
+ servlet. unbindTypesProvider(typesProvider)
249
+
250
+ then :
251
+ null == servlet. configuration. invocationInputFactory. schemaProvider. schema. getType(" Upload" )
252
+
253
+ when :
254
+ servlet. bindProvider(typesProvider)
255
+ then :
256
+ servlet. configuration. invocationInputFactory. schemaProvider. schema. getType(" Upload" ). name == " Upload"
257
+
258
+ when :
259
+ servlet. unbindProvider(typesProvider)
260
+ then :
261
+ null == servlet. configuration. invocationInputFactory. schemaProvider. schema. getType(" Upload" )
262
+ }
263
+
264
+ def " servlet listener is bound and unbound" () {
265
+ setup :
266
+ def servlet = new OsgiGraphQLHttpServlet ()
267
+ def listener = Mock (GraphQLServletListener )
268
+
269
+ when :
270
+ servlet. bindServletListener(listener)
271
+ then :
272
+ servlet. configuration. listeners. contains(listener)
273
+
274
+ when :
275
+ servlet. unbindServletListener(listener)
276
+ then :
277
+ ! servlet. configuration. listeners. contains(listener)
278
+ }
279
+
280
+ def " context builder is bound and unbound" () {
281
+ setup :
282
+ def servlet = new OsgiGraphQLHttpServlet ()
283
+ def context = Mock (GraphQLContext )
284
+ context. getDataLoaderRegistry() >> new DataLoaderRegistry ()
285
+ context. getSubject() >> Optional . empty()
286
+ def contextBuilder = Mock (GraphQLServletContextBuilder )
287
+ contextBuilder. build() >> context
288
+ def request = GraphQLRequest . createIntrospectionRequest()
289
+
290
+ when :
291
+ servlet. setContextBuilder(contextBuilder)
292
+ then :
293
+ def invocationInput = servlet. configuration. invocationInputFactory. create(request)
294
+ invocationInput. executionInput. context == context
295
+
296
+ when :
297
+ servlet. unsetContextBuilder(contextBuilder)
298
+ then :
299
+ servlet. configuration. invocationInputFactory. create(request). executionInput. context instanceof DefaultGraphQLContext
300
+ }
301
+
302
+ def " root object builder is bound and unbound" () {
303
+ setup :
304
+ def servlet = new OsgiGraphQLHttpServlet ()
305
+ def rootObject = Mock (Object )
306
+ def rootObjectBuilder = Mock (GraphQLServletRootObjectBuilder )
307
+ rootObjectBuilder. build() >> rootObject
308
+ def request = GraphQLRequest . createIntrospectionRequest()
309
+
310
+ when :
311
+ servlet. setRootObjectBuilder(rootObjectBuilder)
312
+ then :
313
+ def invocationInput = servlet. configuration. invocationInputFactory. create(request)
314
+ invocationInput. executionInput. root == rootObject
315
+
316
+ when :
317
+ servlet. unsetRootObjectBuilder(rootObjectBuilder)
318
+ then :
319
+ servlet. configuration. invocationInputFactory. create(request). executionInput. root != rootObject
320
+ }
321
+
322
+ def " execution strategy is bound and unbound" () {
323
+ setup :
324
+ def servlet = new OsgiGraphQLHttpServlet ()
325
+ def executionStrategy = Mock (ExecutionStrategyProvider )
326
+ def request = GraphQLRequest . createIntrospectionRequest()
327
+
328
+ when :
329
+ servlet. setExecutionStrategyProvider(executionStrategy)
330
+ def invocationInput = servlet. configuration. invocationInputFactory. create(request)
331
+ servlet. configuration. graphQLInvoker. query(invocationInput)
332
+
333
+ then :
334
+ 1 * executionStrategy. getQueryExecutionStrategy()
335
+
336
+ when :
337
+ servlet. unsetExecutionStrategyProvider(executionStrategy)
338
+ def invocationInput2 = servlet. configuration. invocationInputFactory. create(request)
339
+ servlet. configuration. graphQLInvoker. query(invocationInput2)
340
+
341
+ then :
342
+ 0 * executionStrategy. getQueryExecutionStrategy()
188
343
}
189
344
}
0 commit comments