|
| 1 | +import Foundation |
| 2 | +import Graphiti |
| 3 | +import GraphQL |
| 4 | +import NIO |
| 5 | +import XCTest |
| 6 | + |
| 7 | +final class FederationOnlySchemaTests: XCTestCase { |
| 8 | + private var group: MultiThreadedEventLoopGroup! |
| 9 | + private var api: FederationOnlyAPI! |
| 10 | + |
| 11 | + struct Profile: Codable { |
| 12 | + let name: String |
| 13 | + let email: String? |
| 14 | + } |
| 15 | + |
| 16 | + struct User: Codable { |
| 17 | + let id: String |
| 18 | + |
| 19 | + func profile(context: NoContext, args: NoArguments) async throws -> Profile { |
| 20 | + if id == "1" { |
| 21 | + return Profile(name: "User \(id)", email: nil) |
| 22 | + } else { |
| 23 | + return Profile(name: "User \(id)", email: "\(id)@example.com") |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + struct Key: Codable { |
| 28 | + let id: String |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + struct FederationOnlyResolver { |
| 33 | + func user(context: NoContext, key: User.Key) async throws -> User { |
| 34 | + User(id: key.id) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + struct FederationOnlyAPI: API { |
| 39 | + var resolver: FederationOnlyResolver |
| 40 | + var schema: Schema<FederationOnlyResolver, NoContext> |
| 41 | + } |
| 42 | + |
| 43 | + static let federatedSDL: String = |
| 44 | + """ |
| 45 | + type User @key(fields: "id") { |
| 46 | + id: String! |
| 47 | + profile: Profile! |
| 48 | + } |
| 49 | +
|
| 50 | + type Profile { |
| 51 | + name: String! |
| 52 | + email: String |
| 53 | + } |
| 54 | + """ |
| 55 | + |
| 56 | + override func setUpWithError() throws { |
| 57 | + let schema = try SchemaBuilder(FederationOnlyResolver.self, NoContext.self) |
| 58 | + .setFederatedSDL(to: Self.federatedSDL) |
| 59 | + .add { |
| 60 | + Type(User.self) { |
| 61 | + Field("id", at: \.id) |
| 62 | + Field("profile", at: User.profile) |
| 63 | + } |
| 64 | + .key(at: FederationOnlyResolver.user) { |
| 65 | + Argument("id", at: \.id) |
| 66 | + } |
| 67 | + |
| 68 | + Type(Profile.self) { |
| 69 | + Field("name", at: \.name) |
| 70 | + Field("email", at: \.email) |
| 71 | + } |
| 72 | + } |
| 73 | + .build() |
| 74 | + group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount) |
| 75 | + api = FederationOnlyAPI(resolver: FederationOnlyResolver(), schema: schema) |
| 76 | + } |
| 77 | + |
| 78 | + override func tearDownWithError() throws { |
| 79 | + try group.syncShutdownGracefully() |
| 80 | + group = nil |
| 81 | + api = nil |
| 82 | + } |
| 83 | + |
| 84 | + func execute(request: String, variables: [String: Map] = [:]) throws -> GraphQLResult { |
| 85 | + try api |
| 86 | + .execute( |
| 87 | + request: request, |
| 88 | + context: NoContext(), |
| 89 | + on: group, |
| 90 | + variables: variables |
| 91 | + ) |
| 92 | + .wait() |
| 93 | + } |
| 94 | + |
| 95 | + func testUserFederationSimple() throws { |
| 96 | + let representations: [String: Map] = [ |
| 97 | + "representations": [ |
| 98 | + ["__typename": "User", "id": "1234"], |
| 99 | + ], |
| 100 | + ] |
| 101 | + |
| 102 | + let query = |
| 103 | + """ |
| 104 | + query user($representations: [_Any!]!) { |
| 105 | + _entities(representations: $representations) { |
| 106 | + ... on User { |
| 107 | + id |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + """ |
| 112 | + |
| 113 | + try XCTAssertEqual( |
| 114 | + execute(request: query, variables: representations), |
| 115 | + GraphQLResult(data: [ |
| 116 | + "_entities": [ |
| 117 | + [ |
| 118 | + "id": "1234" |
| 119 | + ] |
| 120 | + ] |
| 121 | + ]) |
| 122 | + ) |
| 123 | + } |
| 124 | + |
| 125 | + func testUserFederationNested() throws { |
| 126 | + let representations: [String: Map] = [ |
| 127 | + "representations": [ |
| 128 | + ["__typename": "User", "id": "1234"], |
| 129 | + ], |
| 130 | + ] |
| 131 | + |
| 132 | + let query = |
| 133 | + """ |
| 134 | + query user($representations: [_Any!]!) { |
| 135 | + _entities(representations: $representations) { |
| 136 | + ... on User { |
| 137 | + id |
| 138 | + profile { name, email } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + """ |
| 143 | + |
| 144 | + try XCTAssertEqual( |
| 145 | + execute(request: query, variables: representations), |
| 146 | + GraphQLResult(data: [ |
| 147 | + "_entities": [ |
| 148 | + [ |
| 149 | + "id": "1234", |
| 150 | + "profile": [ |
| 151 | + "name": "User 1234", |
| 152 | + |
| 153 | + ] |
| 154 | + ] |
| 155 | + ] |
| 156 | + ]) |
| 157 | + ) |
| 158 | + } |
| 159 | + |
| 160 | + func testUserFederationNestedOptional() throws { |
| 161 | + let representations: [String: Map] = [ |
| 162 | + "representations": [ |
| 163 | + ["__typename": "User", "id": "1"], |
| 164 | + ], |
| 165 | + ] |
| 166 | + |
| 167 | + let query = |
| 168 | + """ |
| 169 | + query user($representations: [_Any!]!) { |
| 170 | + _entities(representations: $representations) { |
| 171 | + ... on User { |
| 172 | + id |
| 173 | + profile { name, email } |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + """ |
| 178 | + |
| 179 | + try XCTAssertEqual( |
| 180 | + execute(request: query, variables: representations), |
| 181 | + GraphQLResult(data: [ |
| 182 | + "_entities": [ |
| 183 | + [ |
| 184 | + "id": "1", |
| 185 | + "profile": [ |
| 186 | + "name": "User 1", |
| 187 | + "email": .null |
| 188 | + ] |
| 189 | + ] |
| 190 | + ] |
| 191 | + ]) |
| 192 | + ) |
| 193 | + } |
| 194 | +} |
0 commit comments