-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathQueryResult.cs
More file actions
87 lines (70 loc) · 2.41 KB
/
QueryResult.cs
File metadata and controls
87 lines (70 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System.Collections.Generic;
using System.Linq;
namespace EntityGraphQL;
public class QueryResult : Dictionary<string, object?>
{
private static readonly string DataKey = "data";
private static readonly string ErrorsKey = "errors";
internal static readonly string ExtensionsKey = "extensions";
public List<GraphQLError>? Errors => (List<GraphQLError>?)this.GetValueOrDefault(ErrorsKey);
public Dictionary<string, object?>? Data => (Dictionary<string, object?>?)this.GetValueOrDefault(DataKey);
/// <summary>
/// Use Extensions to add any custom data for the result
/// </summary>
public Dictionary<string, object>? Extensions => (Dictionary<string, object>?)this.GetValueOrDefault(ExtensionsKey);
public QueryResult() { }
public QueryResult(GraphQLError error)
{
this[ErrorsKey] = new List<GraphQLError> { error };
}
public QueryResult(IEnumerable<GraphQLError> errors)
{
this[ErrorsKey] = errors.ToList();
}
public bool HasErrors() => Errors?.Count > 0;
public void AddError(string error, IDictionary<string, object>? extensions = null)
{
AddError(new GraphQLError(error, extensions));
}
public void AddError(GraphQLError error)
{
if (!this.ContainsKey(ErrorsKey))
{
this[ErrorsKey] = new List<GraphQLError>();
}
((List<GraphQLError>)this[ErrorsKey]!).Add(error);
}
public void AddErrors(IEnumerable<GraphQLError> errors)
{
if (!this.ContainsKey(ErrorsKey))
{
this[ErrorsKey] = new List<GraphQLError>();
}
((List<GraphQLError>)this[ErrorsKey]!).AddRange(errors);
}
public void SetData(IDictionary<string, object?>? data)
{
this[DataKey] = data?.ToDictionary(d => d.Key, d => d.Value) ?? null;
}
public bool HasDataKey => ContainsKey(DataKey);
public void RemoveDataKey()
{
Remove(DataKey);
}
public bool HasErrorKey()
{
return ContainsKey(ErrorsKey);
}
/// <summary>
/// Add query information to the result extensions
/// </summary>
internal void SetQueryInfo(QueryInfo queryInfo)
{
if (!ContainsKey(ExtensionsKey))
{
this[ExtensionsKey] = new Dictionary<string, object>();
}
var extensions = (Dictionary<string, object>)this[ExtensionsKey]!;
extensions["queryInfo"] = queryInfo;
}
}