1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
|
package schema_test
import (
"fmt"
"strings"
"testing"
"github.com/graph-gophers/graphql-go/ast"
"github.com/graph-gophers/graphql-go/internal/schema"
)
func TestParse(t *testing.T) {
for _, test := range []struct {
name string
sdl string
useStringDescriptions bool
validateError func(err error) error
validateSchema func(s *ast.Schema) error
}{
{
name: "Parses interface definition",
sdl: "interface Greeting { message: String! }",
validateSchema: func(s *ast.Schema) error {
const typeName = "Greeting"
typ, ok := s.Types[typeName].(*ast.InterfaceTypeDefinition)
if !ok {
return fmt.Errorf("interface %q not found", typeName)
}
if want, have := 1, len(typ.Fields); want != have {
return fmt.Errorf("invalid number of fields: want %d, have %d", want, have)
}
const fieldName = "message"
if typ.Fields[0].Name != fieldName {
return fmt.Errorf("field %q not found", fieldName)
}
return nil
},
},
{
name: "Parses implementing type without providing required fields",
sdl: `
interface Greeting {
message: String!
}
type Welcome implements Greeting {
id: ID!
}`,
validateError: func(err error) error {
if err == nil {
return fmt.Errorf("want error, have <nil>")
}
if want, have := `graphql: interface "Greeting" expects field "message" but "Welcome" does not provide it`, err.Error(); want != have {
return fmt.Errorf("unexpected error: want %q, have %q", want, have)
}
return nil
},
},
{
name: "Parses type with description string",
sdl: `
"Single line description."
type Type {
field: String
}`,
useStringDescriptions: true,
validateSchema: func(s *ast.Schema) error {
const typeName = "Type"
typ, ok := s.Types[typeName].(*ast.ObjectTypeDefinition)
if !ok {
return fmt.Errorf("type %q not found", typeName)
}
if want, have := "Single line description.", typ.Description(); want != have {
return fmt.Errorf("invalid description: want %q, have %q", want, have)
}
return nil
},
},
{
name: "Parses type with simple multi-line 'BlockString' description",
sdl: `
"""
Multi-line description.
"""
type Type {
field: String
}`,
useStringDescriptions: true,
validateSchema: func(s *ast.Schema) error {
const typeName = "Type"
typ, ok := s.Types[typeName].(*ast.ObjectTypeDefinition)
if !ok {
return fmt.Errorf("type %q not found", typeName)
}
if want, have := "Multi-line description.", typ.Description(); want != have {
return fmt.Errorf("invalid description: want %q, have %q", want, have)
}
return nil
},
},
{
name: "Parses type with empty multi-line 'BlockString' description",
sdl: `
"""
"""
type Type {
field: String
}`,
useStringDescriptions: true,
validateSchema: func(s *ast.Schema) error {
const typeName = "Type"
typ, ok := s.Types[typeName].(*ast.ObjectTypeDefinition)
if !ok {
return fmt.Errorf("type %q not found", typeName)
}
if want, have := "", typ.Description(); want != have {
return fmt.Errorf("invalid description: want %q, have %q", want, have)
}
return nil
},
},
{
name: "Parses type with multi-line 'BlockString' description",
sdl: `
"""
First line of the description.
Second line of the description.
query {
code {
example
}
}
Notes:
* First note
* Second note
"""
type Type {
field: String
}`,
useStringDescriptions: true,
validateSchema: func(s *ast.Schema) error {
const typeName = "Type"
typ, ok := s.Types[typeName].(*ast.ObjectTypeDefinition)
if !ok {
return fmt.Errorf("type %q not found", typeName)
}
want := "First line of the description.\n\nSecond line of the description.\n\n\tquery {\n\t\tcode {\n\t\t\texample\n\t\t}\n\t}\n\nNotes:\n\n * First note\n * Second note"
if have := typ.Description(); want != have {
return fmt.Errorf("invalid description: want %q, have %q", want, have)
}
return nil
},
},
{
name: "Parses type with un-indented multi-line 'BlockString' description",
sdl: `
"""
First line of the description.
Second line of the description.
"""
type Type {
field: String
}`,
useStringDescriptions: true,
validateSchema: func(s *ast.Schema) error {
const typeName = "Type"
typ, ok := s.Types[typeName].(*ast.ObjectTypeDefinition)
if !ok {
return fmt.Errorf("type %q not found", typeName)
}
want := "First line of the description.\n\nSecond line of the description."
if have := typ.Description(); want != have {
return fmt.Errorf("invalid description: want %q, have %q", want, have)
}
return nil
},
},
{
name: "Parses type with space-indented multi-line 'BlockString' description",
sdl: `
"""
First line of the description.
Second line of the description.
query {
code {
example
}
}
"""
type Type {
field: String
}`,
useStringDescriptions: true,
validateSchema: func(s *ast.Schema) error {
const typeName = "Type"
typ, ok := s.Types[typeName].(*ast.ObjectTypeDefinition)
if !ok {
return fmt.Errorf("type %q not found", typeName)
}
want := "First line of the description.\n\nSecond line of the description.\n\n query {\n code {\n example\n }\n }"
if have := typ.Description(); want != have {
return fmt.Errorf("invalid description: want %q, have %q", want, have)
}
return nil
},
},
{
name: "Parses type with multi-line 'BlockString' description and ignores comments",
sdl: `
"""
Multi-line description with ignored comments.
"""
# This comment should be ignored.
type Type {
field: String
}`,
useStringDescriptions: true,
validateSchema: func(s *ast.Schema) error {
const typeName = "Type"
typ, ok := s.Types[typeName].(*ast.ObjectTypeDefinition)
if !ok {
return fmt.Errorf("type %q not found", typeName)
}
if want, have := "Multi-line description with ignored comments.", typ.Description(); want != have {
return fmt.Errorf("invalid description: want %q, have %q", want, have)
}
return nil
},
},
{
name: "Parses type invalid syntax",
sdl: `
type U = T
`,
validateError: func(err error) error {
msg := `graphql: syntax error: unexpected "=", expecting "{" (line 2, column 11)`
if err == nil || err.Error() != msg {
return fmt.Errorf("expected error %q, but got %q", msg, err)
}
return nil
},
},
{
name: "Description is correctly parsed for non-described types",
sdl: `
"Some description."
scalar MyInt
type Type {
field: String
}`,
useStringDescriptions: true,
validateSchema: func(s *ast.Schema) error {
typ, ok := s.Types["Type"]
if !ok {
return fmt.Errorf("type %q not found", "Type")
}
if want, have := "", typ.Description(); want != have {
return fmt.Errorf("description does not match: want %q, have %q ", want, have)
}
return nil
},
},
{
name: "Multi-line comment is correctly parsed",
sdl: `
# Multi-line
# comment.
" This description should be ignored. "
scalar MyInt
type Type {
field: String
}`,
validateSchema: func(s *ast.Schema) error {
typ, ok := s.Types["MyInt"]
if !ok {
return fmt.Errorf("scalar %q not found", "MyInt")
}
if want, have := "Multi-line\ncomment.", typ.Description(); want != have {
return fmt.Errorf("description does not match: want %q, have %q ", want, have)
}
typ, ok = s.Types["Type"]
if !ok {
return fmt.Errorf("type %q not found", "Type")
}
if want, have := "", typ.Description(); want != have {
return fmt.Errorf("description does not match: want %q, have %q ", want, have)
}
return nil
},
},
{
name: "Default Root schema",
sdl: `
type Query {
hello: String!
}
type Mutation {
concat(a: String!, b: String!): String!
}
`,
validateSchema: func(s *ast.Schema) error {
typq, ok := s.Types["Query"].(*ast.ObjectTypeDefinition)
if !ok {
return fmt.Errorf("type %q not found", "Query")
}
helloField := typq.Fields.Get("hello")
if helloField == nil {
return fmt.Errorf("field %q not found", "hello")
}
if helloField.Type.String() != "String!" {
return fmt.Errorf("field %q has an invalid type: %q", "hello", helloField.Type.String())
}
typm, ok := s.Types["Mutation"].(*ast.ObjectTypeDefinition)
if !ok {
return fmt.Errorf("type %q not found", "Mutation")
}
concatField := typm.Fields.Get("concat")
if concatField == nil {
return fmt.Errorf("field %q not found", "concat")
}
if concatField.Type.String() != "String!" {
return fmt.Errorf("field %q has an invalid type: %q", "concat", concatField.Type.String())
}
if len(concatField.Arguments) != 2 || concatField.Arguments[0] == nil || concatField.Arguments[1] == nil || concatField.Arguments[0].Type.String() != "String!" || concatField.Arguments[1].Type.String() != "String!" {
return fmt.Errorf("field %q has an invalid args: %+v", "concat", concatField.Arguments)
}
return nil
},
},
{
name: "Extend type",
sdl: `
type Query {
hello: String!
}
extend type Query {
world: String!
}`,
validateSchema: func(s *ast.Schema) error {
typ, ok := s.Types["Query"].(*ast.ObjectTypeDefinition)
if !ok {
return fmt.Errorf("type %q not found", "Query")
}
helloField := typ.Fields.Get("hello")
if helloField == nil {
return fmt.Errorf("field %q not found", "hello")
}
if helloField.Type.String() != "String!" {
return fmt.Errorf("field %q has an invalid type: %q", "hello", helloField.Type.String())
}
worldField := typ.Fields.Get("world")
if worldField == nil {
return fmt.Errorf("field %q not found", "world")
}
if worldField.Type.String() != "String!" {
return fmt.Errorf("field %q has an invalid type: %q", "world", worldField.Type.String())
}
return nil
},
},
{
name: "Extend schema",
sdl: `
schema {
query: Query
}
type Query {
hello: String!
}
extend schema {
mutation: Mutation
}
type Mutation {
concat(a: String!, b: String!): String!
}
`,
validateSchema: func(s *ast.Schema) error {
typq, ok := s.Types["Query"].(*ast.ObjectTypeDefinition)
if !ok {
return fmt.Errorf("type %q not found", "Query")
}
helloField := typq.Fields.Get("hello")
if helloField == nil {
return fmt.Errorf("field %q not found", "hello")
}
if helloField.Type.String() != "String!" {
return fmt.Errorf("field %q has an invalid type: %q", "hello", helloField.Type.String())
}
typm, ok := s.Types["Mutation"].(*ast.ObjectTypeDefinition)
if !ok {
return fmt.Errorf("type %q not found", "Mutation")
}
concatField := typm.Fields.Get("concat")
if concatField == nil {
return fmt.Errorf("field %q not found", "concat")
}
if concatField.Type.String() != "String!" {
return fmt.Errorf("field %q has an invalid type: %q", "concat", concatField.Type.String())
}
if len(concatField.Arguments) != 2 || concatField.Arguments[0] == nil || concatField.Arguments[1] == nil || concatField.Arguments[0].Type.String() != "String!" || concatField.Arguments[1].Type.String() != "String!" {
return fmt.Errorf("field %q has an invalid args: %+v", "concat", concatField.Arguments)
}
return nil
},
},
{
name: "Extend type with interface implementation",
sdl: `
interface Named {
name: String!
}
type Product {
id: ID!
}
extend type Product implements Named {
name: String!
}`,
validateSchema: func(s *ast.Schema) error {
typ, ok := s.Types["Product"].(*ast.ObjectTypeDefinition)
if !ok {
return fmt.Errorf("type %q not found", "Product")
}
idField := typ.Fields.Get("id")
if idField == nil {
return fmt.Errorf("field %q not found", "id")
}
if idField.Type.String() != "ID!" {
return fmt.Errorf("field %q has an invalid type: %q", "id", idField.Type.String())
}
nameField := typ.Fields.Get("name")
if nameField == nil {
return fmt.Errorf("field %q not found", "name")
}
if nameField.Type.String() != "String!" {
return fmt.Errorf("field %q has an invalid type: %q", "name", nameField.Type.String())
}
ifc, ok := s.Types["Named"].(*ast.InterfaceTypeDefinition)
if !ok {
return fmt.Errorf("type %q not found", "Named")
}
nameField = ifc.Fields.Get("name")
if nameField == nil {
return fmt.Errorf("field %q not found", "name")
}
if nameField.Type.String() != "String!" {
return fmt.Errorf("field %q has an invalid type: %q", "name", nameField.Type.String())
}
return nil
},
},
{
name: "Extend union type",
sdl: `
type Named {
name: String!
}
type Numbered {
num: Int!
}
union Item = Named | Numbered
type Coloured {
Colour: String!
}
extend union Item = Coloured
`,
validateSchema: func(s *ast.Schema) error {
typ, ok := s.Types["Item"].(*ast.Union)
if !ok {
return fmt.Errorf("type %q not found", "Item")
}
if len(typ.UnionMemberTypes) != 3 {
return fmt.Errorf("Expected 3 possible types, but instead got %d types", len(typ.UnionMemberTypes))
}
posible := map[string]struct{}{
"Coloured": {},
"Named": {},
"Numbered": {},
}
for _, pt := range typ.UnionMemberTypes {
if _, ok := posible[pt.Name]; !ok {
return fmt.Errorf("Unexpected possible type %q", pt.Name)
}
}
return nil
},
},
{
name: "Extend enum type",
sdl: `
enum Currencies{
AUD
USD
EUR
}
extend enum Currencies {
BGN
GBP
}
`,
validateSchema: func(s *ast.Schema) error {
typ, ok := s.Types["Currencies"].(*ast.EnumTypeDefinition)
if !ok {
return fmt.Errorf("enum %q not found", "Currencies")
}
if len(typ.EnumValuesDefinition) != 5 {
return fmt.Errorf("Expected 5 enum values, but instead got %d types", len(typ.EnumValuesDefinition))
}
posible := map[string]struct{}{
"AUD": {},
"USD": {},
"EUR": {},
"BGN": {},
"GBP": {},
}
for _, v := range typ.EnumValuesDefinition {
if _, ok := posible[v.EnumValue]; !ok {
return fmt.Errorf("Unexpected enum value %q", v.EnumValue)
}
}
return nil
},
},
{
name: "Extend incompatible type",
sdl: `
type Query {
hello: String!
}
extend interface Query {
name: String!
}`,
validateError: func(err error) error {
msg := `trying to extend type "OBJECT" with type "INTERFACE"`
if err == nil || err.Error() != msg {
return fmt.Errorf("expected error %q, but got %q", msg, err)
}
return nil
},
},
{
name: "Extend type already implements an interface",
sdl: `
interface Named {
name: String!
}
type Product implements Named {
id: ID!
name: String!
}
extend type Product implements Named {
}`,
validateError: func(err error) error {
msg := `interface "Named" implemented in the extension is already implemented in "Product"`
if err == nil || err.Error() != msg {
return fmt.Errorf("expected error %q, but got %q", msg, err)
}
return nil
},
},
{
name: "Extend union already contains type",
sdl: `
type Named {
name: String!
}
type Numbered {
num: Int!
}
union Item = Named | Numbered
type Coloured {
Colour: String!
}
extend union Item = Coloured | Named
`,
validateError: func(err error) error {
msg := `union type "Named" already declared in "Item"`
if err == nil || err.Error() != msg {
return fmt.Errorf("expected error %q, but got %q", msg, err)
}
return nil
},
},
{
name: "Extend union contains type",
sdl: `
type Named {
name: String!
}
type Numbered {
num: Int!
}
union Item = Named | Numbered
type Coloured {
Colour: String!
}
extend union Item = Coloured
`,
validateSchema: func(s *ast.Schema) error {
typ, ok := s.Types["Item"].(*ast.Union)
if !ok {
return fmt.Errorf("type %q not found", "Item")
}
if len(typ.UnionMemberTypes) != 3 {
return fmt.Errorf("Expected 3 possible types, but instead got %d types", len(typ.UnionMemberTypes))
}
posible := map[string]struct{}{
"Coloured": {},
"Named": {},
"Numbered": {},
}
for _, pt := range typ.UnionMemberTypes {
if _, ok := posible[pt.Name]; !ok {
return fmt.Errorf("Unexpected possible type %q", pt.Name)
}
}
return nil
},
},
{
name: "Extend input",
sdl: `
input Product {
id: ID!
name: String!
}
extend input Product {
category: Category!
tags: [String!]! = ["sale", "shoes"]
}
input Category {
id: ID!
name: String!
}
`,
validateSchema: func(s *ast.Schema) error {
typ, ok := s.Types["Product"].(*ast.InputObject)
if !ok {
return fmt.Errorf("type %q not found", "Product")
}
if len(typ.Values) != 4 {
return fmt.Errorf("Expected 4 fields, but instead got %d types", len(typ.Values))
}
posible := map[string]struct{}{
"id": {},
"name": {},
"category": {},
"tags": {},
}
for _, pt := range typ.Values {
if _, ok := posible[pt.Name.Name]; !ok {
return fmt.Errorf("Unexpected possible type %q", pt.Name)
}
}
categoryField := typ.Values.Get("category")
if categoryField == nil {
return fmt.Errorf("field %q not found", "category")
}
if categoryField.Type.String() != "Category!" {
return fmt.Errorf("expected type %q, but got %q", "Category!", categoryField.Type.String())
}
if categoryField.Type.Kind() != "NON_NULL" {
return fmt.Errorf("expected kind %q, but got %q", "NON_NULL", categoryField.Type.Kind())
}
return nil
},
},
{
name: "Extend enum value already exists",
sdl: `
enum Currencies{
AUD
USD
EUR
}
extend enum Currencies {
AUD
}`,
validateError: func(err error) error {
msg := `enum value "AUD" already declared in "Currencies"`
if err == nil || err.Error() != msg {
return fmt.Errorf("expected error %q, but got %q", msg, err)
}
return nil
},
},
{
name: "Extend input field already exists",
sdl: `
input Product{
name: String!
}
extend input Product {
name: String!
}`,
validateError: func(err error) error {
msg := `extended field {"name" {'\x06' '\x05'}} already exists`
if err == nil || err.Error() != msg {
return fmt.Errorf("expected error %q, but got %q", msg, err)
}
return nil
},
},
{
name: "Extend field already exists",
sdl: `
interface Named {
name: String!
}
type Product implements Named {
id: ID!
name: String!
}
extend type Product {
name: String!
}`,
validateError: func(err error) error {
msg := `extended field "name" already exists`
if err == nil || err.Error() != msg {
return fmt.Errorf("expected error %q, but got %q", msg, err)
}
return nil
},
},
{
name: "Extend interface type",
sdl: `
interface Product {
id: ID!
name: String!
}
extend interface Product {
category: String!
}
`,
validateSchema: func(s *ast.Schema) error {
typ, ok := s.Types["Product"].(*ast.InterfaceTypeDefinition)
if !ok {
return fmt.Errorf("type %q not found", "Product")
}
if len(typ.Fields) != 3 {
return fmt.Errorf("Expected 3 fields, but instead got %d types", len(typ.Fields))
}
fields := map[string]struct{}{
"id": {},
"name": {},
"category": {},
}
for _, f := range typ.Fields {
if _, ok := fields[f.Name]; !ok {
return fmt.Errorf("Unexpected field %q", f.Name)
}
}
return nil
},
},
{
name: "Extend unknown type",
sdl: `
extend type User {
name: String!
}
`,
validateError: func(err error) error {
msg := `trying to extend unknown type "User"`
if err == nil || err.Error() != msg {
return fmt.Errorf("expected error %q, but got %q", msg, err)
}
return nil
},
},
{
name: "Extend invalid syntax",
sdl: `
extend invalid Node {
id: ID!
}
`,
validateError: func(err error) error {
msg := `graphql: syntax error: unexpected "invalid", expecting "schema", "type", "enum", "interface", "union" or "input" (line 2, column 19)`
if err == nil || err.Error() != msg {
return fmt.Errorf("expected error %q, but got %q", msg, err)
}
return nil
},
},
{
name: "Parses directives",
sdl: `
directive @objectdirective on OBJECT
directive @fielddirective on FIELD_DEFINITION
directive @enumdirective on ENUM
directive @uniondirective on UNION
directive @directive on SCALAR
| OBJECT
| FIELD_DEFINITION
| ARGUMENT_DEFINITION
| INTERFACE
| UNION
| ENUM
| ENUM_VALUE
| INPUT_OBJECT
| INPUT_FIELD_DEFINITION
directive @repeatabledirective repeatable on SCALAR
interface NamedEntity @directive { name: String }
scalar Time @directive
type Photo @objectdirective {
id: ID! @deprecated @fielddirective
}
type Person implements NamedEntity @objectdirective {
name: String
}
enum Direction @enumdirective {
NORTH @deprecated
EAST
SOUTH
WEST
}
union Union @uniondirective = Photo | Person
scalar Mass @repeatabledirective @repeatabledirective
`,
validateSchema: func(s *ast.Schema) error {
namedEntityDirectives := s.Types["NamedEntity"].(*ast.InterfaceTypeDefinition).Directives
if len(namedEntityDirectives) != 1 || namedEntityDirectives[0].Name.Name != "directive" {
return fmt.Errorf("missing directive on NamedEntity interface, expected @directive but got %v", namedEntityDirectives)
}
timeDirectives := s.Types["Time"].(*ast.ScalarTypeDefinition).Directives
if len(timeDirectives) != 1 || timeDirectives[0].Name.Name != "directive" {
return fmt.Errorf("missing directive on Time scalar, expected @directive but got %v", timeDirectives)
}
photo := s.Types["Photo"].(*ast.ObjectTypeDefinition)
photoDirectives := photo.Directives
if len(photoDirectives) != 1 || photoDirectives[0].Name.Name != "objectdirective" {
return fmt.Errorf("missing directive on Time scalar, expected @objectdirective but got %v", photoDirectives)
}
if len(photo.Fields.Get("id").Directives) != 2 {
return fmt.Errorf("expected Photo.id to have 2 directives but got %v", photoDirectives)
}
directionDirectives := s.Types["Direction"].(*ast.EnumTypeDefinition).Directives
if len(directionDirectives) != 1 || directionDirectives[0].Name.Name != "enumdirective" {
return fmt.Errorf("missing directive on Direction enum, expected @enumdirective but got %v", directionDirectives)
}
unionDirectives := s.Types["Union"].(*ast.Union).Directives
if len(unionDirectives) != 1 || unionDirectives[0].Name.Name != "uniondirective" {
return fmt.Errorf("missing directive on Union union, expected @uniondirective but got %v", unionDirectives)
}
massDirectives := s.Types["Mass"].(*ast.ScalarTypeDefinition).Directives
if len(massDirectives) != 2 || massDirectives[0].Name.Name != "repeatabledirective" || massDirectives[1].Name.Name != "repeatabledirective" {
return fmt.Errorf("missing directive on Repeatable scalar, expected @repeatabledirective @repeatabledirective but got %v", massDirectives)
}
return nil
},
},
{
name: "Sets Directive.Repeatable if `repeatable` keyword is given",
sdl: `
directive @nonrepeatabledirective on SCALAR
directive @repeatabledirective repeatable on SCALAR
`,
validateSchema: func(s *ast.Schema) error {
if dir := s.Directives["nonrepeatabledirective"]; dir.Repeatable {
return fmt.Errorf("did not expect directive to be repeatable: %v", dir)
}
if dir := s.Directives["repeatabledirective"]; !dir.Repeatable {
return fmt.Errorf("expected directive to be repeatable: %v", dir)
}
return nil
},
},
{
name: "Directive definition does not allow double-`repeatable`",
sdl: `
directive @mydirective repeatable repeatable SCALAR
scalar MyScalar @mydirective
`,
validateError: func(err error) error {
msg := `graphql: syntax error: unexpected "repeatable", expecting "on" (line 2, column 38)`
if err == nil || err.Error() != msg {
return fmt.Errorf("expected error %q, but got %q", msg, err)
}
return nil
},
},
{
name: "Directive definition does not allow double-`on` instead of `repeatable on`",
sdl: `
directive @mydirective on on SCALAR
scalar MyScalar @mydirective
`,
validateError: func(err error) error {
prefix := `graphql: syntax error: "on" is not a legal directive location`
if err == nil || !strings.HasPrefix(err.Error(), prefix) {
return fmt.Errorf("expected error starting with %q, but got %q", prefix, err)
}
return nil
},
},
{
name: "Disallow repeat of a directive if it is not `repeatable`",
sdl: `
directive @nonrepeatabledirective on FIELD_DEFINITION
type Foo {
bar: String @nonrepeatabledirective @nonrepeatabledirective
}
`,
validateError: func(err error) error {
prefix := `graphql: non repeatable directive "nonrepeatabledirective" can not be repeated. Consider adding "repeatable"`
if err == nil || !strings.HasPrefix(err.Error(), prefix) {
return fmt.Errorf("expected error starting with %q, but got %q", prefix, err)
}
return nil
},
},
{
name: "Decorating scalar with an undeclared directive should return an error",
sdl: `
scalar S @undeclareddirective
`,
validateError: func(err error) error {
prefix := `graphql: directive "undeclareddirective" not found`
if err == nil || !strings.HasPrefix(err.Error(), prefix) {
return fmt.Errorf("expected error starting with %q, but got %q", prefix, err)
}
return nil
},
},
{
name: "Decorating argument with an undeclared directive should return an error",
sdl: `
type Query {
hello(name: String! @undeclareddirective): String!
}
`,
validateError: func(err error) error {
prefix := `graphql: directive "undeclareddirective" not found`
if err == nil || !strings.HasPrefix(err.Error(), prefix) {
return fmt.Errorf("expected error starting with %q, but got %q", prefix, err)
}
return nil
},
},
{
name: "Decorating input object with an undeclared directive should return an error",
sdl: `
input InputObject @undeclareddirective{field: String!}
`,
validateError: func(err error) error {
prefix := `graphql: directive "undeclareddirective" not found`
if err == nil || !strings.HasPrefix(err.Error(), prefix) {
return fmt.Errorf("expected error starting with %q, but got %q", prefix, err)
}
return nil
},
},
{
name: "Decorating interface with an undeclared directive should return an error",
sdl: `
interface I @undeclareddirective {field: String!}
`,
validateError: func(err error) error {
prefix := `graphql: directive "undeclareddirective" not found`
if err == nil || !strings.HasPrefix(err.Error(), prefix) {
return fmt.Errorf("expected error starting with %q, but got %q", prefix, err)
}
return nil
},
},
} {
t.Run(test.name, func(t *testing.T) {
s, err := schema.ParseSchema(test.sdl, test.useStringDescriptions)
if err != nil {
if test.validateError == nil {
t.Fatal(err)
}
if err2 := test.validateError(err); err2 != nil {
t.Fatal(err2)
}
}
if test.validateSchema != nil {
if err := test.validateSchema(s); err != nil {
t.Fatal(err)
}
}
})
}
}
func TestInterfaceImplementsInterface(t *testing.T) {
for _, tt := range []struct {
name string
sdl string
useStringDescriptions bool
validateError func(err error) error
validateSchema func(s *ast.Schema) error
}{
{
name: "Parses interface implementing other interface",
sdl: `
interface Foo {
field: String!
}
interface Bar implements Foo {
field: String!
}
`,
validateSchema: func(s *ast.Schema) error {
const implementedInterfaceName = "Bar"
typ, ok := s.Types[implementedInterfaceName].(*ast.InterfaceTypeDefinition)
if !ok {
return fmt.Errorf("interface %q not found", implementedInterfaceName)
}
if len(typ.Fields) != 1 {
return fmt.Errorf("invalid number of fields: want %d, have %d", 1, len(typ.Fields))
}
const fieldName = "field"
if typ.Fields[0].Name != fieldName {
return fmt.Errorf("field %q not found", fieldName)
}
if len(typ.Interfaces) != 1 {
return fmt.Errorf("invalid number of implementing interfaces found on %q: want %d, have %d", implementedInterfaceName, 1, len(typ.Interfaces))
}
const implementingInterfaceName = "Foo"
if typ.Interfaces[0].Name != implementingInterfaceName {
return fmt.Errorf("interface %q not found", implementingInterfaceName)
}
return nil
},
},
{
name: "Parses interface transitively implementing an interface that implements an interface",
sdl: `
interface Foo {
field: String!
}
interface Bar implements Foo {
field: String!
}
interface Baz implements Bar & Foo {
field: String!
}
`,
validateSchema: func(s *ast.Schema) error {
const implementedInterfaceName = "Baz"
typ, ok := s.Types[implementedInterfaceName].(*ast.InterfaceTypeDefinition)
if !ok {
return fmt.Errorf("interface %q not found", implementedInterfaceName)
}
if len(typ.Fields) != 1 {
return fmt.Errorf("invalid number of fields: want %d, have %d", 1, len(typ.Fields))
}
const fieldName = "field"
if typ.Fields[0].Name != fieldName {
return fmt.Errorf("field %q not found", fieldName)
}
if len(typ.Interfaces) != 2 {
return fmt.Errorf("invalid number of implementing interfaces found on %q: want %d, have %d", implementedInterfaceName, 2, len(typ.Interfaces))
}
const firstImplementingInterfaceName = "Bar"
if typ.Interfaces[0].Name != firstImplementingInterfaceName {
return fmt.Errorf("first interface %q not found", firstImplementingInterfaceName)
}
const secondImplementingInterfaceName = "Foo"
if typ.Interfaces[1].Name != secondImplementingInterfaceName {
return fmt.Errorf("second interface %q not found", secondImplementingInterfaceName)
}
return nil
},
},
{
name: "Transitively implemented interfaces must also be defined on an implementing type or interface",
sdl: `
interface A {
message: String!
}
interface B implements A {
message: String!
name: String!
}
interface C implements B {
message: String!
name: String!
hug: Boolean!
}
`,
validateError: func(err error) error {
msg := `graphql: interface "C" must explicitly implement transitive interface "A"`
if err == nil || err.Error() != msg {
return fmt.Errorf("expected error %q, but got %q", msg, err)
}
return nil
},
},
{
name: "Unions can be defined with a leading pipe",
sdl: `
type Named {
name: String!
}
type Numbered {
num: Int!
}
union Item1 =
| Named
| Numbered
union Item2 = | Named | Numbered
`,
validateSchema: func(s *ast.Schema) error {
for _, itemName := range []string{"Item1", "Item2"} {
typ, ok := s.Types[itemName].(*ast.Union)
if !ok {
return fmt.Errorf("type %q not found", "Item")
}
if len(typ.UnionMemberTypes) != 2 {
return fmt.Errorf("Expected 2 possible types, but instead got %d types", len(typ.UnionMemberTypes))
}
posible := map[string]struct{}{
"Named": {},
"Numbered": {},
}
for _, pt := range typ.UnionMemberTypes {
if _, ok := posible[pt.Name]; !ok {
return fmt.Errorf("Unexpected possible type %q", pt.Name)
}
}
}
return nil
},
},
} {
t.Run(tt.name, func(t *testing.T) {
s, err := schema.ParseSchema(tt.sdl, tt.useStringDescriptions)
if err != nil {
if tt.validateError == nil {
t.Fatal(err)
}
if err2 := tt.validateError(err); err2 != nil {
t.Fatal(err2)
}
}
if tt.validateSchema != nil {
if err2 := tt.validateSchema(s); err2 != nil {
t.Fatal(err2)
}
}
})
}
}
|