Skip to content

Commit

Permalink
Merge pull request #114 from samisuteria/samisuteria/execute-helper
Browse files Browse the repository at this point in the history
Add helper to API.execute
  • Loading branch information
NeedleInAJayStack authored Mar 19, 2023
2 parents 64b180e + 05a1d20 commit b4942cf
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
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

0 comments on commit b4942cf

Please sign in to comment.