Skip to content

Adds GraphQLRequest type #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
"repositoryURL": "https://github.com/apple/swift-collections",
"state": {
"branch": null,
"revision": "2d33a0ea89c961dcb2b3da2157963d9c0370347e",
"version": "1.0.1"
"revision": "48254824bb4248676bf7ce56014ff57b142b77eb",
"version": "1.0.2"
}
},
{
"package": "swift-nio",
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "120acb15c39aa3217e9888e515de160378fbcc1e",
"version": "2.18.0"
"revision": "51c3fc2e4a0fcdf4a25089b288dd65b73df1b0ef",
"version": "2.37.0"
}
}
]
Expand Down
40 changes: 40 additions & 0 deletions Sources/GraphQL/GraphQLRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Foundation

/// A GraphQL request object, containing `query`, `operationName`, and `variables` fields
public struct GraphQLRequest: Equatable, Codable {
public var query: String
public var operationName: String?
public var variables: [String: Map]

public init(query: String, operationName: String? = nil, variables: [String: Map] = [:]) {
self.query = query
self.operationName = operationName
self.variables = variables
}

// To handle decoding with a default of variables = []
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.query = try container.decode(String.self, forKey: .query)
self.operationName = try container.decodeIfPresent(String.self, forKey: .operationName)
self.variables = try container.decodeIfPresent([String: Map].self, forKey: .variables) ?? [:]
}

/// Boolean indicating if the GraphQL request is a subscription operation.
/// This operation performs an entire AST parse on the GraphQL request, so consider
/// performance when calling multiple times.
///
/// - Returns: True if request is a subscription, false if it is an atomic operation (like `query` or `mutation`)
public func isSubscription() throws -> Bool {
let documentAST = try GraphQL.parse(
instrumentation: NoOpInstrumentation,
source: Source(body: self.query, name: "GraphQL request")
)
let firstOperation = documentAST.definitions.compactMap { $0 as? OperationDefinition }.first
guard let operationType = firstOperation?.operation else {
throw GraphQLError(message: "GraphQL operation type could not be determined")
}
return operationType == .subscription
}
}