|
| 1 | +import GraphQL |
| 2 | +import SwiftGraphQLClient |
| 3 | +import XCTest |
| 4 | +import Combine |
| 5 | +import SwiftGraphQL |
| 6 | + |
| 7 | +final class AsyncInterfaceTests: XCTestCase { |
| 8 | + func testAsyncSelectionQueryReturnsValue() async throws { |
| 9 | + let selection = Selection<String, Objects.User> { |
| 10 | + try $0.id() |
| 11 | + } |
| 12 | + |
| 13 | + let client = MockClient(customExecute: { operation in |
| 14 | + let id = GraphQLField.leaf(field: "id", parent: "User", arguments: []) |
| 15 | + |
| 16 | + let user = GraphQLField.composite( |
| 17 | + field: "user", |
| 18 | + parent: "Query", |
| 19 | + type: "User", |
| 20 | + arguments: [], |
| 21 | + selection: selection.__selection() |
| 22 | + ) |
| 23 | + |
| 24 | + let result = OperationResult( |
| 25 | + operation: operation, |
| 26 | + data: [ |
| 27 | + user.alias!: [ |
| 28 | + id.alias!: "123" |
| 29 | + ] |
| 30 | + ], |
| 31 | + error: nil |
| 32 | + ) |
| 33 | + return Just(result).eraseToAnyPublisher() |
| 34 | + }) |
| 35 | + |
| 36 | + |
| 37 | + let result = try await client.query(Objects.Query.user(selection: selection)) |
| 38 | + XCTAssertEqual(result.data, "123") |
| 39 | + } |
| 40 | + |
| 41 | + func testAsyncSelectionQueryThrowsError() async throws { |
| 42 | + let selection = Selection<String, Objects.User> { |
| 43 | + try $0.id() |
| 44 | + } |
| 45 | + |
| 46 | + let client = MockClient(customExecute: { operation in |
| 47 | + let result = OperationResult( |
| 48 | + operation: operation, |
| 49 | + data: ["unknown_field": "123"], |
| 50 | + error: nil |
| 51 | + ) |
| 52 | + return Just(result).eraseToAnyPublisher() |
| 53 | + }) |
| 54 | + |
| 55 | + await XCTAssertThrowsError(of: ObjectDecodingError.self) { |
| 56 | + try await client.query(Objects.Query.user(selection: selection)) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + func testAsyncSelectionMutationReturnsValue() async throws { |
| 61 | + let selection = Selection.AuthPayload<String?> { |
| 62 | + try $0.on( |
| 63 | + authPayloadSuccess: Selection.AuthPayloadSuccess<String?> { |
| 64 | + try $0.token() |
| 65 | + }, |
| 66 | + authPayloadFailure: Selection.AuthPayloadFailure<String?> { _ in |
| 67 | + nil |
| 68 | + } |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | + let client = MockClient(customExecute: { operation in |
| 73 | + let token = GraphQLField.leaf(field: "token", parent: "AuthPayloadSuccess", arguments: []) |
| 74 | + |
| 75 | + let auth = GraphQLField.composite( |
| 76 | + field: "auth", |
| 77 | + parent: "Mutation", |
| 78 | + type: "AuthPayload", |
| 79 | + arguments: [], |
| 80 | + selection: selection.__selection() |
| 81 | + ) |
| 82 | + |
| 83 | + let result = OperationResult( |
| 84 | + operation: operation, |
| 85 | + data: [ |
| 86 | + auth.alias!: [ |
| 87 | + "__typename": "AuthPayloadSuccess", |
| 88 | + token.alias!: "123" |
| 89 | + ] |
| 90 | + ], |
| 91 | + error: nil |
| 92 | + ) |
| 93 | + return Just(result).eraseToAnyPublisher() |
| 94 | + }) |
| 95 | + |
| 96 | + let result = try await client.mutate(Objects.Mutation.auth(selection: selection)) |
| 97 | + XCTAssertEqual(result.data, "123") |
| 98 | + } |
| 99 | + |
| 100 | + func testAsyncSelectionMutationThrowsError() async throws { |
| 101 | + let selection = Selection.AuthPayload<String?> { |
| 102 | + try $0.on( |
| 103 | + authPayloadSuccess: Selection.AuthPayloadSuccess<String?> { |
| 104 | + try $0.token() |
| 105 | + }, |
| 106 | + authPayloadFailure: Selection.AuthPayloadFailure<String?> { _ in |
| 107 | + nil |
| 108 | + } |
| 109 | + ) |
| 110 | + } |
| 111 | + |
| 112 | + let client = MockClient(customExecute: { operation in |
| 113 | + let result = OperationResult( |
| 114 | + operation: operation, |
| 115 | + data: ["unknown_field": "123"], |
| 116 | + error: nil |
| 117 | + ) |
| 118 | + return Just(result).eraseToAnyPublisher() |
| 119 | + }) |
| 120 | + |
| 121 | + await XCTAssertThrowsError(of: ObjectDecodingError.self) { |
| 122 | + try await client.mutate(Objects.Mutation.auth(selection: selection)) |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments