Skip to content

Commit e994f41

Browse files
dimdimychbbakerman
authored andcommitted
Allow and process DataFetcherResult as an item for GraphQL List fields.
1 parent 821241d commit e994f41

2 files changed

Lines changed: 94 additions & 1 deletion

File tree

‎src/main/java/graphql/execution/ExecutionStrategy.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,13 +519,16 @@ protected FieldValueInfo completeValueForList(ExecutionContext executionContext,
519519
NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext, stepInfoForListElement);
520520

521521
int finalIndex = index;
522+
FetchedValue value = unboxPossibleDataFetcherResult(executionContext, parameters, item);
523+
522524
ExecutionStrategyParameters newParameters = parameters.transform(builder ->
523525
builder.executionStepInfo(stepInfoForListElement)
524526
.nonNullFieldValidator(nonNullableFieldValidator)
525527
.listSize(values.size())
528+
.localContext(value.getLocalContext())
526529
.currentListIndex(finalIndex)
527530
.path(indexedPath)
528-
.source(item)
531+
.source(value.getFetchedValue())
529532
);
530533
fieldValueInfos.add(completeValue(executionContext, newParameters));
531534
index++;
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package graphql
2+
3+
import graphql.execution.DataFetcherResult
4+
import graphql.schema.DataFetcher
5+
import graphql.schema.DataFetchingEnvironment
6+
import graphql.schema.idl.RuntimeWiring
7+
import graphql.schema.idl.TypeRuntimeWiring
8+
import graphql.validation.ValidationError
9+
import graphql.validation.ValidationErrorType
10+
import spock.lang.Specification
11+
12+
class Issue1559 extends Specification {
13+
14+
def graphql = TestUtil.graphQL("""
15+
type Query {
16+
contextAwareList: [ContextAwareEntity!]!
17+
}
18+
19+
type ContextAwareEntity {
20+
name: String
21+
contextInfo: String
22+
}
23+
24+
""",
25+
RuntimeWiring.newRuntimeWiring()
26+
.type("ContextAwareEntity", {
27+
it.dataFetcher("contextInfo", { it.getLocalContext() })
28+
})
29+
.build())
30+
.build()
31+
32+
def "#1559 test if a list of DataFetcherResults is processed properly"() {
33+
34+
when:
35+
def input = ExecutionInput.newExecutionInput()
36+
.root([contextAwareList: [
37+
DataFetcherResult.newResult().localContext("the context 1").data([name: "the name 1"]).build(),
38+
DataFetcherResult.newResult().localContext("the context 2").data([name: "the name 2"]).build(),
39+
DataFetcherResult.newResult().localContext("the context 3").data([name: "the name 3"]).build(),
40+
DataFetcherResult.newResult().localContext("the context 4").data([name: "the name 4"]).build(),
41+
]])
42+
.query('''
43+
query getTheList {
44+
contextAwareList {
45+
name
46+
contextInfo
47+
}
48+
}
49+
''')
50+
.build()
51+
def executionResult = graphql.execute(input)
52+
53+
then:
54+
executionResult.errors.isEmpty()
55+
executionResult.data == [ contextAwareList: [
56+
[name: "the name 1", contextInfo: "the context 1"],
57+
[name: "the name 2", contextInfo: "the context 2"],
58+
[name: "the name 3", contextInfo: "the context 3"],
59+
[name: "the name 4", contextInfo: "the context 4"],
60+
]]
61+
62+
}
63+
def "#1559 test if null validations are processed properly on DataFetcherResult List"() {
64+
65+
when:
66+
def input = ExecutionInput.newExecutionInput()
67+
.root([contextAwareList: [
68+
DataFetcherResult.newResult().localContext("the context 1").data([name: "the name 1"]).build(),
69+
DataFetcherResult.newResult().localContext("the context 2").data(null).build(),
70+
null,
71+
DataFetcherResult.newResult().localContext("the context 4").data([name: "the name 4"]).build(),
72+
]])
73+
.query('''
74+
query getTheList {
75+
contextAwareList {
76+
name
77+
contextInfo
78+
}
79+
}
80+
''')
81+
.build()
82+
def executionResult = graphql.execute(input)
83+
84+
then:
85+
executionResult.errors.size() == 2
86+
executionResult.errors[0].path == ['contextAwareList', 1]
87+
executionResult.errors[1].path == ['contextAwareList', 2]
88+
}
89+
90+
}

0 commit comments

Comments
 (0)