1package graphql_test 2 3import ( 4 "encoding/json" 5 "reflect" 6 "testing" 7 8 "github.com/dennwc/graphql" 9 "github.com/dennwc/graphql/gqlerrors" 10 "github.com/dennwc/graphql/language/ast" 11 "github.com/dennwc/graphql/language/location" 12 "github.com/dennwc/graphql/testutil" 13) 14 15var testComplexScalar *graphql.Scalar = graphql.NewScalar(graphql.ScalarConfig{ 16 Name: "ComplexScalar", 17 Serialize: func(value interface{}) interface{} { 18 if value == "DeserializedValue" { 19 return "SerializedValue" 20 } 21 return nil 22 }, 23 ParseValue: func(value interface{}) interface{} { 24 if value == "SerializedValue" { 25 return "DeserializedValue" 26 } 27 return nil 28 }, 29 ParseLiteral: func(valueAST ast.Value) interface{} { 30 astValue := valueAST.GetValue() 31 if astValue, ok := astValue.(string); ok && astValue == "SerializedValue" { 32 return "DeserializedValue" 33 } 34 return nil 35 }, 36}) 37 38var testInputObject *graphql.InputObject = graphql.NewInputObject(graphql.InputObjectConfig{ 39 Name: "TestInputObject", 40 Fields: graphql.InputObjectConfigFieldMap{ 41 "a": &graphql.InputObjectFieldConfig{ 42 Type: graphql.String, 43 }, 44 "b": &graphql.InputObjectFieldConfig{ 45 Type: graphql.NewList(graphql.String), 46 }, 47 "c": &graphql.InputObjectFieldConfig{ 48 Type: graphql.NewNonNull(graphql.String), 49 }, 50 "d": &graphql.InputObjectFieldConfig{ 51 Type: testComplexScalar, 52 }, 53 }, 54}) 55 56var testNestedInputObject *graphql.InputObject = graphql.NewInputObject(graphql.InputObjectConfig{ 57 Name: "TestNestedInputObject", 58 Fields: graphql.InputObjectConfigFieldMap{ 59 "na": &graphql.InputObjectFieldConfig{ 60 Type: graphql.NewNonNull(testInputObject), 61 }, 62 "nb": &graphql.InputObjectFieldConfig{ 63 Type: graphql.NewNonNull(graphql.String), 64 }, 65 }, 66}) 67 68func inputResolved(p graphql.ResolveParams) (interface{}, error) { 69 input, ok := p.Args["input"] 70 if !ok { 71 return nil, nil 72 } 73 b, err := json.Marshal(input) 74 if err != nil { 75 return nil, nil 76 } 77 return string(b), nil 78} 79 80var testType *graphql.Object = graphql.NewObject(graphql.ObjectConfig{ 81 Name: "TestType", 82 Fields: graphql.Fields{ 83 "fieldWithObjectInput": &graphql.Field{ 84 Type: graphql.String, 85 Args: graphql.FieldConfigArgument{ 86 "input": &graphql.ArgumentConfig{ 87 Type: testInputObject, 88 }, 89 }, 90 Resolve: inputResolved, 91 }, 92 "fieldWithNullableStringInput": &graphql.Field{ 93 Type: graphql.String, 94 Args: graphql.FieldConfigArgument{ 95 "input": &graphql.ArgumentConfig{ 96 Type: graphql.String, 97 }, 98 }, 99 Resolve: inputResolved, 100 }, 101 "fieldWithNonNullableStringInput": &graphql.Field{ 102 Type: graphql.String, 103 Args: graphql.FieldConfigArgument{ 104 "input": &graphql.ArgumentConfig{ 105 Type: graphql.NewNonNull(graphql.String), 106 }, 107 }, 108 Resolve: inputResolved, 109 }, 110 "fieldWithDefaultArgumentValue": &graphql.Field{ 111 Type: graphql.String, 112 Args: graphql.FieldConfigArgument{ 113 "input": &graphql.ArgumentConfig{ 114 Type: graphql.String, 115 DefaultValue: "Hello World", 116 }, 117 }, 118 Resolve: inputResolved, 119 }, 120 "fieldWithNestedInputObject": &graphql.Field{ 121 Type: graphql.String, 122 Args: graphql.FieldConfigArgument{ 123 "input": &graphql.ArgumentConfig{ 124 Type: testNestedInputObject, 125 DefaultValue: "Hello World", 126 }, 127 }, 128 Resolve: inputResolved, 129 }, 130 "list": &graphql.Field{ 131 Type: graphql.String, 132 Args: graphql.FieldConfigArgument{ 133 "input": &graphql.ArgumentConfig{ 134 Type: graphql.NewList(graphql.String), 135 }, 136 }, 137 Resolve: inputResolved, 138 }, 139 "nnList": &graphql.Field{ 140 Type: graphql.String, 141 Args: graphql.FieldConfigArgument{ 142 "input": &graphql.ArgumentConfig{ 143 Type: graphql.NewNonNull(graphql.NewList(graphql.String)), 144 }, 145 }, 146 Resolve: inputResolved, 147 }, 148 "listNN": &graphql.Field{ 149 Type: graphql.String, 150 Args: graphql.FieldConfigArgument{ 151 "input": &graphql.ArgumentConfig{ 152 Type: graphql.NewList(graphql.NewNonNull(graphql.String)), 153 }, 154 }, 155 Resolve: inputResolved, 156 }, 157 "nnListNN": &graphql.Field{ 158 Type: graphql.String, 159 Args: graphql.FieldConfigArgument{ 160 "input": &graphql.ArgumentConfig{ 161 Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(graphql.String))), 162 }, 163 }, 164 Resolve: inputResolved, 165 }, 166 }, 167}) 168 169var variablesTestSchema, _ = graphql.NewSchema(graphql.SchemaConfig{ 170 Query: testType, 171}) 172 173func TestVariables_ObjectsAndNullability_UsingInlineStructs_ExecutesWithComplexInput(t *testing.T) { 174 doc := ` 175 { 176 fieldWithObjectInput(input: {a: "foo", b: ["bar"], c: "baz"}) 177 } 178 ` 179 expected := &graphql.Result{ 180 Data: map[string]interface{}{ 181 "fieldWithObjectInput": `{"a":"foo","b":["bar"],"c":"baz"}`, 182 }, 183 } 184 // parse query 185 ast := testutil.TestParse(t, doc) 186 187 // execute 188 ep := graphql.ExecuteParams{ 189 Schema: variablesTestSchema, 190 AST: ast, 191 } 192 result := testutil.TestExecute(t, ep) 193 if len(result.Errors) > 0 { 194 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 195 } 196 if !reflect.DeepEqual(expected, result) { 197 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 198 } 199} 200func TestVariables_ObjectsAndNullability_UsingInlineStructs_ProperlyParsesSingleValueToList(t *testing.T) { 201 doc := ` 202 { 203 fieldWithObjectInput(input: {a: "foo", b: "bar", c: "baz"}) 204 } 205 ` 206 expected := &graphql.Result{ 207 Data: map[string]interface{}{ 208 "fieldWithObjectInput": `{"a":"foo","b":["bar"],"c":"baz"}`, 209 }, 210 } 211 // parse query 212 ast := testutil.TestParse(t, doc) 213 214 // execute 215 ep := graphql.ExecuteParams{ 216 Schema: variablesTestSchema, 217 AST: ast, 218 } 219 result := testutil.TestExecute(t, ep) 220 if len(result.Errors) > 0 { 221 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 222 } 223 if !reflect.DeepEqual(expected, result) { 224 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 225 } 226} 227func TestVariables_ObjectsAndNullability_UsingInlineStructs_DoesNotUseIncorrectValue(t *testing.T) { 228 doc := ` 229 { 230 fieldWithObjectInput(input: ["foo", "bar", "baz"]) 231 } 232 ` 233 expected := &graphql.Result{ 234 Data: map[string]interface{}{ 235 "fieldWithObjectInput": nil, 236 }, 237 } 238 // parse query 239 ast := testutil.TestParse(t, doc) 240 241 // execute 242 ep := graphql.ExecuteParams{ 243 Schema: variablesTestSchema, 244 AST: ast, 245 } 246 result := testutil.TestExecute(t, ep) 247 if len(result.Errors) > 0 { 248 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 249 } 250 if !reflect.DeepEqual(expected, result) { 251 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 252 } 253} 254func TestVariables_ObjectsAndNullability_UsingInlineStructs_ProperlyRunsParseLiteralOnComplexScalarTypes(t *testing.T) { 255 doc := ` 256 { 257 fieldWithObjectInput(input: {a: "foo", d: "SerializedValue"}) 258 } 259 ` 260 expected := &graphql.Result{ 261 Data: map[string]interface{}{ 262 "fieldWithObjectInput": `{"a":"foo","d":"DeserializedValue"}`, 263 }, 264 } 265 // parse query 266 ast := testutil.TestParse(t, doc) 267 268 // execute 269 ep := graphql.ExecuteParams{ 270 Schema: variablesTestSchema, 271 AST: ast, 272 } 273 result := testutil.TestExecute(t, ep) 274 if len(result.Errors) > 0 { 275 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 276 } 277 if !reflect.DeepEqual(expected, result) { 278 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 279 } 280} 281 282func testVariables_ObjectsAndNullability_UsingVariables_GetAST(t *testing.T) *ast.Document { 283 doc := ` 284 query q($input: TestInputObject) { 285 fieldWithObjectInput(input: $input) 286 } 287 ` 288 return testutil.TestParse(t, doc) 289} 290func TestVariables_ObjectsAndNullability_UsingVariables_ExecutesWithComplexInput(t *testing.T) { 291 292 params := map[string]interface{}{ 293 "input": map[string]interface{}{ 294 "a": "foo", 295 "b": []interface{}{"bar"}, 296 "c": "baz", 297 }, 298 } 299 expected := &graphql.Result{ 300 Data: map[string]interface{}{ 301 "fieldWithObjectInput": `{"a":"foo","b":["bar"],"c":"baz"}`, 302 }, 303 } 304 305 ast := testVariables_ObjectsAndNullability_UsingVariables_GetAST(t) 306 307 // execute 308 ep := graphql.ExecuteParams{ 309 Schema: variablesTestSchema, 310 AST: ast, 311 Args: params, 312 } 313 result := testutil.TestExecute(t, ep) 314 if len(result.Errors) > 0 { 315 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 316 } 317 if !reflect.DeepEqual(expected, result) { 318 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 319 } 320} 321 322func TestVariables_ObjectsAndNullability_UsingVariables_UsesDefaultValueWhenNotProvided(t *testing.T) { 323 324 doc := ` 325 query q($input: TestInputObject = {a: "foo", b: ["bar"], c: "baz"}) { 326 fieldWithObjectInput(input: $input) 327 } 328 ` 329 expected := &graphql.Result{ 330 Data: map[string]interface{}{ 331 "fieldWithObjectInput": `{"a":"foo","b":["bar"],"c":"baz"}`, 332 }, 333 } 334 335 withDefaultsAST := testutil.TestParse(t, doc) 336 337 // execute 338 ep := graphql.ExecuteParams{ 339 Schema: variablesTestSchema, 340 AST: withDefaultsAST, 341 } 342 result := testutil.TestExecute(t, ep) 343 if len(result.Errors) > 0 { 344 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 345 } 346 if !reflect.DeepEqual(expected, result) { 347 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 348 } 349} 350func TestVariables_ObjectsAndNullability_UsingVariables_ProperlyParsesSingleValueToList(t *testing.T) { 351 params := map[string]interface{}{ 352 "input": map[string]interface{}{ 353 "a": "foo", 354 "b": "bar", 355 "c": "baz", 356 }, 357 } 358 expected := &graphql.Result{ 359 Data: map[string]interface{}{ 360 "fieldWithObjectInput": `{"a":"foo","b":["bar"],"c":"baz"}`, 361 }, 362 } 363 364 ast := testVariables_ObjectsAndNullability_UsingVariables_GetAST(t) 365 366 // execute 367 ep := graphql.ExecuteParams{ 368 Schema: variablesTestSchema, 369 AST: ast, 370 Args: params, 371 } 372 result := testutil.TestExecute(t, ep) 373 if len(result.Errors) > 0 { 374 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 375 } 376 if !reflect.DeepEqual(expected, result) { 377 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 378 } 379} 380func TestVariables_ObjectsAndNullability_UsingVariables_ExecutesWithComplexScalarInput(t *testing.T) { 381 params := map[string]interface{}{ 382 "input": map[string]interface{}{ 383 "c": "foo", 384 "d": "SerializedValue", 385 }, 386 } 387 expected := &graphql.Result{ 388 Data: map[string]interface{}{ 389 "fieldWithObjectInput": `{"c":"foo","d":"DeserializedValue"}`, 390 }, 391 } 392 393 ast := testVariables_ObjectsAndNullability_UsingVariables_GetAST(t) 394 395 // execute 396 ep := graphql.ExecuteParams{ 397 Schema: variablesTestSchema, 398 AST: ast, 399 Args: params, 400 } 401 result := testutil.TestExecute(t, ep) 402 if len(result.Errors) > 0 { 403 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 404 } 405 if !reflect.DeepEqual(expected, result) { 406 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 407 } 408} 409func TestVariables_ObjectsAndNullability_UsingVariables_ErrorsOnNullForNestedNonNull(t *testing.T) { 410 params := map[string]interface{}{ 411 "input": map[string]interface{}{ 412 "a": "foo", 413 "b": "bar", 414 "c": nil, 415 }, 416 } 417 expected := &graphql.Result{ 418 Data: nil, 419 Errors: []gqlerrors.FormattedError{ 420 { 421 Message: `Variable "$input" got invalid value {"a":"foo","b":"bar","c":null}.` + 422 "\nIn field \"c\": Expected \"String!\", found null.", 423 Locations: []location.SourceLocation{ 424 { 425 Line: 2, Column: 17, 426 }, 427 }, 428 }, 429 }, 430 } 431 432 ast := testVariables_ObjectsAndNullability_UsingVariables_GetAST(t) 433 434 // execute 435 ep := graphql.ExecuteParams{ 436 Schema: variablesTestSchema, 437 AST: ast, 438 Args: params, 439 } 440 result := testutil.TestExecute(t, ep) 441 if len(result.Errors) != len(expected.Errors) { 442 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors)) 443 } 444 if !reflect.DeepEqual(expected, result) { 445 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 446 } 447} 448func TestVariables_ObjectsAndNullability_UsingVariables_ErrorsOnIncorrectType(t *testing.T) { 449 params := map[string]interface{}{ 450 "input": "foo bar", 451 } 452 expected := &graphql.Result{ 453 Data: nil, 454 Errors: []gqlerrors.FormattedError{ 455 { 456 Message: "Variable \"$input\" got invalid value \"foo bar\".\nExpected \"TestInputObject\", found not an object.", 457 Locations: []location.SourceLocation{ 458 { 459 Line: 2, Column: 17, 460 }, 461 }, 462 }, 463 }, 464 } 465 466 ast := testVariables_ObjectsAndNullability_UsingVariables_GetAST(t) 467 468 // execute 469 ep := graphql.ExecuteParams{ 470 Schema: variablesTestSchema, 471 AST: ast, 472 Args: params, 473 } 474 result := testutil.TestExecute(t, ep) 475 if len(result.Errors) != len(expected.Errors) { 476 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors)) 477 } 478 if !reflect.DeepEqual(expected, result) { 479 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 480 } 481} 482func TestVariables_ObjectsAndNullability_UsingVariables_ErrorsOnOmissionOfNestedNonNull(t *testing.T) { 483 params := map[string]interface{}{ 484 "input": map[string]interface{}{ 485 "a": "foo", 486 "b": "bar", 487 }, 488 } 489 expected := &graphql.Result{ 490 Data: nil, 491 Errors: []gqlerrors.FormattedError{ 492 { 493 Message: `Variable "$input" got invalid value {"a":"foo","b":"bar"}.` + 494 "\nIn field \"c\": Expected \"String!\", found null.", 495 Locations: []location.SourceLocation{ 496 { 497 Line: 2, Column: 17, 498 }, 499 }, 500 }, 501 }, 502 } 503 504 ast := testVariables_ObjectsAndNullability_UsingVariables_GetAST(t) 505 506 // execute 507 ep := graphql.ExecuteParams{ 508 Schema: variablesTestSchema, 509 AST: ast, 510 Args: params, 511 } 512 result := testutil.TestExecute(t, ep) 513 if len(result.Errors) != len(expected.Errors) { 514 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors)) 515 } 516 if !reflect.DeepEqual(expected, result) { 517 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 518 } 519} 520func TestVariables_ObjectsAndNullability_UsingVariables_ErrorsOnDeepNestedErrorsAndWithManyErrors(t *testing.T) { 521 params := map[string]interface{}{ 522 "input": map[string]interface{}{ 523 "na": map[string]interface{}{ 524 "a": "foo", 525 }, 526 }, 527 } 528 expected := &graphql.Result{ 529 Data: nil, 530 Errors: []gqlerrors.FormattedError{ 531 { 532 Message: `Variable "$input" got invalid value {"na":{"a":"foo"}}.` + 533 "\nIn field \"na\": In field \"c\": Expected \"String!\", found null." + 534 "\nIn field \"nb\": Expected \"String!\", found null.", 535 Locations: []location.SourceLocation{ 536 { 537 Line: 2, Column: 19, 538 }, 539 }, 540 }, 541 }, 542 } 543 doc := ` 544 query q($input: TestNestedInputObject) { 545 fieldWithNestedObjectInput(input: $input) 546 } 547 ` 548 549 nestedAST := testutil.TestParse(t, doc) 550 551 // execute 552 ep := graphql.ExecuteParams{ 553 Schema: variablesTestSchema, 554 AST: nestedAST, 555 Args: params, 556 } 557 result := testutil.TestExecute(t, ep) 558 if len(result.Errors) != len(expected.Errors) { 559 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors)) 560 } 561 if !reflect.DeepEqual(expected, result) { 562 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 563 } 564} 565func TestVariables_ObjectsAndNullability_UsingVariables_ErrorsOnAdditionOfUnknownInputField(t *testing.T) { 566 params := map[string]interface{}{ 567 "input": map[string]interface{}{ 568 "a": "foo", 569 "b": "bar", 570 "c": "baz", 571 "extra": "dog", 572 }, 573 } 574 expected := &graphql.Result{ 575 Data: nil, 576 Errors: []gqlerrors.FormattedError{ 577 { 578 Message: `Variable "$input" got invalid value {"a":"foo","b":"bar","c":"baz","extra":"dog"}.` + 579 "\nIn field \"extra\": Unknown field.", 580 Locations: []location.SourceLocation{ 581 { 582 Line: 2, Column: 17, 583 }, 584 }, 585 }, 586 }, 587 } 588 589 ast := testVariables_ObjectsAndNullability_UsingVariables_GetAST(t) 590 591 // execute 592 ep := graphql.ExecuteParams{ 593 Schema: variablesTestSchema, 594 AST: ast, 595 Args: params, 596 } 597 result := testutil.TestExecute(t, ep) 598 if len(result.Errors) != len(expected.Errors) { 599 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors)) 600 } 601 if !reflect.DeepEqual(expected, result) { 602 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 603 } 604} 605 606func TestVariables_NullableScalars_AllowsNullableInputsToBeOmitted(t *testing.T) { 607 doc := ` 608 { 609 fieldWithNullableStringInput 610 } 611 ` 612 expected := &graphql.Result{ 613 Data: map[string]interface{}{ 614 "fieldWithNullableStringInput": nil, 615 }, 616 } 617 618 ast := testutil.TestParse(t, doc) 619 620 // execute 621 ep := graphql.ExecuteParams{ 622 Schema: variablesTestSchema, 623 AST: ast, 624 } 625 result := testutil.TestExecute(t, ep) 626 if len(result.Errors) > 0 { 627 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 628 } 629 if !reflect.DeepEqual(expected, result) { 630 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 631 } 632} 633func TestVariables_NullableScalars_AllowsNullableInputsToBeOmittedInAVariable(t *testing.T) { 634 doc := ` 635 query SetsNullable($value: String) { 636 fieldWithNullableStringInput(input: $value) 637 } 638 ` 639 expected := &graphql.Result{ 640 Data: map[string]interface{}{ 641 "fieldWithNullableStringInput": nil, 642 }, 643 } 644 645 ast := testutil.TestParse(t, doc) 646 647 // execute 648 ep := graphql.ExecuteParams{ 649 Schema: variablesTestSchema, 650 AST: ast, 651 } 652 result := testutil.TestExecute(t, ep) 653 if len(result.Errors) > 0 { 654 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 655 } 656 if !reflect.DeepEqual(expected, result) { 657 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 658 } 659} 660func TestVariables_NullableScalars_AllowsNullableInputsToBeOmittedInAnUnlistedVariable(t *testing.T) { 661 doc := ` 662 query SetsNullable { 663 fieldWithNullableStringInput(input: $value) 664 } 665 ` 666 expected := &graphql.Result{ 667 Data: map[string]interface{}{ 668 "fieldWithNullableStringInput": nil, 669 }, 670 } 671 672 ast := testutil.TestParse(t, doc) 673 674 // execute 675 ep := graphql.ExecuteParams{ 676 Schema: variablesTestSchema, 677 AST: ast, 678 } 679 result := testutil.TestExecute(t, ep) 680 if len(result.Errors) > 0 { 681 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 682 } 683 if !reflect.DeepEqual(expected, result) { 684 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 685 } 686} 687func TestVariables_NullableScalars_AllowsNullableInputsToBeSetToNullInAVariable(t *testing.T) { 688 doc := ` 689 query SetsNullable($value: String) { 690 fieldWithNullableStringInput(input: $value) 691 } 692 ` 693 params := map[string]interface{}{ 694 "value": nil, 695 } 696 expected := &graphql.Result{ 697 Data: map[string]interface{}{ 698 "fieldWithNullableStringInput": nil, 699 }, 700 } 701 702 ast := testutil.TestParse(t, doc) 703 704 // execute 705 ep := graphql.ExecuteParams{ 706 Schema: variablesTestSchema, 707 AST: ast, 708 Args: params, 709 } 710 result := testutil.TestExecute(t, ep) 711 if len(result.Errors) > 0 { 712 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 713 } 714 if !reflect.DeepEqual(expected, result) { 715 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 716 } 717} 718func TestVariables_NullableScalars_AllowsNullableInputsToBeSetToAValueInAVariable(t *testing.T) { 719 doc := ` 720 query SetsNullable($value: String) { 721 fieldWithNullableStringInput(input: $value) 722 } 723 ` 724 params := map[string]interface{}{ 725 "value": "a", 726 } 727 expected := &graphql.Result{ 728 Data: map[string]interface{}{ 729 "fieldWithNullableStringInput": `"a"`, 730 }, 731 } 732 733 ast := testutil.TestParse(t, doc) 734 735 // execute 736 ep := graphql.ExecuteParams{ 737 Schema: variablesTestSchema, 738 AST: ast, 739 Args: params, 740 } 741 result := testutil.TestExecute(t, ep) 742 if len(result.Errors) > 0 { 743 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 744 } 745 if !reflect.DeepEqual(expected, result) { 746 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 747 } 748} 749func TestVariables_NullableScalars_AllowsNullableInputsToBeSetToAValueDirectly(t *testing.T) { 750 doc := ` 751 { 752 fieldWithNullableStringInput(input: "a") 753 } 754 ` 755 expected := &graphql.Result{ 756 Data: map[string]interface{}{ 757 "fieldWithNullableStringInput": `"a"`, 758 }, 759 } 760 761 ast := testutil.TestParse(t, doc) 762 763 // execute 764 ep := graphql.ExecuteParams{ 765 Schema: variablesTestSchema, 766 AST: ast, 767 } 768 result := testutil.TestExecute(t, ep) 769 if len(result.Errors) > 0 { 770 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 771 } 772 if !reflect.DeepEqual(expected, result) { 773 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 774 } 775} 776 777func TestVariables_NonNullableScalars_DoesNotAllowNonNullableInputsToBeOmittedInAVariable(t *testing.T) { 778 779 doc := ` 780 query SetsNonNullable($value: String!) { 781 fieldWithNonNullableStringInput(input: $value) 782 } 783 ` 784 785 expected := &graphql.Result{ 786 Data: nil, 787 Errors: []gqlerrors.FormattedError{ 788 { 789 Message: `Variable "$value" of required type "String!" was not provided.`, 790 Locations: []location.SourceLocation{ 791 { 792 Line: 2, Column: 31, 793 }, 794 }, 795 }, 796 }, 797 } 798 799 ast := testutil.TestParse(t, doc) 800 801 // execute 802 ep := graphql.ExecuteParams{ 803 Schema: variablesTestSchema, 804 AST: ast, 805 } 806 result := testutil.TestExecute(t, ep) 807 if len(result.Errors) != len(expected.Errors) { 808 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors)) 809 } 810 if !reflect.DeepEqual(expected, result) { 811 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 812 } 813} 814func TestVariables_NonNullableScalars_DoesNotAllowNonNullableInputsToBeSetToNullInAVariable(t *testing.T) { 815 doc := ` 816 query SetsNonNullable($value: String!) { 817 fieldWithNonNullableStringInput(input: $value) 818 } 819 ` 820 821 params := map[string]interface{}{ 822 "value": nil, 823 } 824 expected := &graphql.Result{ 825 Data: nil, 826 Errors: []gqlerrors.FormattedError{ 827 { 828 Message: `Variable "$value" of required type "String!" was not provided.`, 829 Locations: []location.SourceLocation{ 830 { 831 Line: 2, Column: 31, 832 }, 833 }, 834 }, 835 }, 836 } 837 838 ast := testutil.TestParse(t, doc) 839 840 // execute 841 ep := graphql.ExecuteParams{ 842 Schema: variablesTestSchema, 843 AST: ast, 844 Args: params, 845 } 846 result := testutil.TestExecute(t, ep) 847 if len(result.Errors) != len(expected.Errors) { 848 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors)) 849 } 850 if !reflect.DeepEqual(expected, result) { 851 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 852 } 853} 854func TestVariables_NonNullableScalars_AllowsNonNullableInputsToBeSetToAValueInAVariable(t *testing.T) { 855 doc := ` 856 query SetsNonNullable($value: String!) { 857 fieldWithNonNullableStringInput(input: $value) 858 } 859 ` 860 861 params := map[string]interface{}{ 862 "value": "a", 863 } 864 expected := &graphql.Result{ 865 Data: map[string]interface{}{ 866 "fieldWithNonNullableStringInput": `"a"`, 867 }, 868 } 869 870 ast := testutil.TestParse(t, doc) 871 872 // execute 873 ep := graphql.ExecuteParams{ 874 Schema: variablesTestSchema, 875 AST: ast, 876 Args: params, 877 } 878 result := testutil.TestExecute(t, ep) 879 if len(result.Errors) > 0 { 880 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 881 } 882 if !reflect.DeepEqual(expected, result) { 883 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 884 } 885} 886func TestVariables_NonNullableScalars_AllowsNonNullableInputsToBeSetToAValueDirectly(t *testing.T) { 887 doc := ` 888 { 889 fieldWithNonNullableStringInput(input: "a") 890 } 891 ` 892 893 params := map[string]interface{}{ 894 "value": "a", 895 } 896 897 expected := &graphql.Result{ 898 Data: map[string]interface{}{ 899 "fieldWithNonNullableStringInput": `"a"`, 900 }, 901 } 902 903 ast := testutil.TestParse(t, doc) 904 905 // execute 906 ep := graphql.ExecuteParams{ 907 Schema: variablesTestSchema, 908 AST: ast, 909 Args: params, 910 } 911 result := testutil.TestExecute(t, ep) 912 if len(result.Errors) > 0 { 913 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 914 } 915 if !reflect.DeepEqual(expected, result) { 916 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 917 } 918} 919func TestVariables_NonNullableScalars_PassesAlongNullForNonNullableInputsIfExplicitlySetInTheQuery(t *testing.T) { 920 doc := ` 921 { 922 fieldWithNonNullableStringInput 923 } 924 ` 925 926 params := map[string]interface{}{ 927 "value": "a", 928 } 929 930 expected := &graphql.Result{ 931 Data: map[string]interface{}{ 932 "fieldWithNonNullableStringInput": nil, 933 }, 934 } 935 936 ast := testutil.TestParse(t, doc) 937 938 // execute 939 ep := graphql.ExecuteParams{ 940 Schema: variablesTestSchema, 941 AST: ast, 942 Args: params, 943 } 944 result := testutil.TestExecute(t, ep) 945 if len(result.Errors) > 0 { 946 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 947 } 948 if !reflect.DeepEqual(expected, result) { 949 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 950 } 951} 952 953func TestVariables_ListsAndNullability_AllowsListsToBeNull(t *testing.T) { 954 doc := ` 955 query q($input: [String]) { 956 list(input: $input) 957 } 958 ` 959 params := map[string]interface{}{ 960 "input": nil, 961 } 962 963 expected := &graphql.Result{ 964 Data: map[string]interface{}{ 965 "list": nil, 966 }, 967 } 968 ast := testutil.TestParse(t, doc) 969 970 // execute 971 ep := graphql.ExecuteParams{ 972 Schema: variablesTestSchema, 973 AST: ast, 974 Args: params, 975 } 976 result := testutil.TestExecute(t, ep) 977 if len(result.Errors) > 0 { 978 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 979 } 980 if !reflect.DeepEqual(expected, result) { 981 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 982 } 983} 984func TestVariables_ListsAndNullability_AllowsListsToContainValues(t *testing.T) { 985 doc := ` 986 query q($input: [String]) { 987 list(input: $input) 988 } 989 ` 990 params := map[string]interface{}{ 991 "input": []interface{}{"A"}, 992 } 993 994 expected := &graphql.Result{ 995 Data: map[string]interface{}{ 996 "list": `["A"]`, 997 }, 998 } 999 ast := testutil.TestParse(t, doc) 1000 1001 // execute 1002 ep := graphql.ExecuteParams{ 1003 Schema: variablesTestSchema, 1004 AST: ast, 1005 Args: params, 1006 } 1007 result := testutil.TestExecute(t, ep) 1008 if len(result.Errors) > 0 { 1009 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 1010 } 1011 if !reflect.DeepEqual(expected, result) { 1012 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 1013 } 1014} 1015func TestVariables_ListsAndNullability_AllowsListsToContainNull(t *testing.T) { 1016 doc := ` 1017 query q($input: [String]) { 1018 list(input: $input) 1019 } 1020 ` 1021 params := map[string]interface{}{ 1022 "input": []interface{}{"A", nil, "B"}, 1023 } 1024 1025 expected := &graphql.Result{ 1026 Data: map[string]interface{}{ 1027 "list": `["A",null,"B"]`, 1028 }, 1029 } 1030 ast := testutil.TestParse(t, doc) 1031 1032 // execute 1033 ep := graphql.ExecuteParams{ 1034 Schema: variablesTestSchema, 1035 AST: ast, 1036 Args: params, 1037 } 1038 result := testutil.TestExecute(t, ep) 1039 if len(result.Errors) > 0 { 1040 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 1041 } 1042 if !reflect.DeepEqual(expected, result) { 1043 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 1044 } 1045} 1046func TestVariables_ListsAndNullability_DoesNotAllowNonNullListsToBeNull(t *testing.T) { 1047 doc := ` 1048 query q($input: [String]!) { 1049 nnList(input: $input) 1050 } 1051 ` 1052 expected := &graphql.Result{ 1053 Data: nil, 1054 Errors: []gqlerrors.FormattedError{ 1055 { 1056 Message: `Variable "$input" of required type "[String]!" was not provided.`, 1057 Locations: []location.SourceLocation{ 1058 { 1059 Line: 2, Column: 17, 1060 }, 1061 }, 1062 }, 1063 }, 1064 } 1065 ast := testutil.TestParse(t, doc) 1066 1067 // execute 1068 ep := graphql.ExecuteParams{ 1069 Schema: variablesTestSchema, 1070 AST: ast, 1071 } 1072 result := testutil.TestExecute(t, ep) 1073 if len(result.Errors) != len(expected.Errors) { 1074 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors)) 1075 } 1076 if !reflect.DeepEqual(expected, result) { 1077 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 1078 } 1079} 1080func TestVariables_ListsAndNullability_AllowsNonNullListsToContainValues(t *testing.T) { 1081 doc := ` 1082 query q($input: [String]!) { 1083 nnList(input: $input) 1084 } 1085 ` 1086 params := map[string]interface{}{ 1087 "input": []interface{}{"A"}, 1088 } 1089 expected := &graphql.Result{ 1090 Data: map[string]interface{}{ 1091 "nnList": `["A"]`, 1092 }, 1093 } 1094 ast := testutil.TestParse(t, doc) 1095 1096 // execute 1097 ep := graphql.ExecuteParams{ 1098 Schema: variablesTestSchema, 1099 AST: ast, 1100 Args: params, 1101 } 1102 result := testutil.TestExecute(t, ep) 1103 if len(result.Errors) > 0 { 1104 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 1105 } 1106 if !reflect.DeepEqual(expected, result) { 1107 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 1108 } 1109} 1110func TestVariables_ListsAndNullability_AllowsNonNullListsToContainNull(t *testing.T) { 1111 doc := ` 1112 query q($input: [String]!) { 1113 nnList(input: $input) 1114 } 1115 ` 1116 params := map[string]interface{}{ 1117 "input": []interface{}{"A", nil, "B"}, 1118 } 1119 expected := &graphql.Result{ 1120 Data: map[string]interface{}{ 1121 "nnList": `["A",null,"B"]`, 1122 }, 1123 } 1124 ast := testutil.TestParse(t, doc) 1125 1126 // execute 1127 ep := graphql.ExecuteParams{ 1128 Schema: variablesTestSchema, 1129 AST: ast, 1130 Args: params, 1131 } 1132 result := testutil.TestExecute(t, ep) 1133 if len(result.Errors) > 0 { 1134 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 1135 } 1136 if !reflect.DeepEqual(expected, result) { 1137 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 1138 } 1139} 1140func TestVariables_ListsAndNullability_AllowsListsOfNonNullsToBeNull(t *testing.T) { 1141 doc := ` 1142 query q($input: [String!]) { 1143 listNN(input: $input) 1144 } 1145 ` 1146 params := map[string]interface{}{ 1147 "input": nil, 1148 } 1149 expected := &graphql.Result{ 1150 Data: map[string]interface{}{ 1151 "listNN": nil, 1152 }, 1153 } 1154 ast := testutil.TestParse(t, doc) 1155 1156 // execute 1157 ep := graphql.ExecuteParams{ 1158 Schema: variablesTestSchema, 1159 AST: ast, 1160 Args: params, 1161 } 1162 result := testutil.TestExecute(t, ep) 1163 if len(result.Errors) > 0 { 1164 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 1165 } 1166 if !reflect.DeepEqual(expected, result) { 1167 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 1168 } 1169} 1170func TestVariables_ListsAndNullability_AllowsListsOfNonNullsToContainValues(t *testing.T) { 1171 doc := ` 1172 query q($input: [String!]) { 1173 listNN(input: $input) 1174 } 1175 ` 1176 params := map[string]interface{}{ 1177 "input": []interface{}{"A"}, 1178 } 1179 expected := &graphql.Result{ 1180 Data: map[string]interface{}{ 1181 "listNN": `["A"]`, 1182 }, 1183 } 1184 ast := testutil.TestParse(t, doc) 1185 1186 // execute 1187 ep := graphql.ExecuteParams{ 1188 Schema: variablesTestSchema, 1189 AST: ast, 1190 Args: params, 1191 } 1192 result := testutil.TestExecute(t, ep) 1193 if len(result.Errors) > 0 { 1194 t.Fatalf("wrong result, unexpected errors: %v", result.Errors) 1195 } 1196 if !reflect.DeepEqual(expected, result) { 1197 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 1198 } 1199} 1200func TestVariables_ListsAndNullability_DoesNotAllowListOfNonNullsToContainNull(t *testing.T) { 1201 doc := ` 1202 query q($input: [String!]) { 1203 listNN(input: $input) 1204 } 1205 ` 1206 params := map[string]interface{}{ 1207 "input": []interface{}{"A", nil, "B"}, 1208 } 1209 expected := &graphql.Result{ 1210 Data: nil, 1211 Errors: []gqlerrors.FormattedError{ 1212 { 1213 Message: `Variable "$input" got invalid value ` + 1214 `["A",null,"B"].` + 1215 "\nIn element #1: Expected \"String!\", found null.", 1216 Locations: []location.SourceLocation{ 1217 { 1218 Line: 2, Column: 17, 1219 }, 1220 }, 1221 }, 1222 }, 1223 } 1224 ast := testutil.TestParse(t, doc) 1225 1226 // execute 1227 ep := graphql.ExecuteParams{ 1228 Schema: variablesTestSchema, 1229 AST: ast, 1230 Args: params, 1231 } 1232 result := testutil.TestExecute(t, ep) 1233 if len(result.Errors) != len(expected.Errors) { 1234 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors)) 1235 } 1236 if !reflect.DeepEqual(expected, result) { 1237 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 1238 } 1239} 1240func TestVariables_ListsAndNullability_DoesNotAllowNonNullListOfNonNullsToBeNull(t *testing.T) { 1241 doc := ` 1242 query q($input: [String!]!) { 1243 nnListNN(input: $input) 1244 } 1245 ` 1246 params := map[string]interface{}{ 1247 "input": nil, 1248 } 1249 expected := &graphql.Result{ 1250 Data: nil, 1251 Errors: []gqlerrors.FormattedError{ 1252 { 1253 Message: `Variable "$input" of required type "[String!]!" was not provided.`, 1254 Locations: []location.SourceLocation{ 1255 { 1256 Line: 2, Column: 17, 1257 }, 1258 }, 1259 }, 1260 }, 1261 } 1262 ast := testutil.TestParse(t, doc) 1263 1264 // execute 1265 ep := graphql.ExecuteParams{ 1266 Schema: variablesTestSchema, 1267 AST: ast, 1268 Args: params, 1269 } 1270 result := testutil.TestExecute(t, ep) 1271 if len(result.Errors) != len(expected.Errors) { 1272 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors)) 1273 } 1274 if !reflect.DeepEqual(expected, result) { 1275 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 1276 } 1277} 1278func TestVariables_ListsAndNullability_AllowsNonNullListsOfNonNulsToContainValues(t *testing.T) { 1279 doc := ` 1280 query q($input: [String!]!) { 1281 nnListNN(input: $input) 1282 } 1283 ` 1284 params := map[string]interface{}{ 1285 "input": []interface{}{"A"}, 1286 } 1287 expected := &graphql.Result{ 1288 Data: map[string]interface{}{ 1289 "nnListNN": `["A"]`, 1290 }, 1291 } 1292 ast := testutil.TestParse(t, doc) 1293 1294 // execute 1295 ep := graphql.ExecuteParams{ 1296 Schema: variablesTestSchema, 1297 AST: ast, 1298 Args: params, 1299 } 1300 result := testutil.TestExecute(t, ep) 1301 if len(result.Errors) != len(expected.Errors) { 1302 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors)) 1303 } 1304 if !reflect.DeepEqual(expected, result) { 1305 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 1306 } 1307} 1308func TestVariables_ListsAndNullability_DoesNotAllowNonNullListOfNonNullsToContainNull(t *testing.T) { 1309 doc := ` 1310 query q($input: [String!]!) { 1311 nnListNN(input: $input) 1312 } 1313 ` 1314 params := map[string]interface{}{ 1315 "input": []interface{}{"A", nil, "B"}, 1316 } 1317 expected := &graphql.Result{ 1318 Data: nil, 1319 Errors: []gqlerrors.FormattedError{ 1320 { 1321 Message: `Variable "$input" got invalid value ` + 1322 `["A",null,"B"].` + 1323 "\nIn element #1: Expected \"String!\", found null.", 1324 Locations: []location.SourceLocation{ 1325 { 1326 Line: 2, Column: 17, 1327 }, 1328 }, 1329 }, 1330 }, 1331 } 1332 ast := testutil.TestParse(t, doc) 1333 1334 // execute 1335 ep := graphql.ExecuteParams{ 1336 Schema: variablesTestSchema, 1337 AST: ast, 1338 Args: params, 1339 } 1340 result := testutil.TestExecute(t, ep) 1341 if len(result.Errors) != len(expected.Errors) { 1342 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors)) 1343 } 1344 if !reflect.DeepEqual(expected, result) { 1345 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 1346 } 1347} 1348func TestVariables_ListsAndNullability_DoesNotAllowInvalidTypesToBeUsedAsValues(t *testing.T) { 1349 doc := ` 1350 query q($input: TestType!) { 1351 fieldWithObjectInput(input: $input) 1352 } 1353 ` 1354 params := map[string]interface{}{ 1355 "input": map[string]interface{}{ 1356 "list": []interface{}{"A", "B"}, 1357 }, 1358 } 1359 expected := &graphql.Result{ 1360 Data: nil, 1361 Errors: []gqlerrors.FormattedError{ 1362 { 1363 Message: `Variable "$input" expected value of type "TestType!" which cannot be used as an input type.`, 1364 Locations: []location.SourceLocation{ 1365 { 1366 Line: 2, Column: 17, 1367 }, 1368 }, 1369 }, 1370 }, 1371 } 1372 ast := testutil.TestParse(t, doc) 1373 1374 // execute 1375 ep := graphql.ExecuteParams{ 1376 Schema: variablesTestSchema, 1377 AST: ast, 1378 Args: params, 1379 } 1380 result := testutil.TestExecute(t, ep) 1381 if len(result.Errors) != len(expected.Errors) { 1382 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors)) 1383 } 1384 if !reflect.DeepEqual(expected, result) { 1385 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 1386 } 1387} 1388func TestVariables_ListsAndNullability_DoesNotAllowUnknownTypesToBeUsedAsValues(t *testing.T) { 1389 doc := ` 1390 query q($input: UnknownType!) { 1391 fieldWithObjectInput(input: $input) 1392 } 1393 ` 1394 params := map[string]interface{}{ 1395 "input": "whoknows", 1396 } 1397 expected := &graphql.Result{ 1398 Data: nil, 1399 Errors: []gqlerrors.FormattedError{ 1400 { 1401 Message: `Variable "$input" expected value of type "UnknownType!" which cannot be used as an input type.`, 1402 Locations: []location.SourceLocation{ 1403 { 1404 Line: 2, Column: 17, 1405 }, 1406 }, 1407 }, 1408 }, 1409 } 1410 ast := testutil.TestParse(t, doc) 1411 1412 // execute 1413 ep := graphql.ExecuteParams{ 1414 Schema: variablesTestSchema, 1415 AST: ast, 1416 Args: params, 1417 } 1418 result := testutil.TestExecute(t, ep) 1419 if len(result.Errors) != len(expected.Errors) { 1420 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors)) 1421 } 1422 if !reflect.DeepEqual(expected, result) { 1423 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 1424 } 1425} 1426 1427func TestVariables_UsesArgumentDefaultValues_WhenNoArgumentProvided(t *testing.T) { 1428 doc := ` 1429 { 1430 fieldWithDefaultArgumentValue 1431 } 1432 ` 1433 expected := &graphql.Result{ 1434 Data: map[string]interface{}{ 1435 "fieldWithDefaultArgumentValue": `"Hello World"`, 1436 }, 1437 } 1438 ast := testutil.TestParse(t, doc) 1439 1440 // execute 1441 ep := graphql.ExecuteParams{ 1442 Schema: variablesTestSchema, 1443 AST: ast, 1444 } 1445 result := testutil.TestExecute(t, ep) 1446 if len(result.Errors) != len(expected.Errors) { 1447 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors)) 1448 } 1449 if !reflect.DeepEqual(expected, result) { 1450 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 1451 } 1452} 1453func TestVariables_UsesArgumentDefaultValues_WhenNullableVariableProvided(t *testing.T) { 1454 doc := ` 1455 query optionalVariable($optional: String) { 1456 fieldWithDefaultArgumentValue(input: $optional) 1457 } 1458 ` 1459 expected := &graphql.Result{ 1460 Data: map[string]interface{}{ 1461 "fieldWithDefaultArgumentValue": `"Hello World"`, 1462 }, 1463 } 1464 ast := testutil.TestParse(t, doc) 1465 1466 // execute 1467 ep := graphql.ExecuteParams{ 1468 Schema: variablesTestSchema, 1469 AST: ast, 1470 } 1471 result := testutil.TestExecute(t, ep) 1472 if len(result.Errors) != len(expected.Errors) { 1473 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors)) 1474 } 1475 if !reflect.DeepEqual(expected, result) { 1476 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 1477 } 1478} 1479func TestVariables_UsesArgumentDefaultValues_WhenArgumentProvidedCannotBeParsed(t *testing.T) { 1480 doc := ` 1481 { 1482 fieldWithDefaultArgumentValue(input: WRONG_TYPE) 1483 } 1484 ` 1485 expected := &graphql.Result{ 1486 Data: map[string]interface{}{ 1487 "fieldWithDefaultArgumentValue": `"Hello World"`, 1488 }, 1489 } 1490 ast := testutil.TestParse(t, doc) 1491 1492 // execute 1493 ep := graphql.ExecuteParams{ 1494 Schema: variablesTestSchema, 1495 AST: ast, 1496 } 1497 result := testutil.TestExecute(t, ep) 1498 if len(result.Errors) != len(expected.Errors) { 1499 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors)) 1500 } 1501 if !reflect.DeepEqual(expected, result) { 1502 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result)) 1503 } 1504} 1505