forked from graph-gophers/graphql-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraphql.go
125 lines (105 loc) · 2.96 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
package graphql
import (
"context"
"encoding/json"
"fmt"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"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/introspection"
)
const OpenTracingTagQuery = "graphql.query"
const OpenTracingTagOperationName = "graphql.operationName"
const OpenTracingTagVariables = "graphql.variables"
const OpenTracingTagType = "graphql.type"
const OpenTracingTagField = "graphql.field"
const OpenTracingTagTrivial = "graphql.trivial"
const OpenTracingTagArgsPrefix = "graphql.args."
const OpenTracingTagError = "graphql.error"
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")
}
}
func ParseSchema(schemaString string, resolver interface{}) (*Schema, error) {
s := &Schema{
schema: schema.New(),
}
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
}
func MustParseSchema(schemaString string, resolver interface{}) *Schema {
s, err := ParseSchema(schemaString, resolver)
if err != nil {
panic(err)
}
return s
}
type Schema struct {
schema *schema.Schema
exec *exec.Exec
}
type Response struct {
Data interface{} `json:"data,omitempty"`
Errors []*errors.QueryError `json:"errors,omitempty"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
}
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")
}
document, err := query.Parse(queryString, s.schema.Resolve)
if err != nil {
return &Response{
Errors: []*errors.QueryError{err},
}
}
span, subCtx := opentracing.StartSpanFromContext(ctx, "GraphQL request")
span.SetTag(OpenTracingTagQuery, queryString)
if operationName != "" {
span.SetTag(OpenTracingTagOperationName, operationName)
}
if len(variables) != 0 {
span.SetTag(OpenTracingTagVariables, variables)
}
defer span.Finish()
data, errs := exec.ExecuteRequest(subCtx, s.exec, document, operationName, variables)
if len(errs) != 0 {
ext.Error.Set(span, true)
span.SetTag(OpenTracingTagError, errs)
}
return &Response{
Data: data,
Errors: errs,
}
}
func (s *Schema) Inspect() *introspection.Schema {
return &introspection.Schema{Schema: s.schema}
}
func (s *Schema) ToJSON() ([]byte, error) {
result, err := exec.IntrospectSchema(s.schema)
if err != nil {
return nil, err
}
return json.MarshalIndent(result, "", "\t")
}