Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions Sources/GraphQL/Execution/Execute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
52 changes: 0 additions & 52 deletions Sources/GraphQL/GraphQL.swift
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 3 additions & 1 deletion Sources/GraphQL/GraphQLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
51 changes: 51 additions & 0 deletions Sources/GraphQL/GraphQLResult.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading