22
33
44import graphql .execution .TypeFromAST ;
5- import graphql .language .*;
6- import graphql .schema .*;
5+ import graphql .language .Argument ;
6+ import graphql .language .AstComparator ;
7+ import graphql .language .Field ;
8+ import graphql .language .FragmentDefinition ;
9+ import graphql .language .FragmentSpread ;
10+ import graphql .language .InlineFragment ;
11+ import graphql .language .Selection ;
12+ import graphql .language .SelectionSet ;
13+ import graphql .language .Value ;
14+ import graphql .schema .GraphQLFieldDefinition ;
15+ import graphql .schema .GraphQLFieldsContainer ;
16+ import graphql .schema .GraphQLObjectType ;
17+ import graphql .schema .GraphQLOutputType ;
18+ import graphql .schema .GraphQLType ;
719import graphql .validation .AbstractRule ;
820import graphql .validation .ErrorFactory ;
921import graphql .validation .ValidationContext ;
1022import graphql .validation .ValidationErrorCollector ;
1123
12- import java .util .*;
24+ import java .util .ArrayList ;
25+ import java .util .LinkedHashMap ;
26+ import java .util .LinkedHashSet ;
27+ import java .util .List ;
28+ import java .util .Map ;
29+ import java .util .Set ;
1330
14- import static graphql .language .NodeUtil .directivesByName ;
1531import static graphql .validation .ValidationErrorType .FieldsConflict ;
1632
1733public class OverlappingFieldsCanBeMerged extends AbstractRule {
@@ -66,6 +82,7 @@ private boolean isAlreadyChecked(Field field1, Field field2) {
6682 return false ;
6783 }
6884
85+ @ SuppressWarnings ("ConstantConditions" )
6986 private Conflict findConflict (String responseName , FieldAndType fieldAndType1 , FieldAndType fieldAndType2 ) {
7087
7188 Field field1 = fieldAndType1 .field ;
@@ -114,10 +131,6 @@ private Conflict findConflict(String responseName, FieldAndType fieldAndType1, F
114131 String reason = String .format ("%s: they have differing arguments" , responseName );
115132 return new Conflict (responseName , reason , field1 , field2 );
116133 }
117- if (!sameDirectives (field1 .getDirectives (), field2 .getDirectives ())) {
118- String reason = String .format ("%s: they have differing directives" , responseName );
119- return new Conflict (responseName , reason , field1 , field2 );
120- }
121134 SelectionSet selectionSet1 = field1 .getSelectionSet ();
122135 SelectionSet selectionSet2 = field2 .getSelectionSet ();
123136 if (selectionSet1 != null && selectionSet2 != null ) {
@@ -160,11 +173,13 @@ private String joinReasons(List<Conflict> conflicts) {
160173 return result .toString ();
161174 }
162175
176+ @ SuppressWarnings ("SimplifiableIfStatement" )
163177 private boolean sameType (GraphQLType type1 , GraphQLType type2 ) {
164178 if (type1 == null || type2 == null ) return true ;
165179 return type1 .equals (type2 );
166180 }
167181
182+ @ SuppressWarnings ("SimplifiableIfStatement" )
168183 private boolean sameValue (Value value1 , Value value2 ) {
169184 if (value1 == null && value2 == null ) return true ;
170185 if (value1 == null ) return false ;
@@ -189,21 +204,6 @@ private Argument findArgumentByName(String name, List<Argument> arguments) {
189204 return null ;
190205 }
191206
192- private boolean sameDirectives (List <Directive > directives1 , List <Directive > directives2 ) {
193- if (directives1 .size () != directives2 .size ()) return false ;
194- for (Directive directive : directives1 ) {
195- Directive matchedDirective = getDirectiveByName (directive .getName (), directives2 );
196- if (matchedDirective == null ) return false ;
197- if (!sameArguments (directive .getArguments (), matchedDirective .getArguments ())) return false ;
198- }
199- return true ;
200- }
201-
202- private Directive getDirectiveByName (String name , List <Directive > directives ) {
203- return directivesByName (directives ).get (name );
204- }
205-
206-
207207 private void collectFields (Map <String , List <FieldAndType >> fieldMap , SelectionSet selectionSet , GraphQLType parentType , Set <String > visitedFragmentSpreads ) {
208208
209209 for (Selection selection : selectionSet .getSelections ()) {
@@ -220,8 +220,7 @@ private void collectFields(Map<String, List<FieldAndType>> fieldMap, SelectionSe
220220
221221 }
222222
223- private void collectFieldsForFragmentSpread (Map <String , List <FieldAndType >> fieldMap , Set <String > visitedFragmentSpreads , FragmentSpread selection ) {
224- FragmentSpread fragmentSpread = selection ;
223+ private void collectFieldsForFragmentSpread (Map <String , List <FieldAndType >> fieldMap , Set <String > visitedFragmentSpreads , FragmentSpread fragmentSpread ) {
225224 FragmentDefinition fragment = getValidationContext ().getFragment (fragmentSpread .getName ());
226225 if (fragment == null ) return ;
227226 if (visitedFragmentSpreads .contains (fragment .getName ())) {
@@ -233,16 +232,14 @@ private void collectFieldsForFragmentSpread(Map<String, List<FieldAndType>> fiel
233232 collectFields (fieldMap , fragment .getSelectionSet (), graphQLType , visitedFragmentSpreads );
234233 }
235234
236- private void collectFieldsForInlineFragment (Map <String , List <FieldAndType >> fieldMap , Set <String > visitedFragmentSpreads , GraphQLType parentType , InlineFragment selection ) {
237- InlineFragment inlineFragment = selection ;
235+ private void collectFieldsForInlineFragment (Map <String , List <FieldAndType >> fieldMap , Set <String > visitedFragmentSpreads , GraphQLType parentType , InlineFragment inlineFragment ) {
238236 GraphQLType graphQLType = inlineFragment .getTypeCondition () != null
239237 ? (GraphQLOutputType ) TypeFromAST .getTypeFromAST (getValidationContext ().getSchema (), inlineFragment .getTypeCondition ())
240238 : parentType ;
241239 collectFields (fieldMap , inlineFragment .getSelectionSet (), graphQLType , visitedFragmentSpreads );
242240 }
243241
244- private void collectFieldsForField (Map <String , List <FieldAndType >> fieldMap , GraphQLType parentType , Field selection ) {
245- Field field = selection ;
242+ private void collectFieldsForField (Map <String , List <FieldAndType >> fieldMap , GraphQLType parentType , Field field ) {
246243 String responseName = field .getAlias () != null ? field .getAlias () : field .getName ();
247244 if (!fieldMap .containsKey (responseName )) {
248245 fieldMap .put (responseName , new ArrayList <>());
@@ -257,7 +254,7 @@ private void collectFieldsForField(Map<String, List<FieldAndType>> fieldMap, Gra
257254 }
258255
259256 private GraphQLFieldDefinition getVisibleFieldDefinition (GraphQLFieldsContainer fieldsContainer , Field field ) {
260- return getValidationContext ().getSchema ().getFieldVisibility ().getFieldDefinition (fieldsContainer ,field .getName ());
257+ return getValidationContext ().getSchema ().getFieldVisibility ().getFieldDefinition (fieldsContainer , field .getName ());
261258 }
262259
263260 private static class FieldPair {
0 commit comments