-
Notifications
You must be signed in to change notification settings - Fork 25
/
gqlserver.go
145 lines (135 loc) · 3.67 KB
/
gqlserver.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
// Package gqlserver provides a GraphQL server.
// It is a singleton initialized via init() functions.
package gqlserver
import (
"context"
"net/http"
"reflect"
"strings"
"time"
"github.com/bhoriuchi/graphql-go-tools/handler"
"github.com/functionalfoundry/graphqlws"
"github.com/graphql-go/graphql"
goutils "github.com/onichandame/go-utils"
gqlwsserver "github.com/onichandame/gql-ws/server"
"github.com/sirupsen/logrus"
"github.com/usnistgov/ndn-dpdk/core/logging"
"github.com/usnistgov/ndn-dpdk/core/version"
"go.uber.org/zap"
)
var logger = logging.New("gqlserver")
// Schema is the singleton of graphql.SchemaConfig.
// It is available until Prepare() is called.
var Schema = &graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{},
}),
Mutation: graphql.NewObject(graphql.ObjectConfig{
Name: "Mutation",
Fields: graphql.Fields{},
}),
Subscription: graphql.NewObject(graphql.ObjectConfig{
Name: "Subscription",
Fields: graphql.Fields{},
}),
}
// AddQuery adds a top-level query field.
func AddQuery(f *graphql.Field) {
Schema.Query.AddFieldConfig(f.Name, f)
}
// AddMutation adds a top-level mutation field.
func AddMutation(f *graphql.Field) {
name, resolve := f.Name, f.Resolve
f.Resolve = func(p graphql.ResolveParams) (any, error) {
defer func() {
if e := recover(); e != nil {
logger.Error("panic in GraphQL mutation resolver",
zap.String("name", name),
zap.Any("error", e),
zap.StackSkip("stack", 2),
)
panic(e)
}
}()
return resolve(p)
}
Schema.Mutation.AddFieldConfig(f.Name, f)
}
// AddSubscription adds a top-level subscription field.
func AddSubscription(f *graphql.Field) {
if f.Resolve == nil {
f.Resolve = func(p graphql.ResolveParams) (any, error) {
return p.Info.RootValue, nil
}
}
if subscribe := f.Subscribe; subscribe != nil {
f.Subscribe = func(p graphql.ResolveParams) (any, error) {
var stop chan any
goutils.Try(func() { stop = gqlwsserver.GetSubscriptionStopSig(p.Context) })
if stop != nil {
ctx, cancel := context.WithCancel(p.Context)
go func() {
<-stop
cancel()
}()
p.Context = ctx
}
return subscribe(p)
}
}
Schema.Subscription.AddFieldConfig(f.Name, f)
}
func init() {
versionType := graphql.NewObject(graphql.ObjectConfig{
Name: "Version",
Fields: BindFields[version.Version](FieldTypes{
reflect.TypeOf(time.Time{}): graphql.DateTime,
}),
})
AddQuery(&graphql.Field{
Name: "version",
Type: graphql.NewNonNull(versionType),
Resolve: func(p graphql.ResolveParams) (any, error) {
return version.V, nil
},
})
}
// Prepare compiles the schema and adds handlers on http.DefaultServeMux.
func Prepare() {
sch, e := graphql.NewSchema(*Schema)
if e != nil {
logger.Panic("graphql.NewSchema",
zap.Error(e),
zap.Any("schema", Schema),
)
}
Schema = nil
logrus.SetLevel(logrus.PanicLevel)
wsHandler := graphqlws.NewHandler(graphqlws.HandlerConfig{
SubscriptionManager: newSubManager(context.Background(), &sch),
})
twsHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gqlwsserver.NewSocket(&gqlwsserver.Config{
Response: w,
Request: r,
Schema: &sch,
})
})
httpHandler := handler.New(&handler.Config{
Schema: &sch,
Pretty: true,
PlaygroundConfig: handler.NewDefaultPlaygroundConfig(),
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("connection") == "Upgrade" {
if strings.Contains(r.Header.Get("sec-websocket-protocol"), "graphql-transport-ws") {
twsHandler.ServeHTTP(w, r)
} else {
wsHandler.ServeHTTP(w, r)
}
} else {
httpHandler.ServeHTTP(w, r)
}
})
}