-
Notifications
You must be signed in to change notification settings - Fork 2
/
graphqlparser.go
60 lines (48 loc) · 1.42 KB
/
graphqlparser.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
package graphqlparser
import (
"github.com/bucketd/go-graphqlparser/ast"
"github.com/bucketd/go-graphqlparser/graphql"
"github.com/bucketd/go-graphqlparser/language"
"github.com/bucketd/go-graphqlparser/validation"
"github.com/bucketd/go-graphqlparser/validation/rules"
)
var (
// DefaultValidationWalker ...
DefaultValidationWalker = validation.NewWalker(rules.Specified)
// DefaultValidationWalkerSDL ...
DefaultValidationWalkerSDL = validation.NewWalker(rules.SpecifiedSDL)
)
// ParseDoc ...
func ParseDoc(doc []byte, schema *graphql.Schema) (*ast.Document, *graphql.Errors, error) {
parser := language.NewParser(doc)
queryAST, err := parser.Parse()
if err != nil {
return nil, nil, err
}
ctx := validation.Validate(queryAST, schema, DefaultValidationWalker)
if ctx.Errors.Len() > 0 {
return nil, ctx.Errors, nil
}
return &queryAST, nil, nil
}
// ParseSDLDoc ...
func ParseSDLDoc(doc []byte, schema *graphql.Schema) (*graphql.Schema, *graphql.Errors, error) {
parser := language.NewParser(doc)
sdlAST, err := parser.Parse()
if err != nil {
return nil, nil, err
}
ctx := validation.ValidateSDL(sdlAST, schema, DefaultValidationWalkerSDL)
if ctx.Errors.Len() > 0 {
return nil, ctx.Errors, nil
}
schema, err = validation.BuildSchema(ctx)
if err != nil {
return nil, nil, err
}
errs := validation.ValidateSchema(ctx, schema)
if errs.Len() > 0 {
return nil, errs, nil
}
return schema, nil, nil
}