|
| 1 | +import Graphiti |
| 2 | +import GraphQL |
| 3 | +import NIO |
| 4 | +import XCTest |
| 5 | + |
| 6 | +class DefaultValueTests: XCTestCase { |
| 7 | + let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 8 | + |
| 9 | + func testBoolDefault() async throws { |
| 10 | + XCTAssertEqual( |
| 11 | + try DefaultValueAPI().execute( |
| 12 | + request: """ |
| 13 | + { |
| 14 | + bool |
| 15 | + } |
| 16 | + """, |
| 17 | + context: NoContext(), |
| 18 | + on: eventLoopGroup |
| 19 | + ).wait(), |
| 20 | + .init(data: ["bool": true]) |
| 21 | + ) |
| 22 | + } |
| 23 | + |
| 24 | + func testIntDefault() async throws { |
| 25 | + XCTAssertEqual( |
| 26 | + try DefaultValueAPI().execute( |
| 27 | + request: """ |
| 28 | + { |
| 29 | + int |
| 30 | + } |
| 31 | + """, |
| 32 | + context: NoContext(), |
| 33 | + on: eventLoopGroup |
| 34 | + ).wait(), |
| 35 | + .init(data: ["int": 1]) |
| 36 | + ) |
| 37 | + } |
| 38 | + |
| 39 | + func testFloatDefault() async throws { |
| 40 | + XCTAssertEqual( |
| 41 | + try DefaultValueAPI().execute( |
| 42 | + request: """ |
| 43 | + { |
| 44 | + float |
| 45 | + } |
| 46 | + """, |
| 47 | + context: NoContext(), |
| 48 | + on: eventLoopGroup |
| 49 | + ).wait(), |
| 50 | + .init(data: ["float": 1.1]) |
| 51 | + ) |
| 52 | + } |
| 53 | + |
| 54 | + func testStringDefault() async throws { |
| 55 | + XCTAssertEqual( |
| 56 | + try DefaultValueAPI().execute( |
| 57 | + request: """ |
| 58 | + { |
| 59 | + string |
| 60 | + } |
| 61 | + """, |
| 62 | + context: NoContext(), |
| 63 | + on: eventLoopGroup |
| 64 | + ).wait(), |
| 65 | + .init(data: ["string": "hello"]) |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + func testEnumDefault() async throws { |
| 70 | + XCTAssertEqual( |
| 71 | + try DefaultValueAPI().execute( |
| 72 | + request: """ |
| 73 | + { |
| 74 | + enum |
| 75 | + } |
| 76 | + """, |
| 77 | + context: NoContext(), |
| 78 | + on: eventLoopGroup |
| 79 | + ).wait(), |
| 80 | + .init(data: ["enum": "valueA"]) |
| 81 | + ) |
| 82 | + } |
| 83 | + |
| 84 | + func testArrayDefault() async throws { |
| 85 | + XCTAssertEqual( |
| 86 | + try DefaultValueAPI().execute( |
| 87 | + request: """ |
| 88 | + { |
| 89 | + array |
| 90 | + } |
| 91 | + """, |
| 92 | + context: NoContext(), |
| 93 | + on: eventLoopGroup |
| 94 | + ).wait(), |
| 95 | + .init(data: ["array": ["a", "b", "c"]]) |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | + func testInputDefault() async throws { |
| 100 | + // Test input object argument default |
| 101 | + XCTAssertEqual( |
| 102 | + try DefaultValueAPI().execute( |
| 103 | + request: """ |
| 104 | + { |
| 105 | + input { |
| 106 | + bool |
| 107 | + int |
| 108 | + float |
| 109 | + string |
| 110 | + enum |
| 111 | + array |
| 112 | + } |
| 113 | + } |
| 114 | + """, |
| 115 | + context: NoContext(), |
| 116 | + on: eventLoopGroup |
| 117 | + ).wait(), |
| 118 | + .init(data: [ |
| 119 | + "input": [ |
| 120 | + "bool": true, |
| 121 | + "int": 1, |
| 122 | + "float": 1.1, |
| 123 | + "string": "hello", |
| 124 | + "enum": "valueA", |
| 125 | + "array": ["a", "b", "c"], |
| 126 | + ], |
| 127 | + ]) |
| 128 | + ) |
| 129 | + |
| 130 | + // Test input object field defaults |
| 131 | + XCTAssertEqual( |
| 132 | + try DefaultValueAPI().execute( |
| 133 | + request: """ |
| 134 | + { |
| 135 | + input(input: {bool: true}) { |
| 136 | + bool |
| 137 | + int |
| 138 | + float |
| 139 | + string |
| 140 | + enum |
| 141 | + array |
| 142 | + } |
| 143 | + } |
| 144 | + """, |
| 145 | + context: NoContext(), |
| 146 | + on: eventLoopGroup |
| 147 | + ).wait(), |
| 148 | + .init(data: [ |
| 149 | + "input": [ |
| 150 | + "bool": true, |
| 151 | + "int": 1, |
| 152 | + "float": 1.1, |
| 153 | + "string": "hello", |
| 154 | + "enum": "valueA", |
| 155 | + "array": ["a", "b", "c"], |
| 156 | + ], |
| 157 | + ]) |
| 158 | + ) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +struct DefaultValueAPI: API { |
| 163 | + typealias ContextType = NoContext |
| 164 | + struct Resolver { |
| 165 | + struct BoolArgs: Codable { |
| 166 | + let bool: Bool |
| 167 | + } |
| 168 | + |
| 169 | + func bool(context _: NoContext, arguments: BoolArgs) -> Bool { |
| 170 | + return arguments.bool |
| 171 | + } |
| 172 | + |
| 173 | + struct IntArgs: Codable { |
| 174 | + let int: Int |
| 175 | + } |
| 176 | + |
| 177 | + func int(context _: NoContext, arguments: IntArgs) -> Int { |
| 178 | + return arguments.int |
| 179 | + } |
| 180 | + |
| 181 | + struct FloatArgs: Codable { |
| 182 | + let float: Double |
| 183 | + } |
| 184 | + |
| 185 | + func float(context _: NoContext, arguments: FloatArgs) -> Double { |
| 186 | + return arguments.float |
| 187 | + } |
| 188 | + |
| 189 | + struct StringArgs: Codable { |
| 190 | + let string: String |
| 191 | + } |
| 192 | + |
| 193 | + func string(context _: NoContext, arguments: StringArgs) -> String { |
| 194 | + return arguments.string |
| 195 | + } |
| 196 | + |
| 197 | + struct EnumArgs: Codable { |
| 198 | + let `enum`: DefaultEnum |
| 199 | + } |
| 200 | + |
| 201 | + func `enum`(context _: NoContext, arguments: EnumArgs) -> DefaultEnum { |
| 202 | + return arguments.enum |
| 203 | + } |
| 204 | + |
| 205 | + struct ArrayArgs: Codable { |
| 206 | + let array: [String] |
| 207 | + } |
| 208 | + |
| 209 | + func array(context _: NoContext, arguments: ArrayArgs) -> [String] { |
| 210 | + return arguments.array |
| 211 | + } |
| 212 | + |
| 213 | + struct InputArgs: Codable { |
| 214 | + let input: DefaultInputType |
| 215 | + } |
| 216 | + |
| 217 | + func input(context _: NoContext, arguments: InputArgs) -> DefaultOutputType { |
| 218 | + return .init( |
| 219 | + bool: arguments.input.bool, |
| 220 | + int: arguments.input.int, |
| 221 | + float: arguments.input.float, |
| 222 | + string: arguments.input.string, |
| 223 | + enum: arguments.input.enum, |
| 224 | + array: arguments.input.array |
| 225 | + ) |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + let resolver = Resolver() |
| 230 | + |
| 231 | + let schema = try! Schema<Resolver, NoContext> { |
| 232 | + Enum(DefaultEnum.self) { |
| 233 | + Value(.valueA) |
| 234 | + Value(.valueB) |
| 235 | + } |
| 236 | + |
| 237 | + Input(DefaultInputType.self) { |
| 238 | + InputField("bool", at: \.bool).defaultValue(true) |
| 239 | + InputField("int", at: \.int).defaultValue(1) |
| 240 | + InputField("float", at: \.float).defaultValue(1.1) |
| 241 | + InputField("string", at: \.string).defaultValue("hello") |
| 242 | + InputField("enum", at: \.`enum`).defaultValue(.valueA) |
| 243 | + InputField("array", at: \.array).defaultValue(["a", "b", "c"]) |
| 244 | + } |
| 245 | + |
| 246 | + Type(DefaultOutputType.self) { |
| 247 | + Field("bool", at: \.bool) |
| 248 | + Field("int", at: \.int) |
| 249 | + Field("float", at: \.float) |
| 250 | + Field("string", at: \.string) |
| 251 | + Field("enum", at: \.`enum`) |
| 252 | + Field("array", at: \.array) |
| 253 | + } |
| 254 | + |
| 255 | + Query { |
| 256 | + Field("bool", at: Resolver.bool) { |
| 257 | + Argument("bool", at: \.bool).defaultValue(true) |
| 258 | + } |
| 259 | + Field("int", at: Resolver.int) { |
| 260 | + Argument("int", at: \.int).defaultValue(1) |
| 261 | + } |
| 262 | + Field("float", at: Resolver.float) { |
| 263 | + Argument("float", at: \.float).defaultValue(1.1) |
| 264 | + } |
| 265 | + Field("string", at: Resolver.string) { |
| 266 | + Argument("string", at: \.string).defaultValue("hello") |
| 267 | + } |
| 268 | + Field("enum", at: Resolver.enum) { |
| 269 | + Argument("enum", at: \.enum).defaultValue(.valueA) |
| 270 | + } |
| 271 | + Field("array", at: Resolver.array) { |
| 272 | + Argument("array", at: \.array).defaultValue(["a", "b", "c"]) |
| 273 | + } |
| 274 | + Field("input", at: Resolver.input) { |
| 275 | + Argument("input", at: \.input).defaultValue(.init( |
| 276 | + bool: true, |
| 277 | + int: 1, |
| 278 | + float: 1.1, |
| 279 | + string: "hello", |
| 280 | + enum: .valueA, |
| 281 | + array: ["a", "b", "c"] |
| 282 | + )) |
| 283 | + } |
| 284 | + } |
| 285 | + } |
| 286 | + |
| 287 | + enum DefaultEnum: String, Codable { |
| 288 | + case valueA |
| 289 | + case valueB |
| 290 | + } |
| 291 | + |
| 292 | + struct DefaultInputType: Codable { |
| 293 | + let bool: Bool |
| 294 | + let int: Int |
| 295 | + let float: Double |
| 296 | + let string: String |
| 297 | + let `enum`: DefaultEnum |
| 298 | + let array: [String] |
| 299 | + } |
| 300 | + |
| 301 | + struct DefaultOutputType: Codable { |
| 302 | + let bool: Bool |
| 303 | + let int: Int |
| 304 | + let float: Double |
| 305 | + let string: String |
| 306 | + let `enum`: DefaultEnum |
| 307 | + let array: [String] |
| 308 | + } |
| 309 | +} |
0 commit comments