Skip to content

Commit d556287

Browse files
Cookejoemcbride
authored andcommitted
Fixed support for list with nulls (graphql-dotnet#838) (graphql-dotnet#839)
1 parent 31c382c commit d556287

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using GraphQL.Types;
2+
using Xunit;
3+
4+
namespace GraphQL.Tests.Bugs
5+
{
6+
public class ListWithNullablesTest : QueryTestBase<ListWithNullablesSchema>
7+
{
8+
[Fact]
9+
public void Can_Accept_Null_List_From_Literal()
10+
{
11+
var query = @"
12+
query _ {
13+
list {
14+
value
15+
}
16+
}";
17+
var expected = @"
18+
{
19+
'list': [{ value: ""one""}, null, { value: ""three"" }]
20+
}";
21+
AssertQuerySuccess(query, expected);
22+
}
23+
}
24+
25+
public class ListWithNullablesSchema : Schema
26+
{
27+
public ListWithNullablesSchema()
28+
{
29+
Query = new ListWithNullablesQuery();
30+
}
31+
}
32+
33+
public class ListWithNullablesQuery : ObjectGraphType
34+
{
35+
public ListWithNullablesQuery()
36+
{
37+
Name = "Query";
38+
39+
Field<ListGraphType<ListEntityGraphType>>(
40+
"list",
41+
resolve: context => new[] {new ListEntity {Value = "one"}, null, new ListEntity {Value = "three"}});
42+
}
43+
}
44+
45+
public class ListEntity
46+
{
47+
public string Value { get; set; }
48+
}
49+
50+
public class ListEntityGraphType : ObjectGraphType<ListEntity>
51+
{
52+
public ListEntityGraphType()
53+
{
54+
Name = "Entity";
55+
56+
Field(x => x.Value);
57+
}
58+
}
59+
}

src/GraphQL/Execution/ExecutionStrategy.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,26 @@ public static void SetArrayItemNodes(ExecutionContext context, ArrayExecutionNod
106106
{
107107
var path = AppendPath(parent.Path, (index++).ToString());
108108

109-
ExecutionNode node = BuildExecutionNode(parent, itemType, parent.Field, parent.FieldDefinition, path);
110-
node.Result = d;
109+
if (d != null)
110+
{
111+
var node = BuildExecutionNode(parent, itemType, parent.Field, parent.FieldDefinition, path);
112+
node.Result = d;
111113

112-
if (node is ObjectExecutionNode objectNode)
114+
if (node is ObjectExecutionNode objectNode)
115+
{
116+
SetSubFieldNodes(context, objectNode);
117+
}
118+
119+
arrayItems.Add(node);
120+
}
121+
else
113122
{
114-
SetSubFieldNodes(context, objectNode);
123+
var valueExecutionNode = new ValueExecutionNode(parent, itemType, parent.Field, parent.FieldDefinition, path)
124+
{
125+
Result = null
126+
};
127+
arrayItems.Add(valueExecutionNode);
115128
}
116-
117-
arrayItems.Add(node);
118129
}
119130

120131
parent.Items = arrayItems;

0 commit comments

Comments
 (0)