diff --git a/validator/validator_test.go b/validator/validator_test.go index 6c1b50b..6c7250d 100644 --- a/validator/validator_test.go +++ b/validator/validator_test.go @@ -9,6 +9,7 @@ import ( "github.com/vektah/gqlparser/v2/ast" "github.com/vektah/gqlparser/v2/parser" "github.com/vektah/gqlparser/v2/validator" + "github.com/vektah/gqlparser/v2/validator/rules" ) func TestExtendingNonExistantTypes(t *testing.T) { @@ -178,3 +179,123 @@ func TestCustomRuleSet(t *testing.T) { require.Equal(t, "some error message", errList[0].Message) require.Equal(t, "some other error message", errList[1].Message) } + +func TestValidateOptionDisableSuggestionFieldsOnCorrectType(t *testing.T) { + s := gqlparser.MustLoadSchema(&ast.Source{Name: "graph/schema.graphqls", Input: ` + extend type User { + id: ID! + } + + extend type Query { + user: User! + } + `, BuiltIn: false}, + ) + + q, err := parser.ParseQuery(&ast.Source{Name: "ff", Input: `{ + user { + idd + } + }`}) + + validator.AddRule(rules.FieldsOnCorrectTypeRuleWithoutSuggestions.Name, rules.FieldsOnCorrectTypeRuleWithoutSuggestions.RuleFunc) + r := validator.Validate(s, q) + require.NoError(t, err) + require.Len(t, r, 2) + require.EqualError(t, r[0], `ff:3: Cannot query field "idd" on type "User". Did you mean "id"?`) + require.EqualError(t, r[1], `ff:3: Cannot query field "idd" on type "User".`) + + validator.AddRule(rules.KnownTypeNamesRuleWithoutSuggestions.Name, rules.KnownTypeNamesRuleWithoutSuggestions.RuleFunc) +} + +func TestValidateOptionDisableSuggestionFieldsOnKnownArgumentNames(t *testing.T) { + s := gqlparser.MustLoadSchema(&ast.Source{Name: "graph/schema.graphqls", Input: ` + extend type User { + id: ID! + } + + extend type Query { + user(id: ID!): User! + } + `, BuiltIn: false}, + ) + + q, err := parser.ParseQuery(&ast.Source{Name: "ff", Input: `{ + user(idd: "1") { + id + } + }`}) + + validator.AddRule(rules.KnownArgumentNamesRuleWithoutSuggestions.Name, rules.KnownArgumentNamesRuleWithoutSuggestions.RuleFunc) + r := validator.Validate(s, q) + require.NoError(t, err) + require.Len(t, r, 3) + require.EqualError(t, r[0], `ff:2: Unknown argument "idd" on field "Query.user". Did you mean "id"?`) + require.EqualError(t, r[2], `ff:2: Unknown argument "idd" on field "Query.user".`) +} + +func TestValidateOptionDisableSuggestionKnownTypeNames(t *testing.T) { + s := gqlparser.MustLoadSchema(&ast.Source{Name: "graph/schema.graphqls", Input: ` + extend type User { + id: ID! + } + + extend type Query { + user(id: ID!): User! + } + `, BuiltIn: false}, + ) + + validator.AddRule(rules.KnownTypeNamesRuleWithoutSuggestions.Name, rules.KnownTypeNamesRuleWithoutSuggestions.RuleFunc) + _, errList := gqlparser.LoadQuery(s, ` + query { + user(id: "1") { + ...Foo + } + } + + fragment Foo on Use { + id + }`, + ) + + if errList == nil { + t.Fatal("expected an error") + } + if len(errList) != 3 { + t.Fatalf("expected 2 errors, got %d", len(errList)) + } + require.Equal(t, errList[0].Message, "Unknown type \"Use\". Did you mean \"User\"?") + require.Equal(t, errList[1].Message, "Unknown type \"Use\".") +} + +func TestValidateOptionDisableSuggestionValuesOfCorrectType(t *testing.T) { + s := gqlparser.MustLoadSchema(&ast.Source{Name: "graph/schema.graphqls", Input: ` + extend type User { + id: ID! + } + + enum Number { + ONE + TWO + } + + extend type Query { + user(number: Number!): User! + } + `, BuiltIn: false}, + ) + + q, err := parser.ParseQuery(&ast.Source{Name: "ff", Input: `{ + user(number: ON) { + id + } + }`}) + + validator.AddRule(rules.ValuesOfCorrectTypeRuleWithoutSuggestions.Name, rules.ValuesOfCorrectTypeRuleWithoutSuggestions.RuleFunc) + r := validator.Validate(s, q) + require.NoError(t, err) + require.Len(t, r, 2) + require.EqualError(t, r[0], `ff:2: Value "ON" does not exist in "Number!" enum. Did you mean the enum value "ONE"?`) + require.EqualError(t, r[1], `ff:2: Value "ON" does not exist in "Number!" enum.`) +}