forked from graph-gophers/graphql-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql.go
135 lines (114 loc) · 3.01 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
package graphql
import (
"context"
"encoding/json"
"fmt"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"reflect"
"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"
)
type ID string
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 {
s := schema.New()
exec.AddBuiltinScalars(s)
exec.AddCustomScalar(s, "ID", reflect.TypeOf(ID("")), func(input interface{}) (interface{}, error) {
switch input := input.(type) {
case ID:
return input, nil
case string:
return ID(input), nil
default:
return nil, fmt.Errorf("wrong type")
}
})
return &SchemaBuilder{
schema: s,
}
}
func (b *SchemaBuilder) Parse(schemaString string) error {
return b.schema.Parse(schemaString)
}
func (b *SchemaBuilder) AddCustomScalar(name string, scalar *ScalarConfig) {
exec.AddCustomScalar(b.schema, name, scalar.ReflectType, scalar.CoerceInput)
}
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
}
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("query", queryString)
if operationName != "" {
span.SetTag("operationName", operationName)
}
if len(variables) != 0 {
span.SetTag("variables", 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("errorMsg", errs)
}
return &Response{
Data: data,
Errors: errs,
}
}
func SchemaToJSON(schemaString string) ([]byte, error) {
b := New()
if err := b.Parse(schemaString); err != nil {
return nil, err
}
result, err := exec.IntrospectSchema(b.schema)
if err != nil {
return nil, err
}
return json.Marshal(result)
}
type ScalarConfig struct {
ReflectType reflect.Type
CoerceInput func(input interface{}) (interface{}, error)
}