Skip to content

Commit 984f483

Browse files
authored
Fix for empty error array being created on all responses (#2)
1 parent 9914e71 commit 984f483

3 files changed

Lines changed: 34 additions & 8 deletions

File tree

‎src/EntityGraphQL/Compiler/GraphQLResultNode.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public async Task<QueryResult> ExecuteQueryAsync<TContext>(TContext context, ISe
8484
}
8585

8686
if (validator.Errors.Count > 0)
87-
result.Errors.AddRange(validator.Errors);
87+
result.AddErrors(validator.Errors);
8888

8989
return result;
9090
}

‎src/EntityGraphQL/QueryResult.cs‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,47 @@
11
using System.Collections.Concurrent;
22
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.Linq;
35
using Newtonsoft.Json;
46

57
namespace EntityGraphQL
68
{
79
public class QueryResult
810
{
11+
private List<GraphQLError> errors = null;
912
[JsonProperty("errors")]
10-
public List<GraphQLError> Errors => (List<GraphQLError>)dataResults["errors"];
13+
public ReadOnlyCollection<GraphQLError> Errors => errors?.AsReadOnly();
1114
[JsonProperty("data")]
12-
public ConcurrentDictionary<string, object> Data => (ConcurrentDictionary<string, object>)dataResults["data"];
13-
private readonly ConcurrentDictionary<string, object> dataResults = new ConcurrentDictionary<string, object>();
15+
public readonly ConcurrentDictionary<string, object> Data = new ConcurrentDictionary<string, object>();
1416

15-
public QueryResult()
17+
public QueryResult() { }
18+
public QueryResult(GraphQLError error)
1619
{
17-
dataResults["errors"] = new List<GraphQLError>();
18-
dataResults["data"] = new ConcurrentDictionary<string, object>();
20+
errors = new List<GraphQLError> { error };
21+
}
22+
public QueryResult(IEnumerable<GraphQLError> errors)
23+
{
24+
this.errors = errors.ToList();
25+
}
26+
27+
public void AddError(GraphQLError error)
28+
{
29+
if (errors == null)
30+
{
31+
errors = new List<GraphQLError>();
32+
}
33+
34+
errors.Add(error);
35+
}
36+
37+
public void AddErrors(IEnumerable<GraphQLError> errors)
38+
{
39+
if (errors == null)
40+
{
41+
errors = new List<GraphQLError>();
42+
}
43+
44+
this.errors.AddRange(errors);
1945
}
2046
}
2147
}

‎src/EntityGraphQL/Schema/SchemaProvider.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public async Task<QueryResult> ExecuteQueryAsync(QueryRequest gql, TContextType
124124
catch (Exception ex)
125125
{
126126
// error with the whole query
127-
result = new QueryResult { Errors = { new GraphQLError(ex.InnerException != null ? ex.InnerException.Message : ex.Message) } };
127+
result = new QueryResult(new GraphQLError(ex.InnerException != null ? ex.InnerException.Message : ex.Message));
128128
}
129129

130130
return result;

0 commit comments

Comments
 (0)