Skip to content

Commit

Permalink
feature: Adds request operation type function
Browse files Browse the repository at this point in the history
  • Loading branch information
NeedleInAJayStack committed Sep 12, 2023
1 parent c265770 commit 505df31
Showing 1 changed file with 10 additions and 1 deletion.
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 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
}
}

0 comments on commit 505df31

Please sign in to comment.