|
1 | | -public struct GraphQLResult: Equatable, Codable, Sendable, CustomStringConvertible { |
2 | | - public var data: Map? |
3 | | - public var errors: [GraphQLError] |
4 | | - |
5 | | - public init(data: Map? = nil, errors: [GraphQLError] = []) { |
6 | | - self.data = data |
7 | | - self.errors = errors |
8 | | - } |
9 | | - |
10 | | - enum CodingKeys: String, CodingKey { |
11 | | - case data |
12 | | - case errors |
13 | | - } |
14 | | - |
15 | | - public init(from decoder: Decoder) throws { |
16 | | - let container = try decoder.container(keyedBy: CodingKeys.self) |
17 | | - data = try container.decodeIfPresent(Map.self, forKey: .data) |
18 | | - errors = try container.decodeIfPresent([GraphQLError].self, forKey: .errors) ?? [] |
19 | | - } |
20 | | - |
21 | | - public func encode(to encoder: Encoder) throws { |
22 | | - var container = encoder.container(keyedBy: CodingKeys.self) |
23 | | - |
24 | | - if let data = data { |
25 | | - try container.encode(data, forKey: .data) |
26 | | - } |
27 | | - |
28 | | - if !errors.isEmpty { |
29 | | - try container.encode(errors, forKey: .errors) |
30 | | - } |
31 | | - } |
32 | | - |
33 | | - public var description: String { |
34 | | - guard |
35 | | - let data = try? GraphQLJSONEncoder().encode(self), |
36 | | - let dataString = String(data: data, encoding: .utf8) |
37 | | - else { |
38 | | - return "Unable to encode GraphQLResult" |
39 | | - } |
40 | | - return dataString |
41 | | - } |
42 | | -} |
43 | | - |
44 | | -/// A collection of GraphQL errors. Enables returning multiple errors from Result types. |
45 | | -public struct GraphQLErrors: Error, Sendable { |
46 | | - public let errors: [GraphQLError] |
47 | | - |
48 | | - public init(_ errors: [GraphQLError]) { |
49 | | - self.errors = errors |
50 | | - } |
51 | | -} |
52 | | - |
53 | 1 | /// This is the primary entry point function for fulfilling GraphQL operations |
54 | 2 | /// by parsing, validating, and executing a GraphQL document along side a |
55 | 3 | /// GraphQL schema. |
|
0 commit comments