forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NullVariableCoercionTest.groovy
124 lines (96 loc) · 3.62 KB
/
NullVariableCoercionTest.groovy
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
package graphql
import graphql.language.SourceLocation
import graphql.schema.DataFetcher
import graphql.schema.GraphQLObjectType
import graphql.schema.idl.RuntimeWiring
import spock.lang.Specification
import static graphql.ExecutionInput.newExecutionInput
import static graphql.schema.idl.TypeRuntimeWiring.newTypeWiring
class NullVariableCoercionTest extends Specification {
def "null coercion errors have source locations"() {
when:
RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
.type(newTypeWiring("Query")
.dataFetcher("bar",
{ env ->
Map<String, Object> map = new HashMap<>()
map.put("id", "def")
return map
})
)
.type(newTypeWiring("Node")
.typeResolver({ env -> (GraphQLObjectType) env.getSchema().getType("Foo") }))
.build()
def graphQL = TestUtil.graphQL("""
schema {
query: Query
}
type Query {
bar(input: BarInput!): Node
}
input BarInput {
baz: String!
}
interface Node {
id: String
}
type Foo implements Node {
id: String
}
""", runtimeWiring).build()
def variables = ["input": ["baz": null]]
ExecutionInput varInput = newExecutionInput()
.query('query Bar($input: BarInput!) {bar(input: $input) {id}}')
.variables(variables)
.build()
ExecutionResult varResult = graphQL
.executeAsync(varInput)
.join()
then:
varResult.data == null
varResult.errors.size() == 1
varResult.errors[0].errorType == ErrorType.ValidationError
varResult.errors[0].message == "Variable 'input' has invalid value: Field 'baz' has coerced Null value for NonNull type 'String!'"
varResult.errors[0].locations == [new SourceLocation(1, 11)]
}
def "can handle defaulting on complex input objects"() {
def sdl = '''
input Kitchen {
pantry : String
}
input ListSnacksInput {
kitchen: [Kitchen!]! = [{pantry : "Cheezels"}]
snackType : String = "healthy"
startPageToken: String
}
type Snacks {
name : String
}
type Query {
listSnacks(input: ListSnacksInput!) : [Snacks!]!
}
'''
DataFetcher df = { env ->
def val = env.getArgument("input")
assert val instanceof Map
def defaultedKitchenList = val["kitchen"]
assert defaultedKitchenList instanceof List
def snackVal = defaultedKitchenList[0]["pantry"]
def snack = [name: snackVal]
return [snack]
}
def graphQL = TestUtil.graphQL(sdl, [Query: [listSnacks: df]]).build()
def query = '''
query ($input: ListSnacksInput!){
listSnacks(input: $input) {
name
}
}
'''
when:
def er = graphQL.execute(newExecutionInput(query).variables([input: [startPageToken: "dummyToken"]]))
then:
er.errors.isEmpty()
er.data == [listSnacks: [[name: "Cheezels"]]]
}
}