Skip to content

Commit 8e29e85

Browse files
committed
change(validation): see graphql/graphql-js#229 for details
1 parent f264a51 commit 8e29e85

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/main/java/graphql/validation/rules/OverlappingFieldsCanBeMerged.java

+17-7
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33

44
import graphql.execution.TypeFromAST;
55
import graphql.language.*;
6-
import graphql.schema.GraphQLFieldDefinition;
7-
import graphql.schema.GraphQLFieldsContainer;
8-
import graphql.schema.GraphQLOutputType;
9-
import graphql.schema.GraphQLType;
6+
import graphql.schema.*;
107
import graphql.validation.AbstractRule;
118
import graphql.validation.ErrorFactory;
129
import graphql.validation.ValidationContext;
@@ -86,7 +83,18 @@ private Conflict findConflict(String responseName, FieldAndType fieldAndType1, F
8683
}
8784
alreadyChecked.add(new FieldPair(field1, field2));
8885

89-
// System.out.println(responseName + " -> " + fieldName1 + " - " + fieldName2);
86+
// If the statically known parent types could not possibly apply at the same
87+
// time, then it is safe to permit them to diverge as they will not present
88+
// any ambiguity by differing.
89+
// It is known that two parent types could never overlap if they are
90+
// different Object types. Interface or Union types might overlap - if not
91+
// in the current state of the schema, then perhaps in some future version,
92+
// thus may not safely diverge.
93+
if (!sameType(fieldAndType1.parentType, fieldAndType1.parentType) &&
94+
fieldAndType1.parentType instanceof GraphQLObjectType &&
95+
fieldAndType2.parentType instanceof GraphQLObjectType) {
96+
return null;
97+
}
9098

9199
if (!fieldName1.equals(fieldName2)) {
92100
String reason = String.format("%s: %s and %s are different fields", responseName, fieldName1, fieldName2);
@@ -248,7 +256,7 @@ private void collectFieldsForField(Map<String, List<FieldAndType>> fieldMap, Gra
248256
GraphQLFieldDefinition fieldDefinition = fieldsContainer.getFieldDefinition(field.getName());
249257
fieldType = fieldDefinition != null ? fieldDefinition.getType() : null;
250258
}
251-
fieldMap.get(responseName).add(new FieldAndType(field, fieldType));
259+
fieldMap.get(responseName).add(new FieldAndType(field, fieldType, parentType));
252260
}
253261

254262
private static class FieldPair {
@@ -284,12 +292,14 @@ public Conflict(String responseName, String reason, List<Field> fields) {
284292

285293

286294
private static class FieldAndType {
287-
public FieldAndType(Field field, GraphQLType graphQLType) {
295+
public FieldAndType(Field field, GraphQLType graphQLType, GraphQLType parentType) {
288296
this.field = field;
289297
this.graphQLType = graphQLType;
298+
this.parentType = parentType;
290299
}
291300

292301
Field field;
293302
GraphQLType graphQLType;
303+
GraphQLType parentType;
294304
}
295305
}

0 commit comments

Comments
 (0)