44 * it also includes information about the locations in a
55 * GraphQL document and/or execution result that correspond to the error.
66 */
7- public struct GraphQLError : Error , Encodable {
8- private enum CodingKeys : String , CodingKey {
9- case message
10- case locations
11- case path
12- }
7+ public struct GraphQLError : Error {
138
149 /**
1510 * A message describing the Error for debugging purposes.
@@ -36,7 +31,7 @@ public struct GraphQLError : Error, Encodable {
3631 *
3732 * Appears in the result of `description`.
3833 */
39- public let path : IndexPath
34+ public let path : [ IndexPathElement ]
4035
4136 /**
4237 * An array of GraphQL AST Nodes corresponding to this error.
@@ -59,14 +54,8 @@ public struct GraphQLError : Error, Encodable {
5954 */
6055 public let originalError : Error ?
6156
62- public init (
63- message: String ,
64- nodes: [ Node ] = [ ] ,
65- source: Source ? = nil ,
66- positions: [ Int ] = [ ] ,
67- path: IndexPath = [ ] ,
68- originalError: Error ? = nil
69- ) {
57+ public init ( message: String , nodes: [ Node ] = [ ] , source: Source ? = nil , positions: [ Int ] = [ ] ,
58+ path: [ IndexPathElement ] = [ ] , originalError: Error ? = nil ) {
7059 self . message = message
7160 self . nodes = nodes
7261
@@ -93,27 +82,6 @@ public struct GraphQLError : Error, Encodable {
9382 self . path = path
9483 self . originalError = originalError
9584 }
96-
97- public init (
98- message: String ,
99- locations: [ SourceLocation ] ,
100- path: IndexPath = [ ]
101- ) {
102- self . message = message
103- self . locations = locations
104- self . path = path
105- self . nodes = [ ]
106- self . source = nil
107- self . positions = [ ]
108- self . originalError = nil
109- }
110-
111- public init ( _ error: Error ) {
112- self . init (
113- message: error. localizedDescription,
114- originalError: error
115- )
116- }
11785}
11886
11987extension GraphQLError : CustomStringConvertible {
@@ -132,96 +100,23 @@ extension GraphQLError : Hashable {
132100 }
133101}
134102
135- // MARK: IndexPath
136-
137- public struct IndexPath : Encodable {
138- public let elements : [ IndexPathValue ]
139-
140- public init ( _ elements: [ IndexPathElement ] = [ ] ) {
141- self . elements = elements. map ( { $0. indexPathValue } )
142- }
143-
144- public func appending( _ elements: IndexPathElement ) -> IndexPath {
145- return IndexPath ( self . elements + [ elements] )
146- }
147- }
148-
149- extension IndexPath : ExpressibleByArrayLiteral {
150- public init ( arrayLiteral elements: IndexPathElement ... ) {
151- self . elements = elements. map ( { $0. indexPathValue } )
152- }
153- }
103+ extension GraphQLError : MapRepresentable {
104+ public var map : Map {
105+ var dictionary : [ String : Map ] = [ " message " : message. map]
154106
155- public enum IndexPathValue : Encodable {
156- case index( Int )
157- case key( String )
158-
159- public func encode( to encoder: Encoder ) throws {
160- var container = encoder. singleValueContainer ( )
161-
162- switch self {
163- case let . index( index) :
164- try container. encode ( index)
165- case let . key( key) :
166- try container. encode ( key)
107+ if !path. isEmpty {
108+ dictionary [ " path " ] = path. map ( { $0. map } ) . map
167109 }
168- }
169- }
170110
171- extension IndexPathValue : IndexPathElement {
172- public var indexPathValue : IndexPathValue {
173- return self
174- }
175- }
176-
177- extension IndexPathValue : CustomStringConvertible {
178- public var description : String {
179- switch self {
180- case let . index( index) :
181- return index. description
182- case let . key( key) :
183- return key. description
111+ if !locations. isEmpty {
112+ dictionary [ " locations " ] = locations. map ( {
113+ return [
114+ " line " : $0. line. map,
115+ " column " : $0. column. map
116+ ] as Map
117+ } ) . map
184118 }
185- }
186- }
187-
188- public protocol IndexPathElement {
189- var indexPathValue : IndexPathValue { get }
190- }
191-
192- extension IndexPathElement {
193- var constructEmptyContainer : Map {
194- switch indexPathValue {
195- case . index: return [ ]
196- case . key: return [ : ]
197- }
198- }
199- }
200-
201- extension IndexPathElement {
202- public var indexValue : Int ? {
203- if case . index( let index) = indexPathValue {
204- return index
205- }
206- return nil
207- }
208-
209- public var keyValue : String ? {
210- if case . key( let key) = indexPathValue {
211- return key
212- }
213- return nil
214- }
215- }
216-
217- extension Int : IndexPathElement {
218- public var indexPathValue : IndexPathValue {
219- return . index( self )
220- }
221- }
222119
223- extension String : IndexPathElement {
224- public var indexPathValue : IndexPathValue {
225- return . key( self )
120+ return . dictionary( dictionary)
226121 }
227122}
0 commit comments