@@ -325,8 +325,6 @@ public class OperationValidator implements DocumentVisitor {
325325 private @ Nullable Map <String , VariableDefinition > variableDefinitionMap ;
326326
327327 // --- State: OverlappingFieldsCanBeMerged ---
328- private final Set <Set <FieldAndType >> sameResponseShapeChecked = new LinkedHashSet <>();
329- private final Set <Set <FieldAndType >> sameForCommonParentsChecked = new LinkedHashSet <>();
330328 private final Set <Set <Field >> conflictsReported = new LinkedHashSet <>();
331329
332330 // --- State: LoneAnonymousOperation ---
@@ -355,24 +353,25 @@ public class OperationValidator implements DocumentVisitor {
355353 // Max depth seen during current fragment traversal (for calculating fragment's internal depth)
356354 private int fragmentTraversalMaxDepth = 0 ;
357355
358- // --- State: Good Faith Introspection ---
359- // These caches are scoped to one operation and are null when Good Faith counting is inactive .
356+ // --- State: Merged selections and Good Faith Introspection ---
357+ // Caches merge-check progress and merged child selections for the whole document .
360358 // All FieldAndType entries in a key set have the same response name
361359 // (the alias if present, otherwise the field name).
362360 // FieldAndType is identified only by the AST node.
363-
364- // Caches merged child selections, grouped by response name, for each set of collected fields .
365- // Shares field-collection work between merge-validation traversals .
361+ // Child selections depend on the document and schema, not the current operation.
362+ // They are grouped by response name and shared between merge-validation traversals .
363+ // Leaf-only sets share an empty map .
366364 // Example: `fields { a: name } fields { b: name }` uses the two `fields` AST nodes as the key
367365 // and caches their combined children under response names `a` and `b`, both selecting `name`.
368- private @ Nullable Map <Set <FieldAndType >, Map < String , Set < FieldAndType >>> mergedIntrospectionSelections ;
366+ private final Map <Set <FieldAndType >, FieldValidationState < FieldAndType >> fieldValidationStates = new HashMap <>() ;
369367
370368 // Caches field count, relative depth and restricted coordinates for each common-parent validation set.
371369 // These are sets of fields checked together for matching field names and arguments.
372370 // Example: `fields { name } fields { description }` on __Type caches a count of 3 fields,
373371 // a relative depth of 2, and one occurrence of the restricted coordinate __Type.fields.
374372 // Cached summaries contribute again at each response position where the set occurs.
375373 // A present key with a null value marks counting in progress, preventing recursion through fragment cycles.
374+ // This cache is scoped to one operation and is null when Good Faith counting is inactive.
376375 private @ Nullable Map <Set <FieldAndType >, @ Nullable GoodFaithIntrospectionCounts > introspectionCounts ;
377376 private boolean introspectionQueryDetected = false ;
378377
@@ -1094,7 +1093,6 @@ private void leaveOperationDefinition() {
10941093 maxFieldDepthSeen = 0 ;
10951094 fragmentTraversalMaxDepth = 0 ;
10961095 introspectionQueryDetected = false ;
1097- mergedIntrospectionSelections = null ;
10981096 introspectionCounts = null ;
10991097 }
11001098
@@ -1468,7 +1466,6 @@ private void overlappingFieldsImpl(SelectionSet selectionSet, @Nullable GraphQLO
14681466 overlappingFields_collectFields (fieldMap , selectionSet , graphQLOutputType , visitedFragments );
14691467 if (isRuleEnabled (OperationValidationRule .GOOD_FAITH_INTROSPECTION ) && containsIntrospection (fieldMap , graphQLOutputType )) {
14701468 introspectionQueryDetected = true ;
1471- mergedIntrospectionSelections = new HashMap <>();
14721469 introspectionCounts = new HashMap <>();
14731470 }
14741471 List <Conflict > conflicts = findConflicts (fieldMap );
@@ -1545,37 +1542,44 @@ private List<Conflict> findConflicts(Map<String, Set<FieldAndType>> fieldMap) {
15451542
15461543 private void sameResponseShapeByName (Map <String , Set <FieldAndType >> fieldMap , ImmutableList <String > currentPath , List <Conflict > conflictsResult ) {
15471544 for (Map .Entry <String , Set <FieldAndType >> entry : fieldMap .entrySet ()) {
1548- if (sameResponseShapeChecked .contains (entry .getValue ())) {
1545+ FieldValidationState <FieldAndType > state = fieldValidationStates .computeIfAbsent (entry .getValue (), fields -> new FieldValidationState <>());
1546+ if (!state .markResponseShapeChecked ()) {
15491547 continue ;
15501548 }
15511549 ImmutableList <String > newPath = addToList (currentPath , entry .getKey ());
1552- sameResponseShapeChecked .add (entry .getValue ());
15531550 Conflict conflict = requireSameOutputTypeShape (newPath , entry .getValue ());
15541551 if (conflict != null ) {
15551552 conflictsResult .add (conflict );
15561553 continue ;
15571554 }
1558- Map <String , Set <FieldAndType >> subSelections = mergeSubSelections (entry .getValue ());
1555+ Map <String , Set <FieldAndType >> subSelections = mergeSubSelections (entry .getValue (), state );
15591556 sameResponseShapeByName (subSelections , newPath , conflictsResult );
15601557 }
15611558 }
15621559
1563- private Map <String , Set <FieldAndType >> mergeSubSelections (Set <FieldAndType > sameNameFields ) {
1564- if (mergedIntrospectionSelections == null ) {
1565- return collectSubSelections (sameNameFields );
1560+ private Map <String , Set <FieldAndType >> mergeSubSelections (Set <FieldAndType > sameNameFields , FieldValidationState <FieldAndType > state ) {
1561+ Map <String , Set <FieldAndType >> subSelections = state .getMergedSubSelections ();
1562+ if (subSelections == null ) {
1563+ subSelections = collectSubSelections (sameNameFields );
1564+ state .setMergedSubSelections (subSelections );
15661565 }
1567- return mergedIntrospectionSelections . computeIfAbsent ( sameNameFields , this :: collectSubSelections ) ;
1566+ return subSelections ;
15681567 }
15691568
15701569 private Map <String , Set <FieldAndType >> collectSubSelections (Set <FieldAndType > sameNameFields ) {
1571- Map <String , Set <FieldAndType >> fieldMap = new LinkedHashMap <>() ;
1570+ Map <String , Set <FieldAndType >> fieldMap = null ;
15721571 for (FieldAndType fieldAndType : sameNameFields ) {
1573- if (fieldAndType .field .getSelectionSet () != null ) {
1574- Set <String > visitedFragments = new LinkedHashSet <>();
1575- overlappingFields_collectFields (fieldMap , fieldAndType .field .getSelectionSet (), fieldAndType .graphQLType , visitedFragments );
1572+ SelectionSet selectionSet = fieldAndType .field .getSelectionSet ();
1573+ if (selectionSet == null ) {
1574+ continue ;
1575+ }
1576+ if (fieldMap == null ) {
1577+ fieldMap = new LinkedHashMap <>();
15761578 }
1579+ Set <String > visitedFragments = new LinkedHashSet <>();
1580+ overlappingFields_collectFields (fieldMap , selectionSet , fieldAndType .graphQLType , visitedFragments );
15771581 }
1578- return fieldMap ;
1582+ return fieldMap == null ? Collections . emptyMap () : fieldMap ;
15791583 }
15801584
15811585 private @ Nullable GoodFaithIntrospectionCounts sameForCommonParentsByName (Map <String , Set <FieldAndType >> fieldMap ,
@@ -1596,7 +1600,8 @@ private void sameForCommonParentGroup(Set<FieldAndType> group, ImmutableList<Str
15961600 if (selectionCounts != null && reuseIntrospectionCounts (group , path .size () - 1 , selectionCounts )) {
15971601 return ;
15981602 }
1599- boolean newlyChecked = sameForCommonParentsChecked .add (group );
1603+ FieldValidationState <FieldAndType > state = fieldValidationStates .computeIfAbsent (group , fields -> new FieldValidationState <>());
1604+ boolean newlyChecked = state .markSameForCommonParentsChecked ();
16001605 if (!newlyChecked && selectionCounts == null ) {
16011606 return ;
16021607 }
@@ -1614,7 +1619,7 @@ private void sameForCommonParentGroup(Set<FieldAndType> group, ImmutableList<Str
16141619 // A null value marks an in-progress group, stopping invalid fragment cycles.
16151620 Assert .assertNotNull (introspectionCounts ).put (group , null );
16161621 }
1617- Map <String , Set <FieldAndType >> subSelections = mergeSubSelections (group );
1622+ Map <String , Set <FieldAndType >> subSelections = mergeSubSelections (group , state );
16181623 GoodFaithIntrospectionCounts children = sameForCommonParentsByName (subSelections , path , conflictsResult );
16191624 if (groupCounts != null ) {
16201625 groupCounts .addChildren (Assert .assertNotNull (children ));
0 commit comments