Skip to content

feature: Adds request operation type function #129

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
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
11 changes: 10 additions & 1 deletion Sources/GraphQL/GraphQLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public struct GraphQLRequest: Equatable, Codable {
/// - Returns: True if request is a subscription, false if it is an atomic operation (like
/// `query` or `mutation`)
public func isSubscription() throws -> Bool {
return try operationType() == .subscription
}

/// The type of operation perfomed 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
public func operationType() throws -> OperationType {
let documentAST = try GraphQL.parse(
instrumentation: NoOpInstrumentation,
source: Source(body: query, name: "GraphQL request")
Expand All @@ -35,6 +44,6 @@ public struct GraphQLRequest: Equatable, Codable {
guard let operationType = firstOperation?.operation else {
throw GraphQLError(message: "GraphQL operation type could not be determined")
}
return operationType == .subscription
return operationType
}
}