Skip to content

Commit 2fbbb56

Browse files
committed
Make GraphQLResult Encodable.
1 parent a47a2c2 commit 2fbbb56

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

Sources/GraphQL/GraphQL.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
1+
import Foundation
12
import NIO
23

3-
public struct GraphQLResult : Equatable {
4+
public struct GraphQLResult : Equatable, Encodable, CustomStringConvertible {
45
public var data: Map?
56
public var errors: [GraphQLError]
67

78
public init(data: Map? = nil, errors: [GraphQLError] = []) {
89
self.data = data
910
self.errors = errors
1011
}
12+
13+
enum CodingKeys : String, CodingKey {
14+
case data
15+
case errors
16+
}
17+
18+
public func encode(to encoder: Encoder) throws {
19+
var container = encoder.container(keyedBy: CodingKeys.self)
20+
21+
if let data = self.data {
22+
try container.encode(data, forKey: .data)
23+
}
24+
25+
if !self.errors.isEmpty {
26+
try container.encode(self.errors, forKey: .errors)
27+
}
28+
}
29+
30+
public var description: String {
31+
let data = try! JSONEncoder().encode(self)
32+
return String(data: data, encoding: .utf8)!
33+
}
1134
}
1235

1336
/// This is the primary entry point function for fulfilling GraphQL operations

0 commit comments

Comments
 (0)