-
Notifications
You must be signed in to change notification settings - Fork 749
Expand file tree
/
Copy pathGraphQLError.swift
More file actions
78 lines (64 loc) · 2.2 KB
/
GraphQLError.swift
File metadata and controls
78 lines (64 loc) · 2.2 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
import Foundation
#if !COCOAPODS
import ApolloMigrationAPI
#endif
/// Represents an error encountered during the execution of a GraphQL operation.
///
/// - SeeAlso: [The Response Format section in the GraphQL specification](https://facebook.github.io/graphql/#sec-Response-Format)
public struct GraphQLError: Error, Hashable {
private let object: JSONObject
public init(_ object: JSONObject) {
self.object = object
}
init(_ message: String) {
self.init(["message": message])
}
/// GraphQL servers may provide additional entries as they choose to produce more helpful or machine‐readable errors.
public subscript(key: String) -> Any? {
return object[key]
}
/// A description of the error.
public var message: String? {
return self["message"] as? String
}
/// A list of locations in the requested GraphQL document associated with the error.
public var locations: [Location]? {
return (self["locations"] as? [JSONObject])?.compactMap(Location.init)
}
/// A path to the field that triggered the error, represented by an array of Path Entries.
public var path: [PathEntry]? {
return (self["path"] as? [JSONValue])?.compactMap(PathEntry.init)
}
/// A dictionary which services can use however they see fit to provide additional information in errors to clients.
public var extensions: [String : Any]? {
return self["extensions"] as? [String : Any]
}
/// Represents a location in a GraphQL document.
public struct Location {
/// The line number of a syntax element.
public let line: Int
/// The column number of a syntax element.
public let column: Int
init?(_ object: JSONObject) {
guard let line = object["line"] as? Int, let column = object["column"] as? Int else { return nil }
self.line = line
self.column = column
}
}
public typealias PathEntry = PathComponent
}
extension GraphQLError: CustomStringConvertible {
public var description: String {
return self.message ?? "GraphQL Error"
}
}
extension GraphQLError: LocalizedError {
public var errorDescription: String? {
return self.description
}
}
extension GraphQLError {
func asJSONDictionary() -> [String: Any] {
JSONConverter.convert(self)
}
}