@@ -2,6 +2,8 @@ package graphql.schema.validation
22
33import graphql.TypeResolutionEnvironment
44import graphql.schema.GraphQLInterfaceType
5+ import graphql.schema.GraphQLList
6+ import graphql.schema.GraphQLNonNull
57import graphql.schema.GraphQLObjectType
68import graphql.schema.TypeResolver
79import spock.lang.Specification
@@ -13,6 +15,9 @@ import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition
1315import static graphql.schema.GraphQLInterfaceType.newInterface
1416import static graphql.schema.GraphQLList.list
1517import static graphql.schema.GraphQLNonNull.nonNull
18+ import static graphql.schema.GraphQLObjectType.newObject
19+ import static graphql.schema.GraphQLObjectType.newObject
20+ import static graphql.schema.GraphQLUnionType.newUnionType
1621
1722class ObjectsImplementInterfacesTest extends Specification {
1823
@@ -78,20 +83,203 @@ class ObjectsImplementInterfacesTest extends Specification {
7883
7984 errorCollector. containsValidationError(ObjectDoesNotImplementItsInterfaces )
8085 def errors = errorCollector. getErrors()
81- errors. size() == 7
86+ errors. size() == 6
8287 errors. contains(new SchemaValidationError (ObjectDoesNotImplementItsInterfaces ,
8388 " object type 'obj' does not implement interface 'Interface' because field 'friends' is missing" ))
8489 errors. contains(new SchemaValidationError (ObjectDoesNotImplementItsInterfaces ,
8590 " object type 'obj' does not implement interface 'Interface' because field 'age' is defined as 'String' type and not as 'Int' type" ))
86- errors. contains(new SchemaValidationError (ObjectDoesNotImplementItsInterfaces ,
87- " object type 'obj' does not implement interface 'Interface' because field 'address' is defined as '[String!]' type and not as '[String]' type" ))
88- errors. contains(new SchemaValidationError (ObjectDoesNotImplementItsInterfaces ,
89- " object type 'obj' does not implement interface 'Interface' because field 'address' is defined as '[String!]' type and not as '[String]' type" ))
9091 errors. contains(new SchemaValidationError (ObjectDoesNotImplementItsInterfaces ,
9192 " object type 'obj' does not implement interface 'Interface' because field 'argField1' argument 'arg1' is defined differently" ))
9293 errors. contains(new SchemaValidationError (ObjectDoesNotImplementItsInterfaces ,
93- " object type 'obj' does not implement interface 'Interface' because field 'argField1' argument 'arg1' is defined differently" ))
94+ " object type 'obj' does not implement interface 'Interface' because field 'argField1' argument 'arg3' is defined differently" ))
95+ errors. contains(new SchemaValidationError (ObjectDoesNotImplementItsInterfaces ,
96+ " object type 'obj' does not implement interface 'Interface' because field 'argField1' argument 'arg4' is defined differently" ))
9497 errors. contains(new SchemaValidationError (ObjectDoesNotImplementItsInterfaces ,
9598 " object type 'obj' does not implement interface 'Interface' because field 'argField2' has a different number of arguments" ))
9699 }
100+
101+ def " field is object implementing interface" () {
102+ given :
103+ def person = newInterface()
104+ .name(" Person" )
105+ .field(newFieldDefinition(). name(" name" ). type(GraphQLString ). build())
106+ .typeResolver({})
107+ .build()
108+
109+ def actor = newObject()
110+ .name(" Actor" )
111+ .field(newFieldDefinition(). name(" name" ). type(GraphQLString ). build())
112+ .withInterface(person)
113+ .build()
114+
115+ def prop = newObject()
116+ .name(" Prop" )
117+ .field(newFieldDefinition(). name(" name" ). type(GraphQLString ). build())
118+ .build()
119+
120+ GraphQLInterfaceType interfaceType = newInterface()
121+ .name(" TestInterface" )
122+ .field(newFieldDefinition(). name(" field" ). type(person). build())
123+ .typeResolver({})
124+ .build()
125+
126+ GraphQLObjectType goodImpl = newObject()
127+ .name(" GoodImpl" )
128+ .field(newFieldDefinition(). name(" field" ). type(actor). build())
129+ .withInterface(interfaceType)
130+ .build()
131+
132+ GraphQLObjectType badImpl = newObject()
133+ .name(" BadImpl" )
134+ .field(newFieldDefinition(). name(" field" ). type(prop). build())
135+ .withInterface(interfaceType)
136+ .build()
137+
138+ SchemaValidationErrorCollector goodErrorCollector = new SchemaValidationErrorCollector ()
139+ SchemaValidationErrorCollector badErrorCollector = new SchemaValidationErrorCollector ()
140+
141+ when :
142+ new ObjectsImplementInterfaces (). check(goodImpl, goodErrorCollector)
143+ new ObjectsImplementInterfaces (). check(badImpl, badErrorCollector)
144+
145+ then :
146+ goodErrorCollector. getErrors(). isEmpty()
147+ ! badErrorCollector. getErrors(). isEmpty()
148+ }
149+
150+ def " field is list of objects implementing interface" () {
151+ given :
152+ def person = newInterface()
153+ .name(" Person" )
154+ .field(newFieldDefinition(). name(" name" ). type(GraphQLString ). build())
155+ .typeResolver({})
156+ .build()
157+
158+ def actor = newObject()
159+ .name(" Actor" )
160+ .field(newFieldDefinition(). name(" name" ). type(GraphQLString ). build())
161+ .withInterface(person)
162+ .build()
163+
164+ def prop = newObject()
165+ .name(" Prop" )
166+ .field(newFieldDefinition(). name(" name" ). type(GraphQLString ). build())
167+ .build()
168+
169+ GraphQLInterfaceType interfaceType = newInterface()
170+ .name(" TestInterface" )
171+ .field(newFieldDefinition(). name(" field" ). type(new GraphQLList (person)). build())
172+ .typeResolver({})
173+ .build()
174+
175+ GraphQLObjectType goodImpl = newObject()
176+ .name(" GoodImpl" )
177+ .field(newFieldDefinition(). name(" field" ). type(new GraphQLList (actor)). build())
178+ .withInterface(interfaceType)
179+ .build()
180+
181+ GraphQLObjectType badImpl = newObject()
182+ .name(" BadImpl" )
183+ .field(newFieldDefinition(). name(" field" ). type(new GraphQLList (prop)). build())
184+ .withInterface(interfaceType)
185+ .build()
186+
187+ SchemaValidationErrorCollector goodErrorCollector = new SchemaValidationErrorCollector ()
188+ SchemaValidationErrorCollector badErrorCollector = new SchemaValidationErrorCollector ()
189+
190+ when :
191+ new ObjectsImplementInterfaces (). check(goodImpl, goodErrorCollector)
192+ new ObjectsImplementInterfaces (). check(badImpl, badErrorCollector)
193+
194+ then :
195+ goodErrorCollector. getErrors(). isEmpty()
196+ ! badErrorCollector. getErrors(). isEmpty()
197+ }
198+
199+ def " field is member of union" () {
200+ given :
201+ def actor = newObject()
202+ .name(" Actor" )
203+ .field(newFieldDefinition(). name(" name" ). type(GraphQLString ). build())
204+ .build()
205+
206+ def director = newObject()
207+ .name(" Director" )
208+ .field(newFieldDefinition(). name(" name" ). type(GraphQLString ). build())
209+ .build()
210+
211+ def person = newUnionType()
212+ .name(" Person" )
213+ .possibleType(actor)
214+ .possibleType(director)
215+ .typeResolver({})
216+ .build()
217+
218+ def prop = newObject()
219+ .name(" Prop" )
220+ .field(newFieldDefinition(). name(" name" ). type(GraphQLString ). build())
221+ .build()
222+
223+ GraphQLInterfaceType interfaceType = newInterface()
224+ .name(" TestInterface" )
225+ .field(newFieldDefinition(). name(" field" ). type(person). build())
226+ .typeResolver({})
227+ .build()
228+
229+ GraphQLObjectType goodImpl = newObject()
230+ .name(" GoodImpl" )
231+ .field(newFieldDefinition(). name(" field" ). type(actor). build())
232+ .withInterface(interfaceType)
233+ .build()
234+
235+ GraphQLObjectType badImpl = newObject()
236+ .name(" BadImpl" )
237+ .field(newFieldDefinition(). name(" field" ). type(prop). build())
238+ .withInterface(interfaceType)
239+ .build()
240+
241+ SchemaValidationErrorCollector goodErrorCollector = new SchemaValidationErrorCollector ()
242+ SchemaValidationErrorCollector badErrorCollector = new SchemaValidationErrorCollector ()
243+
244+ when :
245+ new ObjectsImplementInterfaces (). check(goodImpl, goodErrorCollector)
246+ new ObjectsImplementInterfaces (). check(badImpl, badErrorCollector)
247+
248+ then :
249+ goodErrorCollector. getErrors(). isEmpty()
250+ ! badErrorCollector. getErrors(). isEmpty()
251+ }
252+
253+ def " field is non-null" () {
254+ given :
255+ GraphQLInterfaceType interfaceType = newInterface()
256+ .name(" TestInterface" )
257+ .field(newFieldDefinition(). name(" field" ). type(GraphQLString ). build())
258+ .typeResolver({})
259+ .build()
260+
261+ GraphQLObjectType goodImpl = newObject()
262+ .name(" GoodImpl" )
263+ .field(newFieldDefinition(). name(" field" ). type(new GraphQLNonNull (GraphQLString )). build())
264+ .withInterface(interfaceType)
265+ .build()
266+
267+ GraphQLObjectType badImpl = newObject()
268+ .name(" BadImpl" )
269+ .field(newFieldDefinition(). name(" field" ). type(new GraphQLNonNull (GraphQLInt )). build())
270+ .withInterface(interfaceType)
271+ .build()
272+
273+ SchemaValidationErrorCollector goodErrorCollector = new SchemaValidationErrorCollector ()
274+ SchemaValidationErrorCollector badErrorCollector = new SchemaValidationErrorCollector ()
275+
276+ when :
277+ new ObjectsImplementInterfaces (). check(goodImpl, goodErrorCollector)
278+ new ObjectsImplementInterfaces (). check(badImpl, badErrorCollector)
279+
280+ then :
281+ goodErrorCollector. getErrors(). isEmpty()
282+ ! badErrorCollector. getErrors(). isEmpty()
283+ }
284+
97285}
0 commit comments