forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphQLTest.groovy
158 lines (133 loc) · 4.84 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
package graphql
import graphql.language.SourceLocation
import graphql.schema.GraphQLFieldDefinition
import graphql.schema.GraphQLNonNull
import graphql.schema.GraphQLObjectType
import graphql.schema.GraphQLSchema
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 fieldDefinition = newFieldDefinition()
.name("hello")
.type(GraphQLString)
.staticValue("world")
.build()
GraphQLSchema schema = newSchema().query(
newObject()
.name("RootQueryType")
.field(fieldDefinition)
.build()
).build()
when:
def result = new GraphQL(schema).execute('{ hello }').data
then:
result == [hello: 'world']
}
def "query with sub-fields"() {
given:
GraphQLObjectType heroType = newObject()
.name("heroType")
.field(
newFieldDefinition()
.name("id")
.type(GraphQLString)
.build())
.field(
newFieldDefinition()
.name("name")
.type(GraphQLString)
.build())
.build()
GraphQLFieldDefinition simpsonField = newFieldDefinition()
.name("simpson")
.type(heroType)
.staticValue([id: '123', name: 'homer']).build()
GraphQLSchema graphQLSchema = newSchema().query(
newObject()
.name("RootQueryType")
.field(simpsonField)
.build()
).build();
when:
def result = new GraphQL(graphQLSchema).execute('{ simpson { id, name } }').data
then:
result == [simpson: [id: '123', name: 'homer']]
}
def "query with validation errors"() {
given:
GraphQLFieldDefinition fieldDefinition = newFieldDefinition()
.name("hello")
.type(GraphQLString)
.argument(newArgument().name("arg").type(GraphQLString).build())
.staticValue("world")
.build()
GraphQLSchema schema = newSchema().query(
newObject()
.name("RootQueryType")
.field(fieldDefinition)
.build()
).build()
when:
def errors = new GraphQL(schema).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 = new GraphQL(schema).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 = new GraphQL(schema).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())
.build()
).build()
when:
def errors = new GraphQL(schema).execute('{ field }').errors
then:
errors.size() == 1
errors[0].errorType == ErrorType.ValidationError
errors[0].validationErrorType == ValidationErrorType.MissingFieldArgument
errors[0].sourceLocations == [new SourceLocation(1, 3)]
}
}