-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Concurrency Stage 1: Introduces initial wrapper-style async/await sup…
…port (#184) * Introduces a wrapper-style async/await approach originally implemented by @elfenlaid * Adds the original implementation + improved header docs with usage caveats * Updates the API.swift based on recent changes that resolve Swift 6 errors (in future) * Update Sources/SwiftGraphQLClient/Client/Core.swift
- Loading branch information
Showing
7 changed files
with
272 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
Tests/SwiftGraphQLClientTests/AsyncClientTests.swift.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import GraphQL | ||
import SwiftGraphQLClient | ||
import XCTest | ||
import Combine | ||
import SwiftGraphQL | ||
|
||
final class AsyncInterfaceTests: XCTestCase { | ||
func testAsyncSelectionQueryReturnsValue() async throws { | ||
let selection = Selection<String, Objects.User> { | ||
try $0.id() | ||
} | ||
|
||
let client = MockClient(customExecute: { operation in | ||
let id = GraphQLField.leaf(field: "id", parent: "User", arguments: []) | ||
|
||
let user = GraphQLField.composite( | ||
field: "user", | ||
parent: "Query", | ||
type: "User", | ||
arguments: [], | ||
selection: selection.__selection() | ||
) | ||
|
||
let result = OperationResult( | ||
operation: operation, | ||
data: [ | ||
user.alias!: [ | ||
id.alias!: "123" | ||
] | ||
], | ||
error: nil | ||
) | ||
return Just(result).eraseToAnyPublisher() | ||
}) | ||
|
||
|
||
let result = try await client.query(Objects.Query.user(selection: selection)) | ||
XCTAssertEqual(result.data, "123") | ||
} | ||
|
||
func testAsyncSelectionQueryThrowsError() async throws { | ||
let selection = Selection<String, Objects.User> { | ||
try $0.id() | ||
} | ||
|
||
let client = MockClient(customExecute: { operation in | ||
let result = OperationResult( | ||
operation: operation, | ||
data: ["unknown_field": "123"], | ||
error: nil | ||
) | ||
return Just(result).eraseToAnyPublisher() | ||
}) | ||
|
||
await XCTAssertThrowsError(of: ObjectDecodingError.self) { | ||
try await client.query(Objects.Query.user(selection: selection)) | ||
} | ||
} | ||
|
||
func testAsyncSelectionMutationReturnsValue() async throws { | ||
let selection = Selection.AuthPayload<String?> { | ||
try $0.on( | ||
authPayloadSuccess: Selection.AuthPayloadSuccess<String?> { | ||
try $0.token() | ||
}, | ||
authPayloadFailure: Selection.AuthPayloadFailure<String?> { _ in | ||
nil | ||
} | ||
) | ||
} | ||
|
||
let client = MockClient(customExecute: { operation in | ||
let token = GraphQLField.leaf(field: "token", parent: "AuthPayloadSuccess", arguments: []) | ||
|
||
let auth = GraphQLField.composite( | ||
field: "auth", | ||
parent: "Mutation", | ||
type: "AuthPayload", | ||
arguments: [], | ||
selection: selection.__selection() | ||
) | ||
|
||
let result = OperationResult( | ||
operation: operation, | ||
data: [ | ||
auth.alias!: [ | ||
"__typename": "AuthPayloadSuccess", | ||
token.alias!: "123" | ||
] | ||
], | ||
error: nil | ||
) | ||
return Just(result).eraseToAnyPublisher() | ||
}) | ||
|
||
let result = try await client.mutate(Objects.Mutation.auth(selection: selection)) | ||
XCTAssertEqual(result.data, "123") | ||
} | ||
|
||
func testAsyncSelectionMutationThrowsError() async throws { | ||
let selection = Selection.AuthPayload<String?> { | ||
try $0.on( | ||
authPayloadSuccess: Selection.AuthPayloadSuccess<String?> { | ||
try $0.token() | ||
}, | ||
authPayloadFailure: Selection.AuthPayloadFailure<String?> { _ in | ||
nil | ||
} | ||
) | ||
} | ||
|
||
let client = MockClient(customExecute: { operation in | ||
let result = OperationResult( | ||
operation: operation, | ||
data: ["unknown_field": "123"], | ||
error: nil | ||
) | ||
return Just(result).eraseToAnyPublisher() | ||
}) | ||
|
||
await XCTAssertThrowsError(of: ObjectDecodingError.self) { | ||
try await client.mutate(Objects.Mutation.auth(selection: selection)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import XCTest | ||
|
||
/// Checks whether the provided `body` throws an `error` of the given `error`'s type | ||
func XCTAssertThrowsError<T: Swift.Error, Output>( | ||
of: T.Type, | ||
file: StaticString = #file, | ||
line: UInt = #line, | ||
_ body: () async throws -> Output | ||
) async { | ||
do { | ||
_ = try await body() | ||
XCTFail("body completed successfully", file: file, line: line) | ||
} catch let error { | ||
XCTAssertNotNil(error as? T, "Expected error of \(T.self), got \(type(of: error))") | ||
} | ||
} |
Oops, something went wrong.