-
Notifications
You must be signed in to change notification settings - Fork 174
/
SchemaClassScannerTest.kt
558 lines (467 loc) · 17.1 KB
/
SchemaClassScannerTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package graphql.kickstart.tools
import graphql.GraphQLContext
import graphql.execution.CoercedVariables
import graphql.language.Value
import graphql.schema.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.junit.Test
import java.util.*
import java.util.concurrent.CompletableFuture
@OptIn(ExperimentalCoroutinesApi::class)
class SchemaClassScannerTest {
@Test
fun `scanner handles futures and immediate return types`() {
SchemaParser.newParser()
.resolvers(FutureImmediateQuery())
.schemaString(
"""
type Query {
future: Int!
immediate: Int!
}
""")
.build()
}
private class FutureImmediateQuery : GraphQLQueryResolver {
fun future(): CompletableFuture<Int> =
CompletableFuture.completedFuture(1)
fun immediate(): Int = 1
}
@Test
fun `scanner handles primitive and boxed return types`() {
SchemaParser.newParser()
.resolvers(PrimitiveBoxedQuery())
.schemaString(
"""
type Query {
primitive: Int!
boxed: Int!
}
""")
.build()
}
private class PrimitiveBoxedQuery : GraphQLQueryResolver {
fun primitive(): Int = 1
fun boxed(): Int? = null
}
@Test
fun `scanner handles different scalars with same java class`() {
SchemaParser.newParser()
.resolvers(ScalarDuplicateQuery())
.schemaString(
"""
type Query {
string: String!
id: ID!
}
""")
.build()
}
private class ScalarDuplicateQuery : GraphQLQueryResolver {
fun string(): String = ""
fun id(): String = ""
}
@Test
fun `scanner handles interfaces referenced by objects that aren't explicitly used`() {
val schema = SchemaParser.newParser()
.resolvers(InterfaceMissingQuery())
.schemaString(
"""
interface Interface {
id: ID!
}
type Query implements Interface {
id: ID!
}
""")
.build()
.makeExecutableSchema()
val interfaceType = schema.additionalTypes.find { it is GraphQLInterfaceType }
assertNotNull(interfaceType)
}
private class InterfaceMissingQuery : GraphQLQueryResolver {
fun id(): String = ""
}
@Test
fun `scanner handles input types that reference other input types`() {
val schema = SchemaParser.newParser()
.resolvers(MultipleInputTypeQuery())
.schemaString(
"""
input FirstInput {
id: String!
second: SecondInput!
third: ThirdInput!
}
input SecondInput {
id: String!
}
input ThirdInput {
id: String!
}
type Query {
test(input: FirstInput): String!
}
""")
.build()
.makeExecutableSchema()
val inputTypeCount = schema.additionalTypes.count { it is GraphQLInputType }
assertEquals(inputTypeCount, 3)
}
private class MultipleInputTypeQuery : GraphQLQueryResolver {
fun test(input: FirstInput): String = ""
class FirstInput {
var id: String? = null
fun second(): SecondInput = SecondInput()
var third: ThirdInput? = null
}
class SecondInput {
var id: String? = null
}
class ThirdInput {
var id: String? = null
}
}
@Test
fun `scanner handles input types extensions`() {
val schema = SchemaParser.newParser()
.schemaString(
"""
type Query { test: Boolean }
type Mutation {
save(input: UserInput!): Boolean
}
input UserInput {
name: String
}
extend input UserInput {
password: String
}
""")
.resolvers(
object : GraphQLMutationResolver {
fun save(map: Map<*, *>): Boolean = true
},
object : GraphQLQueryResolver {
fun test(): Boolean = true
}
)
.build()
.makeExecutableSchema()
val inputTypeExtensionCount = schema.additionalTypes
.filterIsInstance<GraphQLInputObjectType>()
.flatMap { it.extensionDefinitions }
.count()
assertEquals(inputTypeExtensionCount, 1)
}
@Test
fun `scanner allows multiple return types for custom scalars`() {
val schema = SchemaParser.newParser()
.resolvers(ScalarsWithMultipleTypes())
.scalars(GraphQLScalarType.newScalar()
.name("UUID")
.description("Test scalars with duplicate types")
.coercing(object : Coercing<Any, Any> {
override fun serialize(dataFetcherResult: Any, context: GraphQLContext, locale: Locale): Any? = null
override fun parseValue(input: Any, context: GraphQLContext, locale: Locale): Any = input
override fun parseLiteral(input: Value<*>, variables: CoercedVariables, context: GraphQLContext, locale: Locale): Any = input
}).build())
.schemaString(
"""
scalar UUID
type Query {
first: UUID
second: UUID
}
""")
.build()
.makeExecutableSchema()
assert(schema.typeMap.containsKey("UUID"))
}
class ScalarsWithMultipleTypes : GraphQLQueryResolver {
fun first(): Int? = null
fun second(): String? = null
}
@Test
fun `scanner handles multiple interfaces that are not used as field types`() {
val schema = SchemaParser.newParser()
.resolvers(MultipleInterfaces())
.schemaString(
"""
type Query {
query1: NamedResourceImpl
query2: VersionedResourceImpl
}
interface NamedResource {
name: String!
}
interface VersionedResource {
version: Int!
}
type NamedResourceImpl implements NamedResource {
name: String!
}
type VersionedResourceImpl implements VersionedResource {
version: Int!
}
""")
.build()
.makeExecutableSchema()
val interfaceTypeCount = schema.additionalTypes.count { it is GraphQLInterfaceType }
assertEquals(interfaceTypeCount, 2)
}
class MultipleInterfaces : GraphQLQueryResolver {
fun query1(): NamedResourceImpl? = null
fun query2(): VersionedResourceImpl? = null
class NamedResourceImpl : NamedResource {
override fun name(): String? = null
}
class VersionedResourceImpl : VersionedResource {
override fun version(): Int? = null
}
}
interface NamedResource {
fun name(): String?
}
interface VersionedResource {
fun version(): Int?
}
@Test
fun `scanner handles interface implementation that is not used as field type`() {
val schema = SchemaParser.newParser()
// uncommenting the line below makes the test succeed
.dictionary(InterfaceImplementation.NamedResourceImpl::class)
.resolvers(InterfaceImplementation())
.schemaString(
"""
type Query {
query1: NamedResource
}
interface NamedResource {
name: String!
}
type NamedResourceImpl implements NamedResource {
name: String!
}
""")
.build()
.makeExecutableSchema()
val interfaceTypeCount = schema.additionalTypes.count { it is GraphQLInterfaceType }
assertEquals(interfaceTypeCount, 1)
}
class InterfaceImplementation : GraphQLQueryResolver {
fun query1(): NamedResource? = null
fun query2(): NamedResourceImpl? = null
class NamedResourceImpl : NamedResource {
override fun name(): String? = null
}
}
@Test
fun `scanner handles custom scalars when matching input types`() {
val customMap = GraphQLScalarType.newScalar()
.name("customMap")
.coercing(object : Coercing<Map<String, Any>, Map<String, Any>> {
override fun serialize(dataFetcherResult: Any, context: GraphQLContext, locale: Locale): Map<String, Any> = mapOf()
override fun parseValue(input: Any, context: GraphQLContext, locale: Locale): Map<String, Any> = mapOf()
override fun parseLiteral(input: Value<*>, variables: CoercedVariables, context: GraphQLContext, locale: Locale): Map<String, Any> = mapOf()
}).build()
val schema = SchemaParser.newParser()
.resolvers(object : GraphQLQueryResolver {
fun hasRawScalar(rawScalar: Map<String, Any>): Boolean = true
fun hasMapField(mapField: HasMapField): Boolean = true
})
.scalars(customMap)
.schemaString(
"""
type Query {
hasRawScalar(customMap: customMap): Boolean
hasMapField(mapField: HasMapField): Boolean
}
input HasMapField {
map: customMap
}
scalar customMap
""")
.build()
.makeExecutableSchema()
assert(schema.typeMap.containsKey("customMap"))
}
class HasMapField {
var map: Map<String, Any>? = null
}
@Test
fun `scanner allows class to be used for object type and input object type`() {
val schema = SchemaParser.newParser()
.resolvers(object : GraphQLQueryResolver {
fun test(pojo: Pojo): Pojo = pojo
})
.schemaString(
"""
type Query {
test(inPojo: InPojo): OutPojo
}
input InPojo {
name: String
}
type OutPojo {
name: String
}
""")
.build()
.makeExecutableSchema()
val typeCount = schema.additionalTypes.count()
assertEquals(typeCount, 2)
}
class Pojo {
var name: String? = null
}
@Test
fun `scanner should handle nested types in input types`() {
val schema = SchemaParser.newParser()
.schemaString(
"""
schema {
query: Query
}
type Query {
animal: Animal
}
interface Animal {
type: ComplexType
}
type Dog implements Animal {
type: ComplexType
}
type ComplexType {
id: String
}
""")
.resolvers(NestedInterfaceTypeQuery())
.dictionary(NestedInterfaceTypeQuery.Dog::class)
.build()
.makeExecutableSchema()
val typeCount = schema.additionalTypes.count()
assertEquals(typeCount, 3)
}
class NestedInterfaceTypeQuery : GraphQLQueryResolver {
fun animal(): Animal? = null
class Dog : Animal {
override fun type(): ComplexType? = null
}
class ComplexType {
var id: String? = null
}
}
@Test
fun `scanner should handle unused types when option is true`() {
val schema = SchemaParser.newParser()
.schemaString(
"""
# these directives are defined in the Apollo Federation Specification:
# https://www.apollographql.com/docs/apollo-server/federation/federation-spec/
scalar FieldSet
scalar link__Import
enum link__Purpose { SECURITY EXECUTION }
directive @key(fields: FieldSet!, resolvable: Boolean = true) repeatable on OBJECT | INTERFACE
directive @extends on OBJECT | INTERFACE
directive @external on FIELD_DEFINITION | OBJECT
directive @link(url: String!, as: String, for: link__Purpose) repeatable on SCHEMA
extend schema @link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@key", "@shareable"])
# Let's say this is the Products service from Apollo Federation Introduction
type Query {
allProducts: [Product]
}
type Product {
name: String
}
type User @key(fields: "id") @extends {
id: ID! @external
recentPurchasedProducts: [Product]
address: Address
}
type Address {
street: String
}
""")
.resolvers(object : GraphQLQueryResolver {
fun allProducts(): List<Product>? = null
})
.options(SchemaParserOptions.newOptions().includeUnusedTypes(true).build())
.dictionary(User::class)
.dictionary("link__Purpose", LinkPurpose::class)
.scalars(fieldSetScalar)
.build()
.makeExecutableSchema()
val objectTypes = schema.additionalTypes.filterIsInstance<GraphQLObjectType>()
assert(objectTypes.any { it.name == "User" })
assert(objectTypes.any { it.name == "Address" })
}
data class FieldSet(val value: String)
enum class LinkPurpose { SECURITY, EXECUTION }
private val fieldSetScalar: GraphQLScalarType = GraphQLScalarType.newScalar()
.name("FieldSet")
.coercing(object : Coercing<FieldSet, String> {
override fun serialize(input: Any, context: GraphQLContext, locale: Locale) = input.toString()
override fun parseValue(input: Any, context: GraphQLContext, locale: Locale) =
FieldSet(input.toString())
override fun parseLiteral(input: Value<*>, variables: CoercedVariables, context: GraphQLContext, locale: Locale) =
FieldSet(input.toString())
})
.build()
class Product {
var name: String? = null
}
class User {
var id: String? = null
var recentPurchasedProducts: List<Product>? = null
var address: Address? = null
}
class Address {
var street: String? = null
}
@Test
fun `scanner should handle unused types with interfaces when option is true`() {
val schema = SchemaParser.newParser()
.schemaString(
"""
type Query {
whatever: Whatever
}
type Whatever {
value: String
}
type Unused {
someInterface: SomeInterface
}
interface SomeInterface {
value: String
}
type Implementation implements SomeInterface {
value: String
}
""")
.resolvers(object : GraphQLQueryResolver {
fun whatever(): Whatever? = null
})
.options(SchemaParserOptions.newOptions().includeUnusedTypes(true).build())
.dictionary(Unused::class, Implementation::class)
.build()
.makeExecutableSchema()
val objectTypes = schema.additionalTypes.filterIsInstance<GraphQLObjectType>()
val interfaceTypes = schema.additionalTypes.filterIsInstance<GraphQLInterfaceType>()
assert(objectTypes.any { it.name == "Unused" })
assert(objectTypes.any { it.name == "Implementation" })
assert(interfaceTypes.any { it.name == "SomeInterface" })
}
class Whatever {
var value: String? = null
}
class Unused {
var someInterface: SomeInterface? = null
}
class Implementation : SomeInterface {
override fun getValue(): String? {
return null
}
}
}