Skip to content
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

Add helper to API.execute #114

Merged
merged 2 commits into from
Mar 19, 2023
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
65 changes: 65 additions & 0 deletions Sources/Graphiti/API/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ public extension API {
)
}

func execute(
request: GraphQLRequest,
context: ContextType,
on eventLoopGroup: EventLoopGroup,
validationRules: [(ValidationContext) -> Visitor] = []
) -> EventLoopFuture<GraphQLResult> {
return execute(request: request.query,
context: context,
on: eventLoopGroup,
variables: request.variables,
operationName: request.operationName,
validationRules: validationRules
)
}

func subscribe(
request: String,
context: ContextType,
Expand All @@ -46,6 +61,22 @@ public extension API {
validationRules: validationRules
)
}

func subscribe(
request: GraphQLRequest,
context: ContextType,
on eventLoopGroup: EventLoopGroup,
validationRules: [(ValidationContext) -> Visitor] = []
) -> EventLoopFuture<SubscriptionResult> {
return subscribe(
request: request.query,
context: context,
on: eventLoopGroup,
variables: request.variables,
operationName: request.operationName,
validationRules: validationRules
)
}
}

#if compiler(>=5.5) && canImport(_Concurrency)
Expand All @@ -71,6 +102,23 @@ public extension API {
).get()
}

@available(macOS 10.15, iOS 15, watchOS 8, tvOS 15, *)
func execute(
request: GraphQLRequest,
context: ContextType,
on eventLoopGroup: EventLoopGroup,
validationRules: [(ValidationContext) -> Visitor] = []
) async throws -> GraphQLResult {
return try await execute(
request: request.query,
context: context,
on: eventLoopGroup,
variables: request.variables,
operationName: request.operationName,
validationRules: validationRules
)
}

@available(macOS 10.15, iOS 15, watchOS 8, tvOS 15, *)
func subscribe(
request: String,
Expand All @@ -90,6 +138,23 @@ public extension API {
validationRules: validationRules
).get()
}

@available(macOS 10.15, iOS 15, watchOS 8, tvOS 15, *)
func subscribe(
request: GraphQLRequest,
context: ContextType,
on eventLoopGroup: EventLoopGroup,
validationRules: [(ValidationContext) -> Visitor] = []
) async throws -> SubscriptionResult {
return try await subscribe(
request: request.query,
context: context,
on: eventLoopGroup,
variables: request.variables,
operationName: request.operationName,
validationRules: validationRules
)
}
}

#endif
34 changes: 34 additions & 0 deletions Tests/GraphitiTests/HelloWorldTests/HelloWorldTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,40 @@ class HelloWorldTests: XCTestCase {
wait(for: [expectation], timeout: 10)
}

func testInputRequest() throws {
let mutation = """
mutation addUser($user: UserInput!) {
addUser(user: $user) {
id,
name
}
}
"""
let variables: [String: Map] = ["user": ["id": "123", "name": "bob"]]

let request = GraphQLRequest(
query: mutation,
variables: variables
)

let expected = GraphQLResult(
data: ["addUser": ["id": "123", "name": "bob"]]
)

let expectation = XCTestExpectation()

api.execute(
request: request,
context: api.context,
on: group
).whenSuccess { result in
XCTAssertEqual(result, expected)
expectation.fulfill()
}

wait(for: [expectation], timeout: 10)
}

func testInputRecursive() throws {
let mutation = """
mutation addUser($user: UserInput!) {
Expand Down