|
| 1 | +/* |
| 2 | + * Copyright 2016 Palantir Technologies, Inc. All rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +package graphql.execution.batched |
| 6 | + |
| 7 | +import graphql.ExecutionResult |
| 8 | +import graphql.GraphQL |
| 9 | +import graphql.execution.SimpleExecutionStrategy |
| 10 | +import graphql.schema.GraphQLSchema |
| 11 | +import spock.lang.Specification |
| 12 | + |
| 13 | +import java.util.concurrent.atomic.AtomicInteger |
| 14 | + |
| 15 | +/** |
| 16 | + * Created by dcohen on 1/4/16. |
| 17 | + */ |
| 18 | +class GraphqlExecutionSpec extends Specification { |
| 19 | + |
| 20 | + private GraphQLSchema schema = new FunWithStringsSchemaFactory().createSchema(); |
| 21 | + private GraphQL graphQLSimple = new GraphQL(this.schema, new SimpleExecutionStrategy()); |
| 22 | + private GraphQL graphQLBatchedButUnbatched = new GraphQL(this.schema, new BatchedExecutionStrategy()); |
| 23 | + private Map<FunWithStringsSchemaFactory.CallType, AtomicInteger> countMap = new HashMap<>(); |
| 24 | + private GraphQL graphQLBatchedValue = new GraphQL(FunWithStringsSchemaFactory.createBatched(countMap).createSchema(), new BatchedExecutionStrategy()); |
| 25 | + |
| 26 | + private Map<String, Object> nullValueMap = new HashMap<>(); |
| 27 | + |
| 28 | + def setup() { |
| 29 | + nullValueMap.put("value", null); |
| 30 | + } |
| 31 | + |
| 32 | + // Split into sub-methods so the stack trace is more useful |
| 33 | + private void runTest(String query, Map<String, Object> expected) { |
| 34 | + runTestSimple(query, expected); |
| 35 | + runTestBatchingUnbatched(query, expected); |
| 36 | + runTestBatching(query, expected); |
| 37 | + } |
| 38 | + |
| 39 | + private void runTestBatchingUnbatched(String query, Map<String, Object> expected) { |
| 40 | + assert expected == this.graphQLBatchedButUnbatched.execute(query).getData(); |
| 41 | + } |
| 42 | + |
| 43 | + private void runTestBatching(String query, Map<String, Object> expected) { |
| 44 | + assert expected == this.graphQLBatchedValue.execute(query).getData(); |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + private void runTestSimple(String query, Map<String, Object> expected) { |
| 49 | + assert expected == this.graphQLSimple.execute(query).getData(); |
| 50 | + } |
| 51 | + |
| 52 | + // This method is agnostic to whether errors are returned or thrown, provided they contain the desired text |
| 53 | + private void runTestExpectError(String query, String errorSubstring) { |
| 54 | + |
| 55 | + try { |
| 56 | + ExecutionResult result = this.graphQLSimple.execute(query); |
| 57 | + assert !result.getErrors().isEmpty(), "Simple should have errored but was: " + result.getData(); |
| 58 | + } catch (Exception e) { |
| 59 | + assert e.getMessage().contains(errorSubstring), "Simple error must contain '" + errorSubstring + "'"; |
| 60 | + } |
| 61 | + |
| 62 | + try { |
| 63 | + ExecutionResult result = this.graphQLBatchedButUnbatched.execute(query); |
| 64 | + assert !result.getErrors().isEmpty(), "Batched should have errored, but was " + result.getData(); |
| 65 | + } catch (Exception e) { |
| 66 | + assert e.getMessage().contains(errorSubstring), "Batched but unbatched error must contain '" + errorSubstring + "'"; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private Map<String, Object> mapOf(String firstKey, Object firstVal, Object... more) { |
| 71 | + Map<String, Object> retVal = new HashMap<>(); |
| 72 | + retVal.put(firstKey, firstVal); |
| 73 | + for (int i = 0; i < more.length; i += 2) { |
| 74 | + retVal.put((String) more[i], more[i+1]); |
| 75 | + } |
| 76 | + return retVal; |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + def "Basic case works"() { |
| 81 | + given: |
| 82 | + String query = "{ string(value: \"Basic\"){value, nonNullValue, veryNonNullValue} }"; |
| 83 | + |
| 84 | + Map<String, Object> expected = mapOf("string", mapOf("value", "Basic", "nonNullValue", "Basic", "veryNonNullValue", "Basic")); |
| 85 | + |
| 86 | + expect: |
| 87 | + runTest(query, expected); |
| 88 | + } |
| 89 | + |
| 90 | + def "Empty input"() { |
| 91 | + given: |
| 92 | + String query = "{ string(value: \"\"){value} }"; |
| 93 | + |
| 94 | + Map<String, Object> expected = mapOf("string", mapOf("value", "")); |
| 95 | + |
| 96 | + expect: |
| 97 | + runTest(query, expected); |
| 98 | + } |
| 99 | + |
| 100 | + def "Handles implicit null input"() { |
| 101 | + given: |
| 102 | + String query = "{ string{value} }"; |
| 103 | + |
| 104 | + Map<String, Object> expected = new HashMap<>(); |
| 105 | + expected.put("string", null); |
| 106 | + |
| 107 | + expect: |
| 108 | + runTest(query, expected); |
| 109 | + } |
| 110 | + |
| 111 | + def "Handles explict null input"() { |
| 112 | + given: |
| 113 | + String query = "{ string(value: \"null\"){value} }"; |
| 114 | + |
| 115 | + Map<String, Object> expected = mapOf("string", nullValueMap); |
| 116 | + |
| 117 | + expect: |
| 118 | + runTest(query, expected); |
| 119 | + } |
| 120 | + |
| 121 | + def "Shatter works"() { |
| 122 | + given: |
| 123 | + String query = "{ string(value: \"Shatter\"){shatter{value}} }"; |
| 124 | + |
| 125 | + Map<String, Object> expected = mapOf( |
| 126 | + "string", mapOf("shatter", Arrays.asList( |
| 127 | + mapOf("value", "S"), |
| 128 | + mapOf("value", "h"), |
| 129 | + mapOf("value", "a"), |
| 130 | + mapOf("value", "t"), |
| 131 | + mapOf("value", "t"), |
| 132 | + mapOf("value", "e"), |
| 133 | + mapOf("value", "r") |
| 134 | + ))); |
| 135 | + |
| 136 | + expect: |
| 137 | + runTest(query, expected); |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | + def "Shatter then append"() { |
| 142 | + given: |
| 143 | + String query = |
| 144 | + "{ string(value: \"Sh\") { shatter { append(text: \"1\") { value } } } }"; |
| 145 | + |
| 146 | + Map<String, Object> expected = mapOf( |
| 147 | + "string", mapOf("shatter", Arrays.asList( |
| 148 | + mapOf("append", mapOf("value", "S1")), |
| 149 | + mapOf("append", mapOf("value", "h1")) |
| 150 | + ))); |
| 151 | + |
| 152 | + expect: |
| 153 | + runTest(query, expected); |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | + def "Legal null entries in lists"() { |
| 159 | + given: |
| 160 | + String query = |
| 161 | + "{ " + |
| 162 | + "string(value: \"Sh\") {" + |
| 163 | + "shatter { " + |
| 164 | + "append(text: \"1\") {" + |
| 165 | + "split(regex: \"h\") {" + |
| 166 | + "value " + |
| 167 | + "} " + |
| 168 | + "} " + |
| 169 | + "} " + |
| 170 | + "} " + |
| 171 | + "}"; |
| 172 | + |
| 173 | + Map<String, Object> expected = mapOf( |
| 174 | + "string", mapOf("shatter", Arrays.asList( |
| 175 | + mapOf("append", mapOf("split", Arrays.asList( |
| 176 | + mapOf("value", "S1")))), |
| 177 | + mapOf("append", mapOf("split", Arrays.asList( |
| 178 | + null, mapOf("value", "1")))) |
| 179 | + ))); |
| 180 | + |
| 181 | + expect: |
| 182 | + runTest(query, expected); |
| 183 | + |
| 184 | + } |
| 185 | + |
| 186 | + def "Legal null values for entire lists"() { |
| 187 | + |
| 188 | + given: |
| 189 | + String query = |
| 190 | + "{ " + |
| 191 | + "string(value: \"Sh\") {" + |
| 192 | + "shatter { " + |
| 193 | + "append(text: \"1\") {" + |
| 194 | + "split {" + |
| 195 | + "value " + |
| 196 | + "} " + |
| 197 | + "} " + |
| 198 | + "} " + |
| 199 | + "} " + |
| 200 | + "}"; |
| 201 | + |
| 202 | + Map<String, Object> nullSplit = new HashMap<>(); |
| 203 | + nullSplit.put("split", null); |
| 204 | + |
| 205 | + Map<String, Object> expected = mapOf( |
| 206 | + "string", mapOf("shatter", Arrays.asList( |
| 207 | + mapOf("append", nullSplit), |
| 208 | + mapOf("append", nullSplit) |
| 209 | + ))); |
| 210 | + |
| 211 | + expect: |
| 212 | + runTest(query, expected); |
| 213 | + |
| 214 | + } |
| 215 | + |
| 216 | + |
| 217 | + def "Legal null values for primitives"() { |
| 218 | + |
| 219 | + given: |
| 220 | + String query = |
| 221 | + "{ " + |
| 222 | + "string(value: \"Shxnull\") {" + |
| 223 | + "split(regex: \"x\") {" + |
| 224 | + "value " + |
| 225 | + "} " + |
| 226 | + "} " + |
| 227 | + "}"; |
| 228 | + |
| 229 | + Map<String, Object> expected = mapOf( |
| 230 | + "string", mapOf("split", Arrays.asList( |
| 231 | + mapOf("value", "Sh"), |
| 232 | + nullValueMap |
| 233 | + ))); |
| 234 | + |
| 235 | + expect: |
| 236 | + runTest(query, expected); |
| 237 | + |
| 238 | + } |
| 239 | + |
| 240 | + def "Illegal null value for primitives"() { |
| 241 | + |
| 242 | + given: |
| 243 | + String query = |
| 244 | + "{ " + |
| 245 | + "string(value: \"Shxnull\") {" + |
| 246 | + "split(regex: \"x\") {" + |
| 247 | + "nonNullValue " + |
| 248 | + "} " + |
| 249 | + "} " + |
| 250 | + "}"; |
| 251 | + |
| 252 | + expect: |
| 253 | + runTestExpectError(query, "non-null"); |
| 254 | + } |
| 255 | + |
| 256 | + |
| 257 | + def "Illegal null value for objects"() { |
| 258 | + given: |
| 259 | + String query = |
| 260 | + "{ " + |
| 261 | + "string(value: \"\") {" + |
| 262 | + "shatter { " + |
| 263 | + "value " + |
| 264 | + "} " + |
| 265 | + "} " + |
| 266 | + "}"; |
| 267 | + |
| 268 | + expect: |
| 269 | + runTestExpectError(query, "non-null"); |
| 270 | + |
| 271 | + } |
| 272 | + |
| 273 | + |
| 274 | + def "Illegal null value for nested non-null field"() { |
| 275 | + given: |
| 276 | + String query = |
| 277 | + "{ " + |
| 278 | + "string(value: \"Shxnull\") {" + |
| 279 | + "split(regex: \"x\") {" + |
| 280 | + "veryNonNullValue " + |
| 281 | + "} " + |
| 282 | + "} " + |
| 283 | + "}"; |
| 284 | + |
| 285 | + expect: |
| 286 | + runTestExpectError(query, "non-null"); |
| 287 | + } |
| 288 | + |
| 289 | + |
| 290 | + def "Illegal null value for an object in a list"() { |
| 291 | + given: |
| 292 | + String query = |
| 293 | + "{ " + |
| 294 | + "string(value: \"Sh\") {" + |
| 295 | + "shatter { " + |
| 296 | + "append(text: \"1\") {" + |
| 297 | + "splitNonNull(regex: \"h\") {" + |
| 298 | + "value " + |
| 299 | + "} " + |
| 300 | + "} " + |
| 301 | + "} " + |
| 302 | + "} " + |
| 303 | + "}"; |
| 304 | + |
| 305 | + expect: |
| 306 | + runTestExpectError(query, "non-null"); |
| 307 | + |
| 308 | + } |
| 309 | + |
| 310 | + |
| 311 | + |
| 312 | + def "Nested lists"() { |
| 313 | + given: |
| 314 | + String query = |
| 315 | + "{ " + |
| 316 | + "string(value: \"List of words\") {" + |
| 317 | + "wordsAndLetters { " + |
| 318 | + " value " + |
| 319 | + "} " + |
| 320 | + "} " + |
| 321 | + "}"; |
| 322 | + |
| 323 | + Map<String, Object> expected = mapOf( |
| 324 | + "string", mapOf("wordsAndLetters", Arrays.asList( |
| 325 | + Arrays.asList( |
| 326 | + mapOf("value", "L"), |
| 327 | + mapOf("value", "i"), |
| 328 | + mapOf("value", "s"), |
| 329 | + mapOf("value", "t")), |
| 330 | + Arrays.asList( |
| 331 | + mapOf("value", "o"), |
| 332 | + mapOf("value", "f")), |
| 333 | + Arrays.asList( |
| 334 | + mapOf("value", "w"), |
| 335 | + mapOf("value", "o"), |
| 336 | + mapOf("value", "r"), |
| 337 | + mapOf("value", "d"), |
| 338 | + mapOf("value", "s")) |
| 339 | + |
| 340 | + ))); |
| 341 | + |
| 342 | + expect: |
| 343 | + runTest(query, expected); |
| 344 | + |
| 345 | + } |
| 346 | + |
| 347 | + |
| 348 | + def "Batching works"() { |
| 349 | + given: |
| 350 | + String query = |
| 351 | + "{ " + |
| 352 | + "string(value: \"Batch\") {" + |
| 353 | + "shatter { " + |
| 354 | + "append(text: \"1\") {" + |
| 355 | + "split(regex: \"h\") {" + |
| 356 | + "value " + |
| 357 | + "} " + |
| 358 | + "} " + |
| 359 | + "} " + |
| 360 | + "} " + |
| 361 | + "}"; |
| 362 | + |
| 363 | + Map<String, Object> expected = mapOf( |
| 364 | + "string", mapOf("shatter", Arrays.asList( |
| 365 | + mapOf("append", mapOf("split", Arrays.asList( |
| 366 | + mapOf("value", "B1")))), |
| 367 | + mapOf("append", mapOf("split", Arrays.asList( |
| 368 | + mapOf("value", "a1")))), |
| 369 | + mapOf("append", mapOf("split", Arrays.asList( |
| 370 | + mapOf("value", "t1")))), |
| 371 | + mapOf("append", mapOf("split", Arrays.asList( |
| 372 | + mapOf("value", "c1")))), |
| 373 | + mapOf("append", mapOf("split", Arrays.asList( |
| 374 | + null, mapOf("value", "1")))) |
| 375 | + ))); |
| 376 | + |
| 377 | + expect: |
| 378 | + runTest(query, expected); |
| 379 | + |
| 380 | + 1 == this.countMap.get(FunWithStringsSchemaFactory.CallType.VALUE).get(); |
| 381 | + 1 == this.countMap.get(FunWithStringsSchemaFactory.CallType.SHATTER).get(); |
| 382 | + 1 == this.countMap.get(FunWithStringsSchemaFactory.CallType.APPEND).get(); |
| 383 | + 1 == this.countMap.get(FunWithStringsSchemaFactory.CallType.SPLIT).get(); |
| 384 | + } |
| 385 | + |
| 386 | + |
| 387 | + |
| 388 | + |
| 389 | +} |
0 commit comments