forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphQLTest.groovy
219 lines (179 loc) · 6.64 KB
/
GraphQLTest.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
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
package graphql
import graphql.language.SourceLocation
import graphql.schema.*
import graphql.validation.ValidationErrorType
import spock.lang.Specification
import static graphql.Scalars.GraphQLString
import static graphql.schema.GraphQLArgument.newArgument
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition
import static graphql.schema.GraphQLObjectType.newObject
import static graphql.schema.GraphQLSchema.newSchema
class GraphQLTest extends Specification {
def "simple query"() {
given:
GraphQLFieldDefinition.Builder fieldDefinition = newFieldDefinition()
.name("hello")
.type(GraphQLString)
.staticValue("world")
GraphQLSchema schema = newSchema().query(
newObject()
.name("RootQueryType")
.field(fieldDefinition)
.build()
).build()
when:
def result = GraphQL.newGraphQL(schema).build().execute('{ hello }').data
then:
result == [hello: 'world']
}
def "query with sub-fields"() {
given:
GraphQLObjectType heroType = newObject()
.name("heroType")
.field(
newFieldDefinition()
.name("id")
.type(GraphQLString))
.field(
newFieldDefinition()
.name("name")
.type(GraphQLString))
.build()
GraphQLFieldDefinition.Builder simpsonField = newFieldDefinition()
.name("simpson")
.type(heroType)
.staticValue([id: '123', name: 'homer'])
GraphQLSchema graphQLSchema = newSchema().query(
newObject()
.name("RootQueryType")
.field(simpsonField)
.build()
).build()
when:
def result = GraphQL.newGraphQL(graphQLSchema).build().execute('{ simpson { id, name } }').data
then:
result == [simpson: [id: '123', name: 'homer']]
}
def "query with validation errors"() {
given:
GraphQLFieldDefinition.Builder fieldDefinition = newFieldDefinition()
.name("hello")
.type(GraphQLString)
.argument(newArgument().name("arg").type(GraphQLString))
.staticValue("world")
GraphQLSchema schema = newSchema().query(
newObject()
.name("RootQueryType")
.field(fieldDefinition)
.build()
).build()
when:
def errors = GraphQL.newGraphQL(schema).build().execute('{ hello(arg:11) }').errors
then:
errors.size() == 1
}
def "query with invalid syntax"() {
given:
GraphQLSchema schema = newSchema().query(
newObject()
.name("RootQueryType")
.build()
).build()
when:
def errors = GraphQL.newGraphQL(schema).build().execute('{ hello(() }').errors
then:
errors.size() == 1
errors[0].errorType == ErrorType.InvalidSyntax
errors[0].sourceLocations == [new SourceLocation(1, 8)]
}
def "query with invalid syntax 2"() {
given:
GraphQLSchema schema = newSchema().query(
newObject()
.name("RootQueryType")
.build()
).build()
when:
def errors = GraphQL.newGraphQL(schema).build().execute('{ hello[](() }').errors
then:
errors.size() == 1
errors[0].errorType == ErrorType.InvalidSyntax
errors[0].sourceLocations == [new SourceLocation(1, 7)]
}
def "non null argument is missing"() {
given:
GraphQLSchema schema = newSchema().query(
newObject()
.name("RootQueryType")
.field(newFieldDefinition()
.name("field")
.type(GraphQLString)
.argument(newArgument()
.name("arg")
.type(new GraphQLNonNull(GraphQLString))))
.build()
).build()
when:
def errors = GraphQL.newGraphQL(schema).build().execute('{ field }').errors
then:
errors.size() == 1
errors[0].errorType == ErrorType.ValidationError
errors[0].validationErrorType == ValidationErrorType.MissingFieldArgument
errors[0].sourceLocations == [new SourceLocation(1, 3)]
}
def "`Iterable` can be used as a `GraphQLList` field result"() {
given:
def set = new HashSet<String>()
set.add("One")
set.add("Two")
def schema = GraphQLSchema.newSchema()
.query(GraphQLObjectType.newObject()
.name("QueryType")
.field(GraphQLFieldDefinition.newFieldDefinition()
.name("set")
.type(new GraphQLList(GraphQLString))
.dataFetcher({ set })))
.build()
when:
def data = GraphQL.newGraphQL(schema).build().execute("query { set }").data
then:
data == [set: ['One', 'Two']]
}
def "document with two operations executes specified operation"() {
given:
GraphQLSchema schema = newSchema().query(
newObject()
.name("RootQueryType")
.field(newFieldDefinition().name("field1").type(GraphQLString).dataFetcher(new StaticDataFetcher("value1")))
.field(newFieldDefinition().name("field2").type(GraphQLString).dataFetcher(new StaticDataFetcher("value2")))
)
.build()
def query = """
query Query1 { field1 }
query Query2 { field2 }
"""
def expected = [field2: 'value2']
when:
def result = GraphQL.newGraphQL(schema).build().execute(query, 'Query2', null, [:])
then:
result.data == expected
result.errors.size() == 0
}
def "document with two operations but no specified operation throws"() {
given:
GraphQLSchema schema = newSchema().query(
newObject()
.name("RootQueryType")
.field(newFieldDefinition().name("name").type(GraphQLString))
)
.build()
def query = """
query Query1 { name }
query Query2 { name }
"""
when:
GraphQL.newGraphQL(schema).build().execute(query)
then:
thrown(GraphQLException)
}
}