|
1 | 1 | package graphql.validation.rules; |
2 | 2 |
|
3 | 3 |
|
| 4 | +import graphql.execution.TypeFromAST; |
| 5 | +import graphql.language.*; |
| 6 | +import graphql.schema.GraphQLFieldDefinition; |
| 7 | +import graphql.schema.GraphQLFieldsContainer; |
| 8 | +import graphql.schema.GraphQLOutputType; |
| 9 | +import graphql.schema.GraphQLType; |
4 | 10 | import graphql.validation.AbstractRule; |
5 | 11 | import graphql.validation.ValidationContext; |
6 | 12 | import graphql.validation.ValidationErrorCollector; |
7 | 13 |
|
8 | | -public class OverlappingFieldsCanBeMerged extends AbstractRule{ |
| 14 | +import java.util.*; |
| 15 | + |
| 16 | +public class OverlappingFieldsCanBeMerged extends AbstractRule { |
| 17 | + |
9 | 18 |
|
10 | | - // TODO |
11 | 19 | public OverlappingFieldsCanBeMerged(ValidationContext validationContext, ValidationErrorCollector validationErrorCollector) { |
12 | 20 | super(validationContext, validationErrorCollector); |
13 | 21 | } |
| 22 | + |
| 23 | + @Override |
| 24 | + public void checkSelectionSet(SelectionSet selectionSet) { |
| 25 | + Map<String, List<FieldAndType>> fieldMap = new LinkedHashMap<>(); |
| 26 | + Set<String> visitedFragmentSpreads = new LinkedHashSet<>(); |
| 27 | + collectFields(fieldMap, selectionSet, getValidationContext().getOutputType(), visitedFragmentSpreads); |
| 28 | + List<Conflict> conflicts = findConflicts(fieldMap); |
| 29 | + for (Conflict conflict : conflicts) { |
| 30 | +// addError(new ValidationError(ValidationErrorType.FieldsConflict)); |
| 31 | + } |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + private List<Conflict> findConflicts(Map<String, List<FieldAndType>> fieldMap) { |
| 36 | + List<Conflict> result = new ArrayList<>(); |
| 37 | + for (String name : fieldMap.keySet()) { |
| 38 | + List<FieldAndType> fieldAndTypes = fieldMap.get(name); |
| 39 | + for (int i = 0; i < fieldAndTypes.size(); i++) { |
| 40 | + for (int j = i + i; j < fieldAndTypes.size(); j++) { |
| 41 | + Conflict conflict = findConflict(name, fieldAndTypes.get(i), fieldAndTypes.get(j)); |
| 42 | + if (conflict != null) { |
| 43 | + result.add(conflict); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + return result; |
| 49 | + } |
| 50 | + |
| 51 | + private Conflict findConflict(String responseName, FieldAndType fieldAndType1, FieldAndType fieldAndType2) { |
| 52 | + |
| 53 | + Field field1 = fieldAndType1.field; |
| 54 | + Field field2 = fieldAndType2.field; |
| 55 | + |
| 56 | + String fieldName1 = field1.getName(); |
| 57 | + String fieldName2 = field2.getName(); |
| 58 | + if (!fieldName1.equals(fieldName2)) { |
| 59 | + String reason = String.format("%s and %s are different fields", fieldName1, fieldName2); |
| 60 | + return new Conflict(responseName, reason, field1, field2); |
| 61 | + } |
| 62 | + return null; |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + private void collectFields(Map<String, List<FieldAndType>> fieldMap, SelectionSet selectionSet, GraphQLOutputType parentType, Set<String> visitedFragmentSpreads) { |
| 68 | + |
| 69 | + for (Selection selection : selectionSet.getSelections()) { |
| 70 | + if (selection instanceof Field) { |
| 71 | + Field field = (Field) selection; |
| 72 | + String responseName = field.getAlias(); |
| 73 | + if (!fieldMap.containsKey(responseName)) { |
| 74 | + fieldMap.put(responseName, new ArrayList<FieldAndType>()); |
| 75 | + } |
| 76 | + GraphQLOutputType fieldType = null; |
| 77 | + if(parentType instanceof GraphQLFieldsContainer) { |
| 78 | + GraphQLFieldsContainer fieldsContainer = (GraphQLFieldsContainer) parentType; |
| 79 | + GraphQLFieldDefinition fieldDefinition = fieldsContainer.getFieldDefinition(((Field) selection).getName()); |
| 80 | + fieldType = fieldDefinition != null ? fieldDefinition.getType() : null; |
| 81 | + } |
| 82 | + fieldMap.get(responseName).add(new FieldAndType(field, fieldType)); |
| 83 | + |
| 84 | + } else if (selection instanceof InlineFragment) { |
| 85 | + InlineFragment inlineFragment = (InlineFragment) selection; |
| 86 | + GraphQLOutputType graphQLType = (GraphQLOutputType) TypeFromAST.getTypeFromAST(getValidationContext().getSchema(), |
| 87 | + inlineFragment.getTypeCondition()); |
| 88 | + collectFields(fieldMap, inlineFragment.getSelectionSet(), graphQLType, visitedFragmentSpreads); |
| 89 | + |
| 90 | + } else if (selection instanceof FragmentSpread) { |
| 91 | + FragmentSpread fragmentSpread = (FragmentSpread) selection; |
| 92 | + FragmentDefinition fragment = getValidationContext().getFragment(fragmentSpread.getName()); |
| 93 | + if (fragment == null) continue; |
| 94 | + if (visitedFragmentSpreads.contains(fragment.getName())) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + visitedFragmentSpreads.add(fragment.getName()); |
| 98 | + GraphQLOutputType graphQLType = (GraphQLOutputType) TypeFromAST.getTypeFromAST(getValidationContext().getSchema(), |
| 99 | + fragment.getTypeCondition()); |
| 100 | + collectFields(fieldMap, fragment.getSelectionSet(), graphQLType, visitedFragmentSpreads); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + private static class Conflict { |
| 107 | + String responseName; |
| 108 | + String reason; |
| 109 | + List<Field> fields = new ArrayList<>(); |
| 110 | + |
| 111 | + public Conflict(String responseName, String reason, Field field1, Field field2) { |
| 112 | + this.responseName = responseName; |
| 113 | + this.reason = reason; |
| 114 | + this.fields.add(field1); |
| 115 | + this.fields.add(field2); |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + private static class FieldAndType { |
| 122 | + public FieldAndType(Field field, GraphQLType graphQLType) { |
| 123 | + this.field = field; |
| 124 | + this.graphQLType = graphQLType; |
| 125 | + } |
| 126 | + |
| 127 | + Field field; |
| 128 | + GraphQLType graphQLType; |
| 129 | + } |
14 | 130 | } |
0 commit comments