From d2ac764f22d193dae8986fd7c71ebdc1b8baa505 Mon Sep 17 00:00:00 2001 From: Jay Herron Date: Tue, 7 Jul 2026 21:26:29 -0500 Subject: [PATCH 1/3] refactor: Split out GraphQLResult file --- Sources/GraphQL/GraphQL.swift | 52 ----------------------------- Sources/GraphQL/GraphQLResult.swift | 51 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 52 deletions(-) create mode 100644 Sources/GraphQL/GraphQLResult.swift diff --git a/Sources/GraphQL/GraphQL.swift b/Sources/GraphQL/GraphQL.swift index b57199e5..a60a84c1 100644 --- a/Sources/GraphQL/GraphQL.swift +++ b/Sources/GraphQL/GraphQL.swift @@ -1,55 +1,3 @@ -public struct GraphQLResult: Equatable, Codable, Sendable, CustomStringConvertible { - public var data: Map? - public var errors: [GraphQLError] - - public init(data: Map? = nil, errors: [GraphQLError] = []) { - self.data = data - self.errors = errors - } - - enum CodingKeys: String, CodingKey { - case data - case errors - } - - public init(from decoder: Decoder) throws { - let container = try decoder.container(keyedBy: CodingKeys.self) - data = try container.decodeIfPresent(Map.self, forKey: .data) - errors = try container.decodeIfPresent([GraphQLError].self, forKey: .errors) ?? [] - } - - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - - if let data = data { - try container.encode(data, forKey: .data) - } - - if !errors.isEmpty { - try container.encode(errors, forKey: .errors) - } - } - - public var description: String { - guard - let data = try? GraphQLJSONEncoder().encode(self), - let dataString = String(data: data, encoding: .utf8) - else { - return "Unable to encode GraphQLResult" - } - return dataString - } -} - -/// A collection of GraphQL errors. Enables returning multiple errors from Result types. -public struct GraphQLErrors: Error, Sendable { - public let errors: [GraphQLError] - - public init(_ errors: [GraphQLError]) { - self.errors = errors - } -} - /// This is the primary entry point function for fulfilling GraphQL operations /// by parsing, validating, and executing a GraphQL document along side a /// GraphQL schema. diff --git a/Sources/GraphQL/GraphQLResult.swift b/Sources/GraphQL/GraphQLResult.swift new file mode 100644 index 00000000..3ae62bb7 --- /dev/null +++ b/Sources/GraphQL/GraphQLResult.swift @@ -0,0 +1,51 @@ +public struct GraphQLResult: Equatable, Codable, Sendable, CustomStringConvertible { + public var data: Map? + public var errors: [GraphQLError] + + public init(data: Map? = nil, errors: [GraphQLError] = []) { + self.data = data + self.errors = errors + } + + enum CodingKeys: String, CodingKey { + case data + case errors + } + + public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + data = try container.decodeIfPresent(Map.self, forKey: .data) + errors = try container.decodeIfPresent([GraphQLError].self, forKey: .errors) ?? [] + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + + if let data = data { + try container.encode(data, forKey: .data) + } + + if !errors.isEmpty { + try container.encode(errors, forKey: .errors) + } + } + + public var description: String { + guard + let data = try? GraphQLJSONEncoder().encode(self), + let dataString = String(data: data, encoding: .utf8) + else { + return "Unable to encode GraphQLResult" + } + return dataString + } +} + +/// A collection of GraphQL errors. Enables returning multiple errors from Result types. +public struct GraphQLErrors: Error, Sendable { + public let errors: [GraphQLError] + + public init(_ errors: [GraphQLError]) { + self.errors = errors + } +} From 33bb223c365975ebe5cf22d5a1ce2b0ce39838e3 Mon Sep 17 00:00:00 2001 From: Jay Herron Date: Tue, 7 Jul 2026 21:26:50 -0500 Subject: [PATCH 2/3] refactor: Simplify execution result --- Sources/GraphQL/Execution/Execute.swift | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/Sources/GraphQL/Execution/Execute.swift b/Sources/GraphQL/Execution/Execute.swift index c5abd222..1d9ea59c 100644 --- a/Sources/GraphQL/Execution/Execute.swift +++ b/Sources/GraphQL/Execution/Execute.swift @@ -183,26 +183,13 @@ public func execute( } do { - // var executeErrors: [GraphQLError] = [] let data = try await executeOperation( exeContext: buildContext, operation: buildContext.operation, rootValue: rootValue ) - var dataMap: Map = [:] - - for (key, value) in data { - dataMap[key] = try map(from: value) - } - - var result: GraphQLResult = .init(data: dataMap) - - if !buildContext.errors.isEmpty { - result.errors = buildContext.errors - } - - // executeErrors = buildContext.errors - return result + let dataMap = try Map(data.mapValues { try map(from: $0) }) + return GraphQLResult(data: dataMap, errors: buildContext.errors) } catch let error as GraphQLError { return GraphQLResult(errors: [error]) } catch { From f68e9ed72fe058701ab18fe2c11b999f1ffef093 Mon Sep 17 00:00:00 2001 From: Jay Herron Date: Tue, 7 Jul 2026 21:54:37 -0500 Subject: [PATCH 3/3] chore: Deprecate result.operationType and result.isSubscription --- Sources/GraphQL/GraphQLRequest.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sources/GraphQL/GraphQLRequest.swift b/Sources/GraphQL/GraphQLRequest.swift index e894f9a7..fe0d63fe 100644 --- a/Sources/GraphQL/GraphQLRequest.swift +++ b/Sources/GraphQL/GraphQLRequest.swift @@ -26,15 +26,17 @@ public struct GraphQLRequest: Equatable, Codable, Sendable { /// /// - Returns: True if request is a subscription, false if it is an atomic operation (like /// `query` or `mutation`) + @available(*, deprecated, message: "Use `parse` and extract operation manually instead") public func isSubscription() throws -> Bool { return try operationType() == .subscription } - /// The type of operation perfomed by the request. + /// The type of operation performed by the request. /// This operation performs an entire AST parse on the GraphQL request, so consider /// performance when calling multiple times. /// /// - Returns: The operation type performed by the request + @available(*, deprecated, message: "Use `parse` and extract operation manually instead") public func operationType() throws -> OperationType { let documentAST = try GraphQL.parse( source: Source(body: query, name: "GraphQL request")