33import graphql .Internal ;
44import graphql .language .Argument ;
55import graphql .language .AstComparator ;
6+ import graphql .schema .GraphQLNamedOutputType ;
7+ import graphql .schema .GraphQLObjectType ;
8+ import graphql .schema .GraphQLSchema ;
69
710import java .util .ArrayList ;
811import java .util .Iterator ;
1417@ Internal
1518public class ENFMerger {
1619
17- public static void merge (ExecutableNormalizedField parent , List <ExecutableNormalizedField > childrenWithSameResultKey ) {
20+ public static void merge (ExecutableNormalizedField parent , List <ExecutableNormalizedField > childrenWithSameResultKey , GraphQLSchema schema ) {
1821 // they have all the same result key
1922 // we can only merge the fields if they have the same field name + arguments + all children are the same
2023 List <Set <ExecutableNormalizedField >> possibleGroupsToMerge = new ArrayList <>();
@@ -23,7 +26,10 @@ public static void merge(ExecutableNormalizedField parent, List<ExecutableNormal
2326 overPossibleGroups :
2427 for (Set <ExecutableNormalizedField > group : possibleGroupsToMerge ) {
2528 for (ExecutableNormalizedField fieldInGroup : group ) {
26- if (field .getFieldName ().equals (fieldInGroup .getFieldName ()) && sameArguments (field .getAstArguments (), fieldInGroup .getAstArguments ())) {
29+ if (field .getFieldName ().equals (fieldInGroup .getFieldName ()) &&
30+ sameArguments (field .getAstArguments (), fieldInGroup .getAstArguments ())
31+ && isPartOfTheSameHierarchy (field , fieldInGroup , schema )
32+ ) {
2733 addToGroup = true ;
2834 group .add (field );
2935 continue overPossibleGroups ;
@@ -58,6 +64,18 @@ public static void merge(ExecutableNormalizedField parent, List<ExecutableNormal
5864 }
5965 }
6066
67+ private static boolean isPartOfTheSameHierarchy (ExecutableNormalizedField fieldOne , ExecutableNormalizedField fieldTwo , GraphQLSchema schema ) {
68+ // we can get away with only checking one of the object names, because all object names in one ENF are guaranteed to be part of
69+ // the same hierarchy
70+ String firstObject = fieldOne .getSingleObjectTypeName ();
71+ String secondObject = fieldTwo .getSingleObjectTypeName ();
72+ GraphQLObjectType objectTypeOne = schema .getObjectType (firstObject );
73+ GraphQLObjectType objectTypeTwo = schema .getObjectType (firstObject );
74+ List <GraphQLNamedOutputType > interfacesOne = objectTypeOne .getInterfaces ();
75+ List <GraphQLNamedOutputType > interfacesTwo = objectTypeTwo .getInterfaces ();
76+ return interfacesOne .stream ().anyMatch (interfacesTwo ::contains );
77+ }
78+
6179
6280 private static boolean areFieldSetsTheSame (List <Set <ExecutableNormalizedField >> listOfSets ) {
6381 if (listOfSets .size () == 0 || listOfSets .size () == 1 ) {
0 commit comments