From 003eac17a66365c7d3306a9ba65c15605cc65b7d Mon Sep 17 00:00:00 2001 From: Nabiha Date: Wed, 4 Nov 2026 17:19:34 +0530 Subject: [PATCH] visit fragment fields on every spread in QueryTraverser --- .../analysis/NodeVisitorWithTypeTracking.java | 11 ++++ ...xQueryComplexityInstrumentationTest.groovy | 27 +++++++++ .../MaxQueryDepthInstrumentationTest.groovy | 27 +++++++++ .../analysis/QueryTraverserTest.groovy | 60 +++++++++++++++++++ 4 files changed, 125 insertions(+) diff --git a/src/main/java/graphql/analysis/NodeVisitorWithTypeTracking.java b/src/main/java/graphql/analysis/NodeVisitorWithTypeTracking.java index c2f0bebd3f..e085ec15cd 100644 --- a/src/main/java/graphql/analysis/NodeVisitorWithTypeTracking.java +++ b/src/main/java/graphql/analysis/NodeVisitorWithTypeTracking.java @@ -33,6 +33,7 @@ import java.util.Collections; import java.util.Locale; import java.util.Map; +import java.util.Set; import static graphql.Assert.assertNotNull; import static graphql.schema.GraphQLTypeUtil.unwrapAll; @@ -116,6 +117,9 @@ public TraversalControl visitFragmentDefinition(FragmentDefinition fragmentDefin if (context.getPhase() == LEAVE) { postOrderCallback.visitFragmentDefinition(fragmentEnvironment); + // the traverser enters a node only once, so forget the fragment once it has been left and a later + // spread of it is followed again. a spread of the fragment inside itself still finds it visited. + forgetVisited(context.visitedNodes(), fragmentDefinition); return TraversalControl.CONTINUE; } preOrderCallback.visitFragmentDefinition(fragmentEnvironment); @@ -125,6 +129,13 @@ public TraversalControl visitFragmentDefinition(FragmentDefinition fragmentDefin return TraversalControl.CONTINUE; } + private static void forgetVisited(Set visitedNodes, Node node) { + visitedNodes.remove(node); + for (Node child : node.getChildren()) { + forgetVisited(visitedNodes, child); + } + } + @Override public TraversalControl visitFragmentSpread(FragmentSpread fragmentSpread, TraverserContext context) { QueryTraversalContext parentEnv = context.getVarFromParents(QueryTraversalContext.class); diff --git a/src/test/groovy/graphql/analysis/MaxQueryComplexityInstrumentationTest.groovy b/src/test/groovy/graphql/analysis/MaxQueryComplexityInstrumentationTest.groovy index 2aa4369ef4..b8113f497f 100644 --- a/src/test/groovy/graphql/analysis/MaxQueryComplexityInstrumentationTest.groovy +++ b/src/test/groovy/graphql/analysis/MaxQueryComplexityInstrumentationTest.groovy @@ -52,6 +52,33 @@ class MaxQueryComplexityInstrumentationTest extends Specification { } + def "fields of a fragment count for every spread of it"() { + given: + def schema = TestUtil.schema(""" + type Query{ + foo: Foo + bar: String + } + type Foo { + scalar: String + foo: Foo + } + """) + def query = createQuery(""" + {f1: foo {...F} f2: foo {...F} } + fragment F on Foo {scalar foo {scalar}} + """) + MaxQueryComplexityInstrumentation queryComplexityInstrumentation = new MaxQueryComplexityInstrumentation(7) + ExecutionInput executionInput = Mock(ExecutionInput) + def state = createInstrumentationState(queryComplexityInstrumentation) + InstrumentationExecuteOperationParameters executeOperationParameters = createExecuteOperationParameters(queryComplexityInstrumentation, executionInput, query, schema, state) + when: + queryComplexityInstrumentation.beginExecuteOperation(executeOperationParameters, state) + then: + def e = thrown(AbortExecutionException) + e.message == "maximum query complexity exceeded 8 > 7" + } + def "complexity calculator works with __typename field with score 0"() { given: def schema = TestUtil.schema(""" diff --git a/src/test/groovy/graphql/analysis/MaxQueryDepthInstrumentationTest.groovy b/src/test/groovy/graphql/analysis/MaxQueryDepthInstrumentationTest.groovy index acc1a08983..88578a0740 100644 --- a/src/test/groovy/graphql/analysis/MaxQueryDepthInstrumentationTest.groovy +++ b/src/test/groovy/graphql/analysis/MaxQueryDepthInstrumentationTest.groovy @@ -48,6 +48,33 @@ class MaxQueryDepthInstrumentationTest extends Specification { e.message.contains("maximum query depth exceeded 7 > 6") } + def "throws exception if too deep through a fragment that was spread before"() { + given: + def schema = TestUtil.schema(""" + type Query{ + foo: Foo + bar: String + } + type Foo { + scalar: String + foo: Foo + } + """) + def query = createQuery(""" + {f1: foo {...F} f2: foo {foo {foo {foo {...F}}}} } + fragment F on Foo {foo {foo {scalar}}} + """) + MaxQueryDepthInstrumentation maximumQueryDepthInstrumentation = new MaxQueryDepthInstrumentation(6) + ExecutionInput executionInput = Mock(ExecutionInput) + def executionContext = executionCtx(executionInput, query, schema) + def executeOperationParameters = new InstrumentationExecuteOperationParameters(executionContext) + when: + maximumQueryDepthInstrumentation.beginExecuteOperation(executeOperationParameters, null) + then: + def e = thrown(AbortExecutionException) + e.message.contains("maximum query depth exceeded 7 > 6") + } + def "doesn't throw exception if not deep enough"() { given: def schema = TestUtil.schema(""" diff --git a/src/test/groovy/graphql/analysis/QueryTraverserTest.groovy b/src/test/groovy/graphql/analysis/QueryTraverserTest.groovy index 15cf09faec..40ea61a8e6 100644 --- a/src/test/groovy/graphql/analysis/QueryTraverserTest.groovy +++ b/src/test/groovy/graphql/analysis/QueryTraverserTest.groovy @@ -429,6 +429,66 @@ class QueryTraverserTest extends Specification { } + def "fields of a fragment are visited for every spread of it"() { + given: + def schema = TestUtil.schema(""" + type Query{ + foo: Foo + } + type Foo { + subFoo: String + foo: Foo + } + """) + def visitor = mockQueryVisitor() + def query = createQuery(""" + { + a: foo { ...F } + b: foo { foo { ...F } } + } + fragment F on Foo { + subFoo + } + """) + QueryTraverser queryTraversal = createQueryTraversal(query, schema) + when: + queryTraversal.visitPreOrder(visitor) + + then: + 1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl env -> env.field.name == "subFoo" && env.parentEnvironment.field.alias == "a" }) + 1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl env -> env.field.name == "subFoo" && env.parentEnvironment.parentEnvironment.field.alias == "b" }) + } + + def "a fragment spread inside the same fragment is not followed again"() { + given: + def schema = TestUtil.schema(""" + type Query{ + foo: Foo + } + type Foo { + subFoo: String + foo: Foo + } + """) + def visitor = mockQueryVisitor() + def query = createQuery(""" + { + foo { ...F } + } + fragment F on Foo { + subFoo + foo { ...F } + } + """) + QueryTraverser queryTraversal = createQueryTraversal(query, schema) + when: + queryTraversal.visitPreOrder(visitor) + + then: + 1 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl env -> env.field.name == "subFoo" }) + 2 * visitor.visitField({ QueryVisitorFieldEnvironmentImpl env -> env.field.name == "foo" }) + } + def "test preOrder and postOrder order for fragment definitions and raw variables"() { given: