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
|
package validator_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/parser"
"github.com/vektah/gqlparser/v2/validator"
)
func TestExtendingNonExistantTypes(t *testing.T) {
s := gqlparser.MustLoadSchema(
&ast.Source{Name: "graph/schema.graphqls", Input: `
extend type User {
id: ID!
}
extend type Product {
upc: String!
}
union _Entity = Product | User
extend type Query {
entity: _Entity
}
`, BuiltIn: false},
)
q, err := parser.ParseQuery(&ast.Source{Name: "ff", Input: `{
entity {
... on User {
id
}
}
}`})
require.NoError(t, err)
require.Nil(t, validator.Validate(s, q))
}
func TestValidationRulesAreIndependent(t *testing.T) {
s := gqlparser.MustLoadSchema(
&ast.Source{Name: "graph/schema.graphqls", Input: `
extend type Query {
myAction(myEnum: Locale!): SomeResult!
}
type SomeResult {
id: String
}
enum Locale {
EN
LT
DE
}
`, BuiltIn: false},
)
// Validation as a first call
q1, err := parser.ParseQuery(&ast.Source{
Name: "SomeOperation", Input: `
query SomeOperation {
# Note: Not providing mandatory parameter: (myEnum: Locale!)
myAction {
id
}
}
`,
})
require.NoError(t, err)
r1 := validator.Validate(s, q1)
require.Len(t, r1, 1)
const errorString = `SomeOperation:4: Field "myAction" argument "myEnum" of type "Locale!" is required, but it was not provided.`
require.EqualError(t, r1[0], errorString)
// Some other call that should not affect validator behavior
q2, err := parser.ParseQuery(&ast.Source{
Name: "SomeOperation", Input: `
# Note: there is default enum value in variables
query SomeOperation ($locale: Locale! = DE) {
myAction(myEnum: $locale) {
id
}
}
`,
})
require.NoError(t, err)
require.Nil(t, validator.Validate(s, q2))
// Repeating same query and expecting to still return same validation error
require.Len(t, r1, 1)
require.EqualError(t, r1[0], errorString)
}
func TestDeprecatingTypes(t *testing.T) {
schema := &ast.Source{
Name: "graph/schema.graphqls",
Input: `
type DeprecatedType {
deprecatedField: String @deprecated
newField(deprecatedArg: Int): Boolean
}
enum DeprecatedEnum {
ALPHA @deprecated
}
`,
BuiltIn: false,
}
_, err := validator.LoadSchema(append([]*ast.Source{validator.Prelude}, schema)...)
require.NoError(t, err)
}
func TestNoUnusedVariables(t *testing.T) {
// https://github.com/99designs/gqlgen/issues/2028
t.Run("gqlgen issues #2028", func(t *testing.T) {
s := gqlparser.MustLoadSchema(
&ast.Source{Name: "graph/schema.graphqls", Input: `
type Query {
bar: String!
}
`, BuiltIn: false},
)
q, err := parser.ParseQuery(&ast.Source{Name: "2028", Input: `
query Foo($flag: Boolean!) {
...Bar
}
fragment Bar on Query {
bar @include(if: $flag)
}
`})
require.NoError(t, err)
require.Nil(t, validator.Validate(s, q))
})
}
|