-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathGraphQLError.cs
More file actions
78 lines (66 loc) · 2.52 KB
/
GraphQLError.cs
File metadata and controls
78 lines (66 loc) · 2.52 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
using System.Collections.Generic;
using System.Linq;
namespace EntityGraphQL;
/// <summary>
/// Describes any errors that might happen while resolving the query request
/// </summary>
public class GraphQLError : Dictionary<string, object>
{
private static readonly string MessageKey = "message";
private static readonly string PathKey = "path";
public string Message => (string)this[MessageKey];
public List<string>? Path
{
get => (TryGetValue(PathKey, out var value) && value is List<string> list) ? list : null;
set
{
if (value == null)
Remove(PathKey);
else
this[PathKey] = value;
}
}
public Dictionary<string, object>? Extensions => (Dictionary<string, object>?)this.GetValueOrDefault(QueryResult.ExtensionsKey);
public GraphQLError(string message, IDictionary<string, object>? extensions)
{
this[MessageKey] = message;
if (extensions != null)
this[QueryResult.ExtensionsKey] = new Dictionary<string, object>(extensions);
}
public bool IsExecutionError => Path != null;
public GraphQLError(string message, IEnumerable<string>? path, IDictionary<string, object>? extensions)
{
this[MessageKey] = message;
if (path != null)
this[PathKey] = path.ToList();
if (extensions != null)
this[QueryResult.ExtensionsKey] = new Dictionary<string, object>(extensions);
}
public override bool Equals(object? obj)
{
if (obj is not GraphQLError other)
return false;
bool extensionsEqual =
(Extensions == null && other.Extensions == null)
|| (Extensions != null && other.Extensions != null && Extensions.Count == other.Extensions.Count && !Extensions.Except(other.Extensions).Any());
return Message == other.Message && ((Path == null && other.Path == null) || (Path != null && other.Path != null && Path.SequenceEqual(other.Path))) && extensionsEqual;
}
public override int GetHashCode()
{
int hash = Message?.GetHashCode() ?? 0;
if (Path != null)
{
foreach (var p in Path)
hash = hash * 31 + (p?.GetHashCode() ?? 0);
}
if (Extensions != null)
{
foreach (var kv in Extensions.OrderBy(kv => kv.Key))
{
hash = hash * 31 + kv.Key.GetHashCode();
hash = hash * 31 + (kv.Value?.GetHashCode() ?? 0);
}
}
return hash;
}
}