@@ -13,6 +13,7 @@ import graphql.execution.ExecutionId
1313import graphql.execution.ExecutionIdProvider
1414import graphql.execution.ExecutionStrategyParameters
1515import graphql.execution.MissingRootTypeException
16+ import graphql.execution.ResultNodesInfo
1617import graphql.execution.SubscriptionExecutionStrategy
1718import graphql.execution.ValueUnboxer
1819import graphql.execution.instrumentation.Instrumentation
@@ -49,6 +50,7 @@ import static graphql.ExecutionInput.Builder
4950import static graphql.ExecutionInput.newExecutionInput
5051import static graphql.Scalars.GraphQLInt
5152import static graphql.Scalars.GraphQLString
53+ import static graphql.execution.ResultNodesInfo.MAX_RESULT_NODES
5254import static graphql.schema.GraphQLArgument.newArgument
5355import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition
5456import static graphql.schema.GraphQLInputObjectField.newInputObjectField
@@ -1427,4 +1429,143 @@ many lines''']
14271429 then:
14281430 !er.errors.isEmpty()
14291431 }
1432+
1433+ def "max result nodes not breached"() {
1434+ given:
1435+ def sdl = '''
1436+
1437+ type Query {
1438+ hello: String
1439+ }
1440+ '''
1441+ def df = { env -> "world" } as DataFetcher
1442+ def fetchers = ["Query": ["hello": df]]
1443+ def schema = TestUtil.schema(sdl, fetchers)
1444+ def graphQL = GraphQL.newGraphQL(schema).build()
1445+
1446+ def query = "{ hello h1: hello h2: hello h3: hello } "
1447+ def ei = newExecutionInput(query).build()
1448+ ei.getGraphQLContext().put(MAX_RESULT_NODES, 4);
1449+
1450+ when:
1451+ def er = graphQL.execute(ei)
1452+ def rni = ei.getGraphQLContext().get(ResultNodesInfo.RESULT_NODES_INFO) as ResultNodesInfo
1453+ then:
1454+ !rni.maxResultNodesExceeded
1455+ rni.resultNodesCount == 4
1456+ er.data == [hello: "world", h1: "world", h2: "world", h3: "world"]
1457+ }
1458+
1459+ def "max result nodes breached"() {
1460+ given:
1461+ def sdl = '''
1462+
1463+ type Query {
1464+ hello: String
1465+ }
1466+ '''
1467+ def df = { env -> "world" } as DataFetcher
1468+ def fetchers = ["Query": ["hello": df]]
1469+ def schema = TestUtil.schema(sdl, fetchers)
1470+ def graphQL = GraphQL.newGraphQL(schema).build()
1471+
1472+ def query = "{ hello h1: hello h2: hello h3: hello } "
1473+ def ei = newExecutionInput(query).build()
1474+ ei.getGraphQLContext().put(MAX_RESULT_NODES, 3);
1475+
1476+ when:
1477+ def er = graphQL.execute(ei)
1478+ def rni = ei.getGraphQLContext().get(ResultNodesInfo.RESULT_NODES_INFO) as ResultNodesInfo
1479+ then:
1480+ rni.maxResultNodesExceeded
1481+ rni.resultNodesCount == 4
1482+ er.data == [hello: "world", h1: "world", h2: "world", h3: null]
1483+ }
1484+
1485+ def "max result nodes breached with list"() {
1486+ given:
1487+ def sdl = '''
1488+
1489+ type Query {
1490+ hello: [String]
1491+ }
1492+ '''
1493+ def df = { env -> ["w1", "w2", "w3"] } as DataFetcher
1494+ def fetchers = ["Query": ["hello": df]]
1495+ def schema = TestUtil.schema(sdl, fetchers)
1496+ def graphQL = GraphQL.newGraphQL(schema).build()
1497+
1498+ def query = "{ hello}"
1499+ def ei = newExecutionInput(query).build()
1500+ ei.getGraphQLContext().put(MAX_RESULT_NODES, 3);
1501+
1502+ when:
1503+ def er = graphQL.execute(ei)
1504+ def rni = ei.getGraphQLContext().get(ResultNodesInfo.RESULT_NODES_INFO) as ResultNodesInfo
1505+ then:
1506+ rni.maxResultNodesExceeded
1507+ rni.resultNodesCount == 4
1508+ er.data == [hello: null]
1509+ }
1510+
1511+ def "max result nodes breached with list 2"() {
1512+ given:
1513+ def sdl = '''
1514+
1515+ type Query {
1516+ hello: [Foo]
1517+ }
1518+ type Foo {
1519+ name: String
1520+ }
1521+ '''
1522+ def df = { env -> [[name: "w1"], [name: "w2"], [name: "w3"]] } as DataFetcher
1523+ def fetchers = ["Query": ["hello": df]]
1524+ def schema = TestUtil.schema(sdl, fetchers)
1525+ def graphQL = GraphQL.newGraphQL(schema).build()
1526+
1527+ def query = "{ hello {name}}"
1528+ def ei = newExecutionInput(query).build()
1529+ // we have 7 result nodes overall
1530+ ei.getGraphQLContext().put(MAX_RESULT_NODES, 6);
1531+
1532+ when:
1533+ def er = graphQL.execute(ei)
1534+ def rni = ei.getGraphQLContext().get(ResultNodesInfo.RESULT_NODES_INFO) as ResultNodesInfo
1535+ then:
1536+ rni.resultNodesCount == 7
1537+ rni.maxResultNodesExceeded
1538+ er.data == [hello: [[name: "w1"], [name: "w2"], [name: null]]]
1539+ }
1540+
1541+ def "max result nodes not breached with list"() {
1542+ given:
1543+ def sdl = '''
1544+
1545+ type Query {
1546+ hello: [Foo]
1547+ }
1548+ type Foo {
1549+ name: String
1550+ }
1551+ '''
1552+ def df = { env -> [[name: "w1"], [name: "w2"], [name: "w3"]] } as DataFetcher
1553+ def fetchers = ["Query": ["hello": df]]
1554+ def schema = TestUtil.schema(sdl, fetchers)
1555+ def graphQL = GraphQL.newGraphQL(schema).build()
1556+
1557+ def query = "{ hello {name}}"
1558+ def ei = newExecutionInput(query).build()
1559+ // we have 7 result nodes overall
1560+ ei.getGraphQLContext().put(MAX_RESULT_NODES, 7);
1561+
1562+ when:
1563+ def er = graphQL.execute(ei)
1564+ def rni = ei.getGraphQLContext().get(ResultNodesInfo.RESULT_NODES_INFO) as ResultNodesInfo
1565+ then:
1566+ !rni.maxResultNodesExceeded
1567+ rni.resultNodesCount == 7
1568+ er.data == [hello: [[name: "w1"], [name: "w2"], [name: "w3"]]]
1569+ }
1570+
14301571}
0 commit comments