-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathgateway.go
421 lines (362 loc) · 11.8 KB
/
gateway.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package gateway
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/vektah/gqlparser/v2/ast"
"github.com/nautilus/graphql"
)
// Gateway is the top level entry for interacting with a gateway. It is responsible for merging a list of
// remote schemas into one, generating a query plan to execute based on an incoming request, and following
// that plan
type Gateway struct {
sources []*graphql.RemoteSchema
schema *ast.Schema
planner QueryPlanner
executor Executor
logger Logger
merger Merger
middlewares MiddlewareList
queryFields []*QueryField
queryerFactory *QueryerFactory
queryPlanCache QueryPlanCache
locationPriorities []string
// group up the list of middlewares at startup to avoid it during execution
requestMiddlewares []graphql.NetworkMiddleware
responseMiddlewares []ResponseMiddleware
// the urls we have to visit to access certain fields
fieldURLs FieldURLMap
}
// RequestContext holds all of the information required to satisfy the user's query
type RequestContext struct {
Context context.Context
Query string
OperationName string
Variables map[string]interface{}
CacheKey string
}
func (g *Gateway) GetPlans(ctx *RequestContext) (QueryPlanList, error) {
// let the persister grab the plan for us
return g.queryPlanCache.Retrieve(&PlanningContext{
Query: ctx.Query,
Schema: g.schema,
Gateway: g,
Locations: g.fieldURLs,
}, &ctx.CacheKey, g.planner)
}
// Execute takes a query string, executes it, and returns the response
func (g *Gateway) Execute(ctx *RequestContext, plans QueryPlanList) (map[string]interface{}, error) {
// the plan we mean to execute
var plan *QueryPlan
// if there is only one plan (one operation) then use it
if len(plans) == 1 {
plan = plans[0]
} else {
// if we weren't given an operation name then we don't know which one to send
if ctx.OperationName == "" {
return nil, errors.New("please provide an operation name")
}
// find the plan for the right operation
operationPlan, err := plans.ForOperation(ctx.OperationName)
if err != nil {
return nil, err
}
// use the one for the operation
plan = operationPlan
}
// build up the execution context
executionContext := &ExecutionContext{
logger: g.logger,
RequestContext: ctx.Context,
RequestMiddlewares: g.requestMiddlewares,
Plan: plan,
Variables: ctx.Variables,
}
// TODO: handle plans of more than one query
// execute the plan and return the results
result, executeErr := g.executor.Execute(executionContext)
if executeErr != nil && len(result) == 0 {
result = nil
}
// now that we have our response, throw it through the list of middlewarse
for _, ware := range g.responseMiddlewares {
if err := ware(executionContext, result); err != nil {
return nil, err
}
}
// we're done here
return result, executeErr
}
func (g *Gateway) internalSchema() (*ast.Schema, error) {
// we start off with the internal schema
schema, err := graphql.LoadSchema(`
interface Node {
id: ID!
}
type Query {
node(id: ID!): Node
}
`)
if schema == nil {
return nil, fmt.Errorf("Syntax error in schema string: %w", err)
}
// then we have to add any query fields we have
for _, field := range g.queryFields {
if field.Name != "node" { // skip internal Query field name
schema.Query.Fields = append(schema.Query.Fields, &ast.FieldDefinition{
Name: field.Name,
Type: field.Type,
Arguments: field.Arguments,
})
}
}
// we're done
return schema, nil
}
// New instantiates a new schema with the required stuffs.
func New(sources []*graphql.RemoteSchema, configs ...Option) (*Gateway, error) {
// if there are no source schemas
if len(sources) == 0 {
return nil, errors.New("a gateway must have at least one schema")
}
// set any default values before we start doing stuff with it
gateway := &Gateway{
sources: sources,
planner: &MinQueriesPlanner{},
executor: &ParallelExecutor{},
logger: &DefaultLogger{},
merger: MergerFunc(mergeSchemas),
queryFields: []*QueryField{makeNodeField()},
queryPlanCache: &NoQueryPlanCache{},
}
// pass the gateway through any Options
for _, config := range configs {
config(gateway)
}
// if we have a queryer factory to assign
if gateway.queryerFactory != nil {
// if the planner can accept the factory
if planner, ok := gateway.planner.(PlannerWithQueryerFactory); ok {
gateway.planner = planner.WithQueryerFactory(gateway.queryerFactory)
}
}
// if we have location priorities to assign
if gateway.locationPriorities != nil {
// if the planner can accept the priorities
if planner, ok := gateway.planner.(PlannerWithLocationPriorities); ok {
gateway.planner = planner.WithLocationPriorities(gateway.locationPriorities)
}
}
internal, err := gateway.internalSchema()
if err != nil {
return nil, err
}
// find the field URLs before we merge schemas. We need to make sure to include
// the fields defined by the gateway's internal schema
urls := fieldURLs(sources, true).Concat(
fieldURLs([]*graphql.RemoteSchema{
{
URL: internalSchemaLocation,
Schema: internal,
},
},
false,
),
)
// grab the schemas within each source
sourceSchemas := []*ast.Schema{}
for _, source := range sources {
sourceSchemas = append(sourceSchemas, source.Schema)
}
sourceSchemas = append(sourceSchemas, internal)
// merge them into one
schema, err := gateway.merger.Merge(sourceSchemas)
if err != nil {
// if something went wrong during the merge, return the result
return nil, err
}
// the default request middlewares
requestMiddlewares := []graphql.NetworkMiddleware{}
// before we do anything that the user tells us to, we have to scrub the fields
responseMiddlewares := []ResponseMiddleware{scrubInsertionIDs}
// pull out the middlewares once here so that we don't have
// to do it on every execute
for _, mware := range gateway.middlewares {
switch mware := mware.(type) {
case ResponseMiddleware:
responseMiddlewares = append(responseMiddlewares, mware)
case RequestMiddleware:
requestMiddlewares = append(requestMiddlewares, graphql.NetworkMiddleware(mware))
default:
}
}
// we should be able to ask for the id under a gateway field without going to another service
// that requires that the gateway knows that it is a place it can get the `id`
for _, field := range gateway.queryFields {
urls.RegisterURL(field.Type.Name(), "id", internalSchemaLocation)
}
// assign the computed values
gateway.schema = schema
gateway.fieldURLs = urls
gateway.requestMiddlewares = requestMiddlewares
gateway.responseMiddlewares = responseMiddlewares
// we're done here
return gateway, nil
}
// Option is a function to be passed to New that configures the
// resulting schema
type Option func(*Gateway)
// WithPlanner returns an Option that sets the planner of the gateway
func WithPlanner(p QueryPlanner) Option {
return func(g *Gateway) {
g.planner = p
}
}
// WithExecutor returns an Option that sets the executor of the gateway
func WithExecutor(e Executor) Option {
return func(g *Gateway) {
g.executor = e
}
}
// WithMerger returns an Option that sets the merger of the gateway
func WithMerger(m Merger) Option {
return func(g *Gateway) {
g.merger = m
}
}
// WithMiddlewares returns an Option that adds middlewares to the gateway
func WithMiddlewares(middlewares ...Middleware) Option {
return func(g *Gateway) {
g.middlewares = append(g.middlewares, middlewares...)
}
}
// WithQueryFields returns an Option that adds the given query fields to the gateway
func WithQueryFields(fields ...*QueryField) Option {
return func(g *Gateway) {
g.queryFields = append(g.queryFields, fields...)
}
}
// WithQueryerFactory returns an Option that changes the queryer used by the planner
// when generating plans that interact with remote services.
func WithQueryerFactory(factory *QueryerFactory) Option {
return func(g *Gateway) {
g.queryerFactory = factory
}
}
func WithLocationPriorities(priorities []string) Option {
return func(g *Gateway) {
g.locationPriorities = priorities
}
}
// WithLogger returns an Option that sets the logger of the gateway
func WithLogger(l Logger) Option {
return func(g *Gateway) {
g.logger = l
}
}
func makeNodeField() *QueryField {
return &QueryField{
Name: "node",
Type: ast.NamedType("Node", &ast.Position{}),
Arguments: ast.ArgumentDefinitionList{
&ast.ArgumentDefinition{
Name: "id",
Type: ast.NonNullNamedType("ID", &ast.Position{}),
},
},
Resolver: func(ctx context.Context, args map[string]interface{}) (string, error) {
id := args["id"]
if id == nil {
return "", fmt.Errorf("argument 'id' is required")
}
idStr, ok := id.(string)
if !ok {
jsonID, err := json.Marshal(id)
if err != nil {
return "", fmt.Errorf("invalid ID type")
}
return "", fmt.Errorf("invalid ID type: %s", string(jsonID))
}
return idStr, nil
},
}
}
func fieldURLs(schemas []*graphql.RemoteSchema, stripInternal bool) FieldURLMap {
// build the mapping of fields to urls
locations := FieldURLMap{}
// every schema we were given could define types
for _, remoteSchema := range schemas {
// each type defined by the schema can be found at remoteSchema.URL
for name, typeDef := range remoteSchema.Schema.Types {
// if the type is part of the introspection (and can't be left up to the backing services)
if !strings.HasPrefix(typeDef.Name, "__") || !stripInternal {
// you can ask for __typename at any service that defines the type
locations.RegisterURL(name, "__typename", remoteSchema.URL)
// each field of each type can be found here
for _, fieldDef := range typeDef.Fields {
// if the field is not an introspection field
if !(name == typeNameQuery && strings.HasPrefix(fieldDef.Name, "__")) {
locations.RegisterURL(name, fieldDef.Name, remoteSchema.URL)
} else if !stripInternal { // its an introspection name
// register the location for the field
locations.RegisterURL(name, fieldDef.Name, remoteSchema.URL)
}
}
}
}
}
// return the location map
return locations
}
// FieldURLMap holds the intformation for retrieving the valid locations one can find the value for the field
type FieldURLMap map[string][]string
// URLFor returns the list of locations one can find parent.field.
func (m FieldURLMap) URLFor(parent string, field string) ([]string, error) {
// compute the key for the field
key := m.keyFor(parent, field)
// look up the value in the map
value, exists := m[key]
// if it doesn't exist
if !exists {
return []string{}, fmt.Errorf("Could not find location for %s", key)
}
// return the value to the caller
return value, nil
}
// Concat returns a new field map url whose entries are the union of both maps
func (m FieldURLMap) Concat(other FieldURLMap) FieldURLMap {
for key, value := range other {
// if we have seen the location before
if prevValue, ok := m[key]; ok {
// add the values to the internal registry
m[key] = append(prevValue, value...)
// we havent' seen the key before
} else {
m[key] = value
}
}
// return the
return m
}
// RegisterURL adds a new location to the list of possible places to find the value for parent.field
func (m FieldURLMap) RegisterURL(parent string, field string, locations ...string) {
for _, location := range locations {
// compute the key for the field
key := m.keyFor(parent, field)
// look up the value in the map
_, exists := m[key]
// if we haven't seen this key before
if !exists {
// create a new list
m[key] = []string{location}
} else {
// we've seen this key before
m[key] = append(m[key], location)
}
}
}
func (m FieldURLMap) keyFor(parent string, field string) string {
return fmt.Sprintf("%s.%s", parent, field)
}