Skip to content

Commit d68bfb7

Browse files
feature: GraphQLError gets extensions property
1 parent 518f5db commit d68bfb7

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

Sources/GraphQL/Error/GraphQLError.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public struct GraphQLError: Error, Codable {
99
case message
1010
case locations
1111
case path
12+
case extensions
1213
}
1314

1415
/**
@@ -37,6 +38,12 @@ public struct GraphQLError: Error, Codable {
3738
* Appears in the result of `description`.
3839
*/
3940
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]
4047

4148
/**
4249
* An array of GraphQL AST Nodes corresponding to this error.
@@ -65,7 +72,8 @@ public struct GraphQLError: Error, Codable {
6572
source: Source? = nil,
6673
positions: [Int] = [],
6774
path: IndexPath = [],
68-
originalError: Error? = nil
75+
originalError: Error? = nil,
76+
extensions: [String: Map] = [:]
6977
) {
7078
self.message = message
7179
self.nodes = nodes
@@ -92,16 +100,19 @@ public struct GraphQLError: Error, Codable {
92100

93101
self.path = path
94102
self.originalError = originalError
103+
self.extensions = extensions
95104
}
96105

97106
public init(
98107
message: String,
99108
locations: [SourceLocation],
100-
path: IndexPath = []
109+
path: IndexPath = [],
110+
extensions: [String: Map] = [:]
101111
) {
102112
self.message = message
103113
self.locations = locations
104114
self.path = path
115+
self.extensions = extensions
105116
nodes = []
106117
source = nil
107118
positions = []
@@ -120,6 +131,7 @@ public struct GraphQLError: Error, Codable {
120131
message = try container.decode(String.self, forKey: .message)
121132
locations = (try? container.decode([SourceLocation]?.self, forKey: .locations)) ?? []
122133
path = try container.decode(IndexPath.self, forKey: .path)
134+
extensions = try container.decodeIfPresent([String: Map].self, forKey: .extensions) ?? [:]
123135
}
124136

125137
public func encode(to encoder: Encoder) throws {
@@ -132,6 +144,10 @@ public struct GraphQLError: Error, Codable {
132144
}
133145

134146
try container.encode(path, forKey: .path)
147+
148+
if !extensions.isEmpty {
149+
try container.encode(extensions, forKey: .extensions)
150+
}
135151
}
136152
}
137153

0 commit comments

Comments
 (0)