Skip to content

Commit 3e7ef66

Browse files
rhmollerbbakerman
authored andcommitted
Improved object type validation with support for covariant types
1 parent e448e48 commit 3e7ef66

2 files changed

Lines changed: 236 additions & 23 deletions

File tree

‎src/main/java/graphql/schema/validation/ObjectsImplementInterfaces.java‎

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package graphql.schema.validation;
22

3-
import graphql.schema.GraphQLArgument;
4-
import graphql.schema.GraphQLFieldDefinition;
5-
import graphql.schema.GraphQLInterfaceType;
6-
import graphql.schema.GraphQLObjectType;
7-
import graphql.schema.GraphQLOutputType;
8-
import graphql.schema.GraphQLType;
3+
import graphql.schema.*;
94

105
import java.util.List;
116
import java.util.Objects;
@@ -31,12 +26,11 @@ public void check(GraphQLType type, SchemaValidationErrorCollector validationErr
3126
}
3227
}
3328

34-
// visible for testing
35-
private void check(GraphQLObjectType objectTyoe, SchemaValidationErrorCollector validationErrorCollector) {
36-
List<GraphQLOutputType> interfaces = objectTyoe.getInterfaces();
29+
private void check(GraphQLObjectType objectType, SchemaValidationErrorCollector validationErrorCollector) {
30+
List<GraphQLOutputType> interfaces = objectType.getInterfaces();
3731
interfaces.forEach(interfaceType -> {
3832
// we have resolved the interfaces at this point and hence the cast is ok
39-
checkObjectImplementsInterface(objectTyoe, (GraphQLInterfaceType) interfaceType, validationErrorCollector);
33+
checkObjectImplementsInterface(objectType, (GraphQLInterfaceType) interfaceType, validationErrorCollector);
4034
});
4135

4236
}
@@ -50,23 +44,21 @@ private void checkObjectImplementsInterface(GraphQLObjectType objectTyoe, GraphQ
5044
error(format("object type '%s' does not implement interface '%s' because field '%s' is missing",
5145
objectTyoe.getName(), interfaceType.getName(), interfaceFieldDef.getName())));
5246
} else {
53-
checkFieldTypeEquivalence(objectTyoe, interfaceType, validationErrorCollector, interfaceFieldDef, objectFieldDef);
47+
checkFieldTypeCompatibility(objectTyoe, interfaceType, validationErrorCollector, interfaceFieldDef, objectFieldDef);
5448
}
5549
}
5650
}
5751

58-
private void checkFieldTypeEquivalence(GraphQLObjectType objectTyoe, GraphQLInterfaceType interfaceType, SchemaValidationErrorCollector validationErrorCollector, GraphQLFieldDefinition interfaceFieldDef, GraphQLFieldDefinition objectFieldDef) {
59-
// the reference implementation has a full but complicated abstract type equivalence check
60-
// this is not that but we can add that later. It does cover the major cases however
52+
private void checkFieldTypeCompatibility(GraphQLObjectType objectType, GraphQLInterfaceType interfaceType, SchemaValidationErrorCollector validationErrorCollector, GraphQLFieldDefinition interfaceFieldDef, GraphQLFieldDefinition objectFieldDef) {
6153
String interfaceFieldDefStr = getUnwrappedTypeName(interfaceFieldDef.getType());
6254
String objectFieldDefStr = getUnwrappedTypeName(objectFieldDef.getType());
6355

64-
if (!interfaceFieldDefStr.equals(objectFieldDefStr)) {
56+
if (!isCompatible(interfaceFieldDef.getType(), objectFieldDef.getType())) {
6557
validationErrorCollector.addError(
6658
error(format("object type '%s' does not implement interface '%s' because field '%s' is defined as '%s' type and not as '%s' type",
67-
objectTyoe.getName(), interfaceType.getName(), interfaceFieldDef.getName(), objectFieldDefStr, interfaceFieldDefStr)));
59+
objectType.getName(), interfaceType.getName(), interfaceFieldDef.getName(), objectFieldDefStr, interfaceFieldDefStr)));
6860
} else {
69-
checkFieldArgumentEquivalence(objectTyoe, interfaceType, validationErrorCollector, interfaceFieldDef, objectFieldDef);
61+
checkFieldArgumentEquivalence(objectType, interfaceType, validationErrorCollector, interfaceFieldDef, objectFieldDef);
7062
}
7163
}
7264

@@ -114,4 +106,37 @@ private SchemaValidationError error(String msg) {
114106
return new SchemaValidationError(ObjectDoesNotImplementItsInterfaces, msg);
115107
}
116108

109+
boolean isCompatible(GraphQLOutputType a, GraphQLOutputType b) {
110+
if (isSameType(a, b)) {
111+
return true;
112+
} else if (a instanceof GraphQLUnionType) {
113+
return objectIsMemberOfUnion((GraphQLUnionType) a, b);
114+
} else if (a instanceof GraphQLInterfaceType && b instanceof GraphQLObjectType) {
115+
return objectImplementsInterface((GraphQLInterfaceType) a, (GraphQLObjectType) b);
116+
} else if (a instanceof GraphQLList && b instanceof GraphQLList) {
117+
GraphQLOutputType wrappedA = (GraphQLOutputType) ((GraphQLList) a).getWrappedType();
118+
GraphQLOutputType wrappedB = (GraphQLOutputType) ((GraphQLList) b).getWrappedType();
119+
return isCompatible(wrappedA, wrappedB);
120+
} else if (b instanceof GraphQLNonNull) {
121+
GraphQLOutputType wrappedB = (GraphQLOutputType) ((GraphQLNonNull) b).getWrappedType();
122+
return isCompatible(a, wrappedB);
123+
} else {
124+
return false;
125+
}
126+
}
127+
128+
boolean isSameType(GraphQLOutputType a, GraphQLOutputType b) {
129+
String aDefString = getUnwrappedTypeName(a);
130+
String bDefString = getUnwrappedTypeName(b);
131+
return aDefString.equals(bDefString);
132+
}
133+
134+
boolean objectImplementsInterface(GraphQLInterfaceType interfaceType, GraphQLObjectType objectType) {
135+
return objectType.getInterfaces().contains(interfaceType);
136+
}
137+
138+
boolean objectIsMemberOfUnion(GraphQLUnionType unionType, GraphQLOutputType objectType) {
139+
return unionType.getTypes().contains(objectType);
140+
}
141+
117142
}

‎src/test/groovy/graphql/schema/validation/ObjectsImplementInterfacesTest.groovy‎

Lines changed: 194 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package graphql.schema.validation
22

33
import graphql.TypeResolutionEnvironment
44
import graphql.schema.GraphQLInterfaceType
5+
import graphql.schema.GraphQLList
6+
import graphql.schema.GraphQLNonNull
57
import graphql.schema.GraphQLObjectType
68
import graphql.schema.TypeResolver
79
import spock.lang.Specification
@@ -13,6 +15,9 @@ import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition
1315
import static graphql.schema.GraphQLInterfaceType.newInterface
1416
import static graphql.schema.GraphQLList.list
1517
import 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

1722
class 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

Comments
 (0)