Skip to content

Commit 42549f9

Browse files
Merge pull request #185 from NeedleInAJayStack/chore/deprecate-result-operation-type
Deprecates `result.operationType` and `result.isSubscription`
2 parents 850b346 + f68e9ed commit 42549f9

4 files changed

Lines changed: 56 additions & 68 deletions

File tree

Sources/GraphQL/Execution/Execute.swift

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -183,26 +183,13 @@ public func execute(
183183
}
184184

185185
do {
186-
// var executeErrors: [GraphQLError] = []
187186
let data = try await executeOperation(
188187
exeContext: buildContext,
189188
operation: buildContext.operation,
190189
rootValue: rootValue
191190
)
192-
var dataMap: Map = [:]
193-
194-
for (key, value) in data {
195-
dataMap[key] = try map(from: value)
196-
}
197-
198-
var result: GraphQLResult = .init(data: dataMap)
199-
200-
if !buildContext.errors.isEmpty {
201-
result.errors = buildContext.errors
202-
}
203-
204-
// executeErrors = buildContext.errors
205-
return result
191+
let dataMap = try Map(data.mapValues { try map(from: $0) })
192+
return GraphQLResult(data: dataMap, errors: buildContext.errors)
206193
} catch let error as GraphQLError {
207194
return GraphQLResult(errors: [error])
208195
} catch {

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.

Sources/GraphQL/GraphQLRequest.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@ public struct GraphQLRequest: Equatable, Codable, Sendable {
2626
///
2727
/// - Returns: True if request is a subscription, false if it is an atomic operation (like
2828
/// `query` or `mutation`)
29+
@available(*, deprecated, message: "Use `parse` and extract operation manually instead")
2930
public func isSubscription() throws -> Bool {
3031
return try operationType() == .subscription
3132
}
3233

33-
/// The type of operation perfomed by the request.
34+
/// The type of operation performed by the request.
3435
/// This operation performs an entire AST parse on the GraphQL request, so consider
3536
/// performance when calling multiple times.
3637
///
3738
/// - Returns: The operation type performed by the request
39+
@available(*, deprecated, message: "Use `parse` and extract operation manually instead")
3840
public func operationType() throws -> OperationType {
3941
let documentAST = try GraphQL.parse(
4042
source: Source(body: query, name: "GraphQL request")
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)