@@ -9,6 +9,7 @@ public struct GraphQLError: Error, Codable {
9
9
case message
10
10
case locations
11
11
case path
12
+ case extensions
12
13
}
13
14
14
15
/**
@@ -37,6 +38,12 @@ public struct GraphQLError: Error, Codable {
37
38
* Appears in the result of `description`.
38
39
*/
39
40
public let path : IndexPath
41
+
42
+ /// Reserved for implementors to add additional information to errors
43
+ /// however they see fit, and there are no restrictions on its contents.
44
+ ///
45
+ /// See: https://spec.graphql.org/October2021/#sel-HAPHRPZCBiCBzG67F
46
+ public let extensions : [ String : Map ]
40
47
41
48
/**
42
49
* An array of GraphQL AST Nodes corresponding to this error.
@@ -65,7 +72,8 @@ public struct GraphQLError: Error, Codable {
65
72
source: Source ? = nil ,
66
73
positions: [ Int ] = [ ] ,
67
74
path: IndexPath = [ ] ,
68
- originalError: Error ? = nil
75
+ originalError: Error ? = nil ,
76
+ extensions: [ String : Map ] = [ : ]
69
77
) {
70
78
self . message = message
71
79
self . nodes = nodes
@@ -92,16 +100,19 @@ public struct GraphQLError: Error, Codable {
92
100
93
101
self . path = path
94
102
self . originalError = originalError
103
+ self . extensions = extensions
95
104
}
96
105
97
106
public init (
98
107
message: String ,
99
108
locations: [ SourceLocation ] ,
100
- path: IndexPath = [ ]
109
+ path: IndexPath = [ ] ,
110
+ extensions: [ String : Map ] = [ : ]
101
111
) {
102
112
self . message = message
103
113
self . locations = locations
104
114
self . path = path
115
+ self . extensions = extensions
105
116
nodes = [ ]
106
117
source = nil
107
118
positions = [ ]
@@ -120,6 +131,7 @@ public struct GraphQLError: Error, Codable {
120
131
message = try container. decode ( String . self, forKey: . message)
121
132
locations = ( try ? container. decode ( [ SourceLocation] ? . self, forKey: . locations) ) ?? [ ]
122
133
path = try container. decode ( IndexPath . self, forKey: . path)
134
+ extensions = try container. decodeIfPresent ( [ String : Map ] . self, forKey: . extensions) ?? [ : ]
123
135
}
124
136
125
137
public func encode( to encoder: Encoder ) throws {
@@ -132,6 +144,10 @@ public struct GraphQLError: Error, Codable {
132
144
}
133
145
134
146
try container. encode ( path, forKey: . path)
147
+
148
+ if !extensions. isEmpty {
149
+ try container. encode ( extensions, forKey: . extensions)
150
+ }
135
151
}
136
152
}
137
153
0 commit comments