Skip to content

Commit d2ac764

Browse files
refactor: Split out GraphQLResult file
1 parent 850b346 commit d2ac764

2 files changed

Lines changed: 51 additions & 52 deletions

File tree

Sources/GraphQL/GraphQL.swift

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,3 @@
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-
531
/// This is the primary entry point function for fulfilling GraphQL operations
542
/// by parsing, validating, and executing a GraphQL document along side a
553
/// GraphQL schema.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}

0 commit comments

Comments
 (0)