forked from graph-gophers/graphql-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraphql.go
130 lines (110 loc) · 3.04 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
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"
)
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) {
b := New()
if err := b.Parse(schemaString); err != nil {
return nil, err
}
return b.ApplyResolver(resolver)
}
func MustParseSchema(schemaString string, resolver interface{}) *Schema {
s, err := ParseSchema(schemaString, resolver)
if err != nil {
panic(err)
}
return s
}
type SchemaBuilder struct {
schema *schema.Schema
}
func New() *SchemaBuilder {
return &SchemaBuilder{
schema: schema.New(),
}
}
func (b *SchemaBuilder) Parse(schemaString string) error {
return b.schema.Parse(schemaString)
}
func (b *SchemaBuilder) ApplyResolver(resolver interface{}) (*Schema, error) {
e, err2 := exec.Make(b.schema, resolver)
if err2 != nil {
return nil, err2
}
return &Schema{
schema: b.schema,
exec: e,
}, nil
}
func (b *SchemaBuilder) ToJSON() ([]byte, error) {
result, err := exec.IntrospectSchema(b.schema)
if err != nil {
return nil, err
}
return json.MarshalIndent(result, "", "\t")
}
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 {
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,
}
}