@@ -5,10 +5,12 @@ import (
55 "encoding/json"
66
77 "github.com/99designs/gqlgen/graphql"
8- "github.com/99designs/gqlgen/graphql/executor"
98 "github.com/Code-Hex/gqldoc/internal/gqlgen"
109 "github.com/Code-Hex/gqldoc/internal/introspection"
1110 "github.com/pkg/errors"
11+ "github.com/vektah/gqlparser/v2/ast"
12+ "github.com/vektah/gqlparser/v2/parser"
13+ "github.com/vektah/gqlparser/v2/validator"
1214)
1315
1416func LoadSchema (filenames ... string ) (* introspection.Root , error ) {
@@ -17,18 +19,16 @@ func LoadSchema(filenames ...string) (*introspection.Root, error) {
1719 return nil , errors .WithStack (err )
1820 }
1921
20- // Set time for tracing
21- ctx := graphql .StartOperationTrace (context .Background ())
22-
23- exec := executor .New (es )
24- oc , err := exec .CreateOperationContext (ctx , & graphql.RawParams {
25- Query : introspection .Query ,
22+ ctx , err := CreateOperationContext (Params {
23+ Schema : es .Schema (),
24+ Query : introspection .Query ,
25+ Variables : map [string ]interface {}{},
2626 })
27- // Need to do introspection
28- oc .DisableIntrospection = false
27+ if err != nil {
28+ return nil , errors .WithStack (err )
29+ }
2930
30- responses , ctx := exec .DispatchOperation (ctx , oc )
31- resp := responses (ctx )
31+ resp := es .Exec (ctx )(context .Background ())
3232 if len (resp .Errors ) > 0 {
3333 return nil , resp .Errors
3434 }
@@ -39,3 +39,47 @@ func LoadSchema(filenames ...string) (*introspection.Root, error) {
3939 }
4040 return & res , nil
4141}
42+
43+ type Params struct {
44+ Schema * ast.Schema
45+ Query string
46+ Variables map [string ]interface {}
47+ }
48+
49+ func CreateOperationContext (params Params ) (context.Context , error ) {
50+ doc , err := parseQuery (params .Schema , params .Query )
51+ if err != nil {
52+ return nil , err
53+ }
54+ operation := doc .Operations .ForName ("" )
55+ if operation == nil {
56+ return nil , errors .New ("operation not found" )
57+ }
58+ variables , verr := validator .VariableValues (params .Schema , operation , params .Variables )
59+ if verr != nil {
60+ return nil , errors .WithStack (verr )
61+ }
62+
63+ opCtx := & graphql.OperationContext {
64+ RawQuery : params .Query ,
65+ Variables : variables ,
66+ Doc : doc ,
67+ Operation : operation ,
68+ }
69+ ctx := graphql .WithOperationContext (context .Background (), opCtx )
70+ return ctx , nil
71+ }
72+
73+ func parseQuery (schema * ast.Schema , query string ) (* ast.QueryDocument , error ) {
74+ doc , err := parser .ParseQuery (& ast.Source {
75+ Input : query ,
76+ })
77+ if err != nil {
78+ return nil , errors .WithStack (err )
79+ }
80+ listErr := validator .Validate (schema , doc )
81+ if len (listErr ) != 0 {
82+ return nil , listErr
83+ }
84+ return doc , nil
85+ }
0 commit comments