Skip to content

Commit

Permalink
test: add tests for disable suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoikey committed Sep 29, 2024
1 parent 01c77a9 commit f4ff5c8
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.`)
}

0 comments on commit f4ff5c8

Please sign in to comment.