forked from graph-gophers/graphql-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraphql.go
161 lines (136 loc) · 4.7 KB
/
graphql.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package graphql
import (
"context"
"encoding/json"
"fmt"
"github.com/neelance/graphql-go/errors"
"github.com/neelance/graphql-go/internal/exec"
"github.com/neelance/graphql-go/internal/query"
"github.com/neelance/graphql-go/internal/schema"
"github.com/neelance/graphql-go/internal/validation"
"github.com/neelance/graphql-go/introspection"
"github.com/neelance/graphql-go/trace"
)
// ID represents GraphQL's "ID" type. A custom type may be used instead.
type ID string
func (_ ID) ImplementsGraphQLType(name string) bool {
return name == "ID"
}
func (id *ID) UnmarshalGraphQL(input interface{}) error {
switch input := input.(type) {
case string:
*id = ID(input)
return nil
default:
return fmt.Errorf("wrong type")
}
}
// ParseSchema parses a GraphQL schema and attaches the given root resolver. It returns an error if
// the Go type signature of the resolvers does not match the schema. If nil is passed as the
// resolver, then the schema can not be executed, but it may be inspected (e.g. with ToJSON).
func ParseSchema(schemaString string, resolver interface{}) (*Schema, error) {
s := &Schema{
schema: schema.New(),
MaxParallelism: 10,
Tracer: trace.OpenTracingTracer{},
}
if err := s.schema.Parse(schemaString); err != nil {
return nil, err
}
if resolver != nil {
e, err := exec.Make(s.schema, resolver)
if err != nil {
return nil, err
}
s.exec = e
}
return s, nil
}
// MustParseSchema calls ParseSchema and panics on error.
func MustParseSchema(schemaString string, resolver interface{}) *Schema {
s, err := ParseSchema(schemaString, resolver)
if err != nil {
panic(err)
}
return s
}
// Schema represents a GraphQL schema with an optional resolver.
type Schema struct {
schema *schema.Schema
exec *exec.Exec
// MaxParallelism specifies the maximum number of resolvers per request allowed to run in parallel. The default is 10.
MaxParallelism int
// Tracer is used to trace queries and fields. It defaults to trace.OpenTracingTracer.
Tracer trace.Tracer
}
// Response represents a typical response of a GraphQL server. It may be encoded to JSON directly or
// it may be further processed to a custom response type, for example to include custom error data.
type Response struct {
Data interface{} `json:"data,omitempty"`
Errors []*errors.QueryError `json:"errors,omitempty"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
}
// Exec executes the given query with the schema's resolver. It panics if the schema was created
// without a resolver. If the context get cancelled, no further resolvers will be called and a
// the context error will be returned as soon as possible (not immediately).
func (s *Schema) Exec(ctx context.Context, queryString string, operationName string, variables map[string]interface{}) *Response {
if s.exec == nil {
panic("schema created without resolver, can not exec")
}
traceCtx, finish := s.Tracer.TraceQuery(ctx, queryString, operationName, variables)
data, errs := s.doExec(traceCtx, queryString, operationName, variables)
finish(errs)
return &Response{
Data: data,
Errors: errs,
}
}
func (s *Schema) doExec(ctx context.Context, queryString string, operationName string, variables map[string]interface{}) (interface{}, []*errors.QueryError) {
doc, qErr := query.Parse(queryString)
if qErr != nil {
return nil, []*errors.QueryError{qErr}
}
errs := validation.Validate(s.schema, doc)
if len(errs) != 0 {
return nil, errs
}
op, err := getOperation(doc, operationName)
if err != nil {
return nil, []*errors.QueryError{errors.Errorf("%s", err)}
}
r := &exec.Request{
Doc: doc,
Vars: variables,
Schema: s.schema,
Limiter: make(chan struct{}, s.MaxParallelism),
Tracer: s.Tracer,
}
return r.Execute(ctx, s.exec, op)
}
func getOperation(document *query.Document, operationName string) (*query.Operation, error) {
if len(document.Operations) == 0 {
return nil, fmt.Errorf("no operations in query document")
}
if operationName == "" {
if len(document.Operations) > 1 {
return nil, fmt.Errorf("more than one operation in query document and no operation name given")
}
for _, op := range document.Operations {
return op, nil // return the one and only operation
}
}
op := document.Operations.Get(operationName)
if op == nil {
return nil, fmt.Errorf("no operation with name %q", operationName)
}
return op, nil
}
// Inspect allows inspection of the given schema.
func (s *Schema) Inspect() *introspection.Schema {
return introspection.WrapSchema(s.schema)
}
// ToJSON encodes the schema in a JSON format used by tools like Relay.
func (s *Schema) ToJSON() ([]byte, error) {
result := exec.IntrospectSchema(s.schema)
return json.MarshalIndent(result, "", "\t")
}