|
3 | 3 |
|
4 | 4 | import graphql.execution.TypeFromAST;
|
5 | 5 | 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.*; |
10 | 7 | import graphql.validation.AbstractRule;
|
11 | 8 | import graphql.validation.ErrorFactory;
|
12 | 9 | import graphql.validation.ValidationContext;
|
@@ -86,7 +83,18 @@ private Conflict findConflict(String responseName, FieldAndType fieldAndType1, F
|
86 | 83 | }
|
87 | 84 | alreadyChecked.add(new FieldPair(field1, field2));
|
88 | 85 |
|
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 | + } |
90 | 98 |
|
91 | 99 | if (!fieldName1.equals(fieldName2)) {
|
92 | 100 | 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
|
248 | 256 | GraphQLFieldDefinition fieldDefinition = fieldsContainer.getFieldDefinition(field.getName());
|
249 | 257 | fieldType = fieldDefinition != null ? fieldDefinition.getType() : null;
|
250 | 258 | }
|
251 |
| - fieldMap.get(responseName).add(new FieldAndType(field, fieldType)); |
| 259 | + fieldMap.get(responseName).add(new FieldAndType(field, fieldType, parentType)); |
252 | 260 | }
|
253 | 261 |
|
254 | 262 | private static class FieldPair {
|
@@ -284,12 +292,14 @@ public Conflict(String responseName, String reason, List<Field> fields) {
|
284 | 292 |
|
285 | 293 |
|
286 | 294 | private static class FieldAndType {
|
287 |
| - public FieldAndType(Field field, GraphQLType graphQLType) { |
| 295 | + public FieldAndType(Field field, GraphQLType graphQLType, GraphQLType parentType) { |
288 | 296 | this.field = field;
|
289 | 297 | this.graphQLType = graphQLType;
|
| 298 | + this.parentType = parentType; |
290 | 299 | }
|
291 | 300 |
|
292 | 301 | Field field;
|
293 | 302 | GraphQLType graphQLType;
|
| 303 | + GraphQLType parentType; |
294 | 304 | }
|
295 | 305 | }
|
0 commit comments