-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
RuntimeWiringTest.groovy
240 lines (191 loc) · 8.57 KB
/
RuntimeWiringTest.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package graphql.schema.idl
import graphql.Scalars
import graphql.TypeResolutionEnvironment
import graphql.schema.Coercing
import graphql.schema.DataFetcher
import graphql.schema.DataFetchingEnvironment
import graphql.schema.GraphQLFieldDefinition
import graphql.schema.GraphQLFieldsContainer
import graphql.schema.GraphQLObjectType
import graphql.schema.GraphQLScalarType
import graphql.schema.TypeResolver
import graphql.schema.idl.errors.StrictModeWiringException
import graphql.schema.visibility.GraphqlFieldVisibility
import spock.lang.Specification
import java.util.function.UnaryOperator
class RuntimeWiringTest extends Specification {
public static final Coercing coercing = new Coercing() {
@Override
Object serialize(Object input) {
throw new UnsupportedOperationException("Not implemented")
}
@Override
Object parseValue(Object input) {
throw new UnsupportedOperationException("Not implemented")
}
@Override
Object parseLiteral(Object input) {
throw new UnsupportedOperationException("Not implemented")
}
}
class NamedDF implements DataFetcher {
String name
NamedDF(String name) {
this.name = name
}
@Override
Object get(DataFetchingEnvironment environment) {
throw new UnsupportedOperationException("Not implemented")
}
}
class NamedTR implements TypeResolver {
String name
NamedTR(String name) {
this.name = name
}
@Override
GraphQLObjectType getType(TypeResolutionEnvironment env) {
throw new UnsupportedOperationException("Not implemented")
}
}
def "basic call structure"() {
def wiring = RuntimeWiring.newRuntimeWiring()
.type("Query", { type ->
type
.dataFetcher("fieldX", new NamedDF("fieldX"))
.dataFetcher("fieldY", new NamedDF("fieldY"))
.dataFetcher("fieldZ", new NamedDF("fieldZ"))
.defaultDataFetcher(new NamedDF("defaultQueryDF"))
.typeResolver(new NamedTR("typeResolver4Query"))
} as UnaryOperator<TypeRuntimeWiring.Builder>)
.type("Mutation", { type ->
type
.dataFetcher("fieldX", new NamedDF("mfieldX"))
.dataFetcher("fieldY", new NamedDF("mfieldY"))
.dataFetcher("fieldZ", new NamedDF("mfieldZ"))
.defaultDataFetcher(new NamedDF("defaultMutationDF"))
.typeResolver(new NamedTR("typeResolver4Mutation"))
} as UnaryOperator<TypeRuntimeWiring.Builder>)
.build()
def fetchers = wiring.getDataFetchers()
def resolvers = wiring.getTypeResolvers()
expect:
(fetchers.get("Query").get("fieldX") as NamedDF).name == "fieldX"
(fetchers.get("Query").get("fieldY") as NamedDF).name == "fieldY"
(fetchers.get("Query").get("fieldZ") as NamedDF).name == "fieldZ"
(wiring.getDefaultDataFetcherForType("Query") as NamedDF).name == "defaultQueryDF"
(resolvers.get("Query") as NamedTR).name == "typeResolver4Query"
(fetchers.get("Mutation").get("fieldX") as NamedDF).name == "mfieldX"
(fetchers.get("Mutation").get("fieldY") as NamedDF).name == "mfieldY"
(fetchers.get("Mutation").get("fieldZ") as NamedDF).name == "mfieldZ"
(wiring.getDefaultDataFetcherForType("Mutation") as NamedDF).name == "defaultMutationDF"
(resolvers.get("Mutation") as NamedTR).name == "typeResolver4Mutation"
}
def "scalars are present"() {
def customScalar = GraphQLScalarType.newScalar().name("URL").description("Custom").coercing(coercing).build()
def wiring = RuntimeWiring.newRuntimeWiring().scalar(customScalar).build()
expect:
wiring.getScalars().get("URL").name == "URL"
wiring.getScalars().get("URL") == customScalar
wiring.getScalars().get("Int").name == "Int"
wiring.getScalars().get("Float").name == "Float"
wiring.getScalars().get("String").name == "String"
wiring.getScalars().get("Boolean").name == "Boolean"
wiring.getScalars().get("ID").name == "ID"
}
def "newRuntimeWiring works and copies values"() {
when:
def customScalar1 = GraphQLScalarType.newScalar()
.name("Custom1").description("Custom 1").coercing(coercing).build()
def oldWiring = RuntimeWiring.newRuntimeWiring()
.scalar(customScalar1)
.build()
def customScalar2 = GraphQLScalarType.newScalar()
.name("Custom2").description("Custom 2").coercing(coercing).build()
GraphqlFieldVisibility fieldVisibility = new GraphqlFieldVisibility() {
@Override
List<GraphQLFieldDefinition> getFieldDefinitions(GraphQLFieldsContainer fieldsContainer) {
return null
}
@Override
GraphQLFieldDefinition getFieldDefinition(GraphQLFieldsContainer fieldsContainer, String fieldName) {
return null
}
}
def newWiring = RuntimeWiring.newRuntimeWiring(oldWiring)
.scalar(customScalar2)
.fieldVisibility(fieldVisibility)
.build()
then:
newWiring.scalars.entrySet().containsAll(oldWiring.scalars.entrySet())
newWiring.scalars["Custom1"] == customScalar1
newWiring.scalars["Custom2"] == customScalar2
newWiring.fieldVisibility == fieldVisibility
}
def "transform works and copies values"() {
when:
def customScalar1 = GraphQLScalarType.newScalar()
.name("Custom1").description("Custom 1").coercing(coercing).build()
def oldWiring = RuntimeWiring.newRuntimeWiring()
.scalar(customScalar1)
.build()
def customScalar2 = GraphQLScalarType.newScalar()
.name("Custom2").description("Custom 2").coercing(coercing).build()
GraphqlFieldVisibility fieldVisibility = new GraphqlFieldVisibility() {
@Override
List<GraphQLFieldDefinition> getFieldDefinitions(GraphQLFieldsContainer fieldsContainer) {
return null
}
@Override
GraphQLFieldDefinition getFieldDefinition(GraphQLFieldsContainer fieldsContainer, String fieldName) {
return null
}
}
def newWiring = oldWiring.transform({ builder ->
builder
.scalar(customScalar2)
.fieldVisibility(fieldVisibility)
})
then:
newWiring.scalars.entrySet().containsAll(oldWiring.scalars.entrySet())
newWiring.scalars["Custom1"] == customScalar1
newWiring.scalars["Custom2"] == customScalar2
newWiring.fieldVisibility == fieldVisibility
}
def "strict mode can stop certain redefinitions"() {
DataFetcher DF1 = env -> "x"
TypeResolver TR1 = env -> null
EnumValuesProvider EVP1 = name -> null
when:
RuntimeWiring.newRuntimeWiring()
.strictMode()
.type(TypeRuntimeWiring.newTypeWiring("Foo").dataFetcher("foo", DF1))
.type(TypeRuntimeWiring.newTypeWiring("Foo").dataFetcher("bar", DF1))
then:
def e1 = thrown(StrictModeWiringException)
e1.message == "The type Foo has already been defined"
when:
RuntimeWiring.newRuntimeWiring()
.strictMode()
.type(TypeRuntimeWiring.newTypeWiring("Foo").typeResolver(TR1))
.type(TypeRuntimeWiring.newTypeWiring("Foo").typeResolver(TR1))
then:
def e2 = thrown(StrictModeWiringException)
e2.message == "The type Foo already has a type resolver defined"
when:
RuntimeWiring.newRuntimeWiring()
.strictMode()
.type(TypeRuntimeWiring.newTypeWiring("Foo").enumValues(EVP1))
.type(TypeRuntimeWiring.newTypeWiring("Foo").enumValues(EVP1))
then:
def e3 = thrown(StrictModeWiringException)
e3.message == "The type Foo already has a enum provider defined"
when:
RuntimeWiring.newRuntimeWiring()
.strictMode()
.scalar(Scalars.GraphQLString)
then:
def e4 = thrown(StrictModeWiringException)
e4.message == "The scalar String is already defined"
}
}