forked from graphql-go/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator_test.go
98 lines (86 loc) · 2.52 KB
/
validator_test.go
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
package graphql_test
import (
"reflect"
"testing"
"github.com/ahmadmuzakki/graphql"
"github.com/ahmadmuzakki/graphql/gqlerrors"
"github.com/ahmadmuzakki/graphql/language/ast"
"github.com/ahmadmuzakki/graphql/language/location"
"github.com/ahmadmuzakki/graphql/language/parser"
"github.com/ahmadmuzakki/graphql/language/source"
"github.com/ahmadmuzakki/graphql/testutil"
)
func expectValid(t *testing.T, schema *graphql.Schema, queryString string) {
source := source.NewSource(&source.Source{
Body: []byte(queryString),
Name: "GraphQL request",
})
AST, err := parser.Parse(parser.ParseParams{Source: source})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
validationResult := graphql.ValidateDocument(schema, AST, nil)
if !validationResult.IsValid || len(validationResult.Errors) > 0 {
t.Fatalf("Unexpected error: %v", validationResult.Errors)
}
}
func TestValidator_SupportsFullValidation_ValidatesQueries(t *testing.T) {
expectValid(t, testutil.TestSchema, `
query {
catOrDog {
... on Cat {
furColor
}
... on Dog {
isHousetrained
}
}
}
`)
}
// NOTE: experimental
func TestValidator_SupportsFullValidation_ValidatesUsingACustomTypeInfo(t *testing.T) {
// This TypeInfo will never return a valid field.
typeInfo := graphql.NewTypeInfo(&graphql.TypeInfoConfig{
Schema: testutil.TestSchema,
FieldDefFn: func(schema *graphql.Schema, parentType graphql.Type, fieldAST *ast.Field) *graphql.FieldDefinition {
return nil
},
})
ast := testutil.TestParse(t, `
query {
catOrDog {
... on Cat {
furColor
}
... on Dog {
isHousetrained
}
}
}
`)
errors := graphql.VisitUsingRules(testutil.TestSchema, typeInfo, ast, graphql.SpecifiedRules)
expectedErrors := []gqlerrors.FormattedError{
{
Message: `Cannot query field "catOrDog" on type "QueryRoot". Did you mean "catOrDog"?`,
Locations: []location.SourceLocation{
{Line: 3, Column: 9},
},
},
{
Message: `Cannot query field "furColor" on type "Cat". Did you mean "furColor"?`,
Locations: []location.SourceLocation{
{Line: 5, Column: 13},
},
},
{
Message: `Cannot query field "isHousetrained" on type "Dog". Did you mean "isHousetrained"?`,
Locations: []location.SourceLocation{
{Line: 8, Column: 13},
},
},
}
if !reflect.DeepEqual(expectedErrors, errors) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expectedErrors, errors))
}
}