forked from mercurius-js/mercurius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
569 lines (491 loc) · 17.1 KB
/
index.js
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
'use strict'
const fp = require('fastify-plugin')
const LRU = require('tiny-lru')
const routes = require('./lib/routes')
const { compileQuery } = require('graphql-jit')
const { Factory } = require('single-user-cache')
const {
parse,
buildSchema,
getOperationAST,
GraphQLObjectType,
GraphQLScalarType,
GraphQLEnumType,
GraphQLInterfaceType,
GraphQLUnionType,
GraphQLSchema,
extendSchema,
validate,
validateSchema,
specifiedRules,
execute
} = require('graphql')
const { buildExecutionContext } = require('graphql/execution/execute')
const queryDepth = require('./lib/queryDepth')
const buildFederationSchema = require('./lib/federation')
const buildGateway = require('./lib/gateway')
const mq = require('mqemitter')
const { PubSub, withFilter } = require('./lib/subscriber')
const persistedQueryDefaults = require('./lib/persistedQueryDefaults')
const {
ErrorWithProps,
defaultErrorFormatter,
addErrorsToExecutionResult,
MER_ERR_GQL_INVALID_SCHEMA,
MER_ERR_GQL_GATEWAY,
MER_ERR_GQL_VALIDATION,
MER_ERR_INVALID_OPTS,
MER_ERR_METHOD_NOT_ALLOWED,
MER_ERR_INVALID_METHOD
} = require('./lib/errors')
const { Hooks, assignLifeCycleHooksToContext, assignApplicationLifecycleHooksToContext } = require('./lib/hooks')
const { kLoaders, kFactory, kHooks } = require('./lib/symbols')
const { preParsingHandler, preValidationHandler, preExecutionHandler, onResolutionHandler, onGatewayReplaceSchemaHandler } = require('./lib/handlers')
function buildCache (opts) {
if (Object.prototype.hasOwnProperty.call(opts, 'cache')) {
const isBoolean = typeof opts.cache === 'boolean'
const isNumber = typeof opts.cache === 'number'
if (isBoolean && opts.cache === false) {
// no cache
return null
} else if (isNumber) {
// cache size as specified
return LRU(opts.cache)
} else if (!isBoolean && !isNumber) {
throw new MER_ERR_INVALID_OPTS('Cache type is not supported')
}
}
// default cache, 1024 entries
return LRU(1024)
}
const plugin = fp(async function (app, opts) {
const lru = buildCache(opts)
const lruErrors = buildCache(opts)
const lruGatewayResolvers = buildCache(opts)
const minJit = opts.jit || 0
const queryDepthLimit = opts.queryDepth
const errorFormatter = typeof opts.errorFormatter === 'function' ? opts.errorFormatter : defaultErrorFormatter
if (opts.persistedQueries) {
if (opts.onlyPersisted) {
opts.persistedQueryProvider = persistedQueryDefaults.preparedOnly(opts.persistedQueries)
// Disable GraphiQL and GraphQL Playground
opts.graphiql = false
opts.ide = false
} else {
opts.persistedQueryProvider = persistedQueryDefaults.prepared(opts.persistedQueries)
}
} else if (opts.onlyPersisted) {
throw new MER_ERR_INVALID_OPTS('onlyPersisted is true but there are no persistedQueries')
}
if (opts.persistedQueryProvider) {
if (opts.persistedQueryProvider.getHash) {
if (!opts.persistedQueryProvider.getQueryFromHash) {
throw new MER_ERR_INVALID_OPTS('persistedQueryProvider: getQueryFromHash is required when getHash is provided')
}
} else {
throw new MER_ERR_INVALID_OPTS('persistedQueryProvider: getHash is required')
}
if (opts.persistedQueryProvider.getHashForQuery) {
if (!opts.persistedQueryProvider.saveQuery) {
throw new MER_ERR_INVALID_OPTS('persistedQueryProvider: saveQuery is required when getHashForQuery is provided')
}
}
}
if (typeof minJit !== 'number') {
throw new MER_ERR_INVALID_OPTS('the jit option must be a number')
}
const root = {}
let schema = opts.schema
let gateway = opts.gateway
const subscriptionOpts = opts.subscription
let emitter
let subscriber
let verifyClient
let subscriptionContextFn
let onConnect
let onDisconnect
if (typeof subscriptionOpts === 'object') {
if (subscriptionOpts.pubsub) {
subscriber = subscriptionOpts.pubsub
} else {
emitter = subscriptionOpts.emitter || mq()
subscriber = new PubSub(emitter)
}
verifyClient = subscriptionOpts.verifyClient
subscriptionContextFn = subscriptionOpts.context
onConnect = subscriptionOpts.onConnect
onDisconnect = subscriptionOpts.onDisconnect
} else if (subscriptionOpts === true) {
emitter = mq()
subscriber = new PubSub(emitter)
}
if (subscriptionOpts) {
fastifyGraphQl.pubsub = subscriber
}
if (gateway && (schema || opts.resolvers || opts.loaders)) {
throw new MER_ERR_INVALID_OPTS('Adding "schema", "resolvers" or "loaders" to plugin options when plugin is running in gateway mode is not allowed')
}
if (Array.isArray(schema)) {
schema = schema.join('\n')
}
if (typeof schema === 'string') {
if (opts.federationMetadata) {
schema = buildFederationSchema(schema)
} else {
schema = buildSchema(schema)
}
} else if (!opts.schema && !gateway) {
schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {}
}),
mutation: opts.defineMutation
? new GraphQLObjectType({
name: 'Mutation',
fields: {}
})
: undefined
})
}
let entityResolversFactory
if (gateway) {
gateway = await buildGateway(gateway, app)
schema = gateway.schema
entityResolversFactory = gateway.entityResolversFactory
let gatewayInterval = null
if (gateway.pollingInterval !== undefined) {
if (typeof gateway.pollingInterval === 'number') {
gatewayInterval = setInterval(async () => {
try {
const context = assignApplicationLifecycleHooksToContext(fastifyGraphQl[kHooks], {})
const schema = await gateway.refresh()
if (schema !== null) {
// Trigger onGatewayReplaceSchema hook
if (context.onGatewayReplaceSchema !== null) {
await onGatewayReplaceSchemaHandler(context, { instance: app, schema })
}
fastifyGraphQl.replaceSchema(schema)
}
} catch (error) {
app.log.error(error)
}
}, gateway.pollingInterval)
} else {
app.log.warn(`Expected a number for 'gateway.pollingInterval', received: ${typeof gateway.pollingInterval}`)
}
}
app.onClose((fastify, next) => {
gateway.close()
if (gatewayInterval !== null) {
clearInterval(gatewayInterval)
}
setImmediate(next)
})
fastifyGraphQl.gateway = gateway
}
fastifyGraphQl.schema = schema
app.addHook('onReady', async function () {
const schemaValidationErrors = validateSchema(fastifyGraphQl.schema)
if (schemaValidationErrors.length === 1) {
throw schemaValidationErrors[0]
} else if (schemaValidationErrors.length > 1) {
const err = new MER_ERR_GQL_INVALID_SCHEMA()
err.errors = schemaValidationErrors
throw err
}
})
const graphqlCtx = Symbol('ctx')
if (opts.routes !== false) {
const optsIde = opts.graphiql || opts.ide
app.register(routes, {
errorHandler: opts.errorHandler,
errorFormatter: opts.errorFormatter,
ide: optsIde,
ideSettings: opts.playgroundSettings,
playgroundHeaders: opts.playgroundHeaders,
prefix: opts.prefix,
path: opts.path,
context: opts.context,
persistedQueryProvider: opts.persistedQueryProvider,
allowBatchedQueries: opts.allowBatchedQueries,
subscriber,
verifyClient,
onConnect,
onDisconnect,
lruGatewayResolvers,
entityResolversFactory,
subscriptionContextFn
})
}
app.decorateReply(graphqlCtx, null)
app.decorateReply('graphql', function (source, context, variables, operationName) {
context = Object.assign({ reply: this, app }, context)
if (app[kFactory]) {
this[kLoaders] = factory.create(context)
}
return app.graphql(source, context, variables, operationName)
})
app.decorate('graphql', fastifyGraphQl)
fastifyGraphQl.replaceSchema = function (s) {
if (!s || typeof s !== 'object') {
throw new MER_ERR_INVALID_OPTS('Must provide valid Document AST')
}
fastifyGraphQl.schema = s
if (lru) {
lru.clear()
}
if (lruErrors) {
lruErrors.clear()
}
if (lruGatewayResolvers) {
lruGatewayResolvers.clear()
}
}
fastifyGraphQl.extendSchema = function (s) {
if (gateway) {
throw new MER_ERR_GQL_GATEWAY('Calling extendSchema method when plugin is running in gateway mode is not allowed')
}
if (opts.federationMetadata) {
throw new MER_ERR_INVALID_METHOD('Calling extendSchema method when federationMetadata is enabled is not allowed')
}
if (typeof s === 'string') {
s = parse(s)
} else if (!s || typeof s !== 'object') {
throw new MER_ERR_INVALID_OPTS('Must provide valid Document AST')
}
fastifyGraphQl.schema = extendSchema(fastifyGraphQl.schema, s)
}
fastifyGraphQl.defineResolvers = function (resolvers) {
if (gateway) {
throw new MER_ERR_GQL_GATEWAY('Calling defineResolvers method when plugin is running in gateway mode is not allowed')
}
for (const name of Object.keys(resolvers)) {
const type = fastifyGraphQl.schema.getType(name)
if (typeof resolvers[name] === 'function') {
root[name] = resolvers[name]
} else if (type instanceof GraphQLObjectType) {
const fields = type.getFields()
const resolver = resolvers[name]
if (resolver.isTypeOf) {
type.isTypeOf = resolver.isTypeOf
delete resolver.isTypeOf
}
for (const prop of Object.keys(resolver)) {
if (name === 'Subscription') {
fields[prop] = {
...fields[prop],
...resolver[prop]
}
} else if (prop === '__resolveReference') {
type.resolveReference = resolver[prop]
} else if (fields[prop]) {
fields[prop].resolve = resolver[prop]
} else {
throw new MER_ERR_INVALID_OPTS(`Cannot find field ${prop} of type ${type}`)
}
}
} else if (type instanceof GraphQLScalarType || type instanceof GraphQLEnumType) {
const resolver = resolvers[name]
for (const prop of Object.keys(resolver)) {
type[prop] = resolver[prop]
}
} else if (type instanceof GraphQLInterfaceType || type instanceof GraphQLUnionType) {
const resolver = resolvers[name]
type.resolveType = resolver.resolveType
} else {
throw new MER_ERR_INVALID_OPTS(`Cannot find type ${name}`)
}
}
}
let factory
fastifyGraphQl.defineLoaders = function (loaders) {
if (gateway) {
throw new MER_ERR_GQL_GATEWAY('Calling defineLoaders method when plugin is running in gateway mode is not allowed')
}
// set up the loaders factory
if (!factory) {
factory = new Factory()
app.decorateReply(kLoaders)
app.decorate(kFactory, factory)
}
function defineLoader (name) {
// async needed because of throw
return async function (obj, params, { reply }) {
if (!reply) {
throw new MER_ERR_INVALID_OPTS('loaders only work via reply.graphql()')
}
return reply[kLoaders][name]({ obj, params })
}
}
const resolvers = {}
for (const typeKey of Object.keys(loaders)) {
const type = loaders[typeKey]
resolvers[typeKey] = {}
for (const prop of Object.keys(type)) {
const name = typeKey + '-' + prop
resolvers[typeKey][prop] = defineLoader(name)
if (typeof type[prop] === 'function') {
factory.add(name, type[prop])
} else {
factory.add(name, type[prop].opts, type[prop].loader)
}
}
}
fastifyGraphQl.defineResolvers(resolvers)
}
fastifyGraphQl.transformSchema = function (schemaTransforms) {
if (!Array.isArray(schemaTransforms)) {
schemaTransforms = [schemaTransforms]
}
for (const transformFn of schemaTransforms) {
fastifyGraphQl.replaceSchema(transformFn(fastifyGraphQl.schema))
}
}
if (opts.resolvers) {
fastifyGraphQl.defineResolvers(opts.resolvers)
}
if (opts.loaders) {
fastifyGraphQl.defineLoaders(opts.loaders)
}
if (opts.schemaTransforms) {
fastifyGraphQl.transformSchema(opts.schemaTransforms)
}
fastifyGraphQl[kHooks] = new Hooks()
// Wrapper that we expose to the user for GraphQL hooks handling
fastifyGraphQl.addHook = function (name, fn) {
this[kHooks].add(name, fn)
}
async function fastifyGraphQl (source, context, variables, operationName) {
context = Object.assign({ app: this, lruGatewayResolvers, errors: null }, context)
context = assignLifeCycleHooksToContext(fastifyGraphQl[kHooks], context)
const reply = context.reply
// Trigger preParsing hook
if (context.preParsing !== null) {
await preParsingHandler({ schema: fastifyGraphQl.schema, source, context })
}
// Parse, with a little lru
const cached = lru !== null && lru.get(source)
let document = null
if (!cached) {
// We use two caches to avoid errors bust the good
// cache. This is a protection against DoS attacks
const cachedError = lruErrors !== null && lruErrors.get(source)
if (cachedError) {
// this query errored
const err = new MER_ERR_GQL_VALIDATION()
err.errors = cachedError.validationErrors
throw err
}
try {
document = parse(source)
} catch (syntaxError) {
const err = new MER_ERR_GQL_VALIDATION()
err.errors = [syntaxError]
throw err
}
// Trigger preValidation hook
if (context.preValidation !== null) {
await preValidationHandler({ schema: fastifyGraphQl.schema, document, context })
}
// Validate
let validationRules = []
if (opts.validationRules) {
if (Array.isArray(opts.validationRules)) {
validationRules = opts.validationRules
} else {
validationRules = opts.validationRules({ source, variables, operationName })
}
}
const validationErrors = validate(fastifyGraphQl.schema, document, [...specifiedRules, ...validationRules])
if (validationErrors.length > 0) {
if (lruErrors) {
lruErrors.set(source, { document, validationErrors })
}
const err = new MER_ERR_GQL_VALIDATION()
err.errors = validationErrors
throw err
}
if (queryDepthLimit) {
const queryDepthErrors = queryDepth(document.definitions, queryDepthLimit)
if (queryDepthErrors.length > 0) {
const err = new MER_ERR_GQL_VALIDATION()
err.errors = queryDepthErrors
throw err
}
}
if (lru) {
lru.set(source, { document, validationErrors, count: 1, jit: null })
}
} else {
document = cached.document
}
if (reply && reply.request.raw.method === 'GET') {
// let's validate we cannot do mutations here
const operationAST = getOperationAST(document, operationName)
if (operationAST.operation !== 'query') {
const err = new MER_ERR_METHOD_NOT_ALLOWED()
err.errors = [new Error('Operation cannot be performed via a GET request')]
throw err
}
}
const shouldCompileJit = cached && cached.count++ === minJit
// Validate variables
if (variables !== undefined && !shouldCompileJit) {
const executionContext = buildExecutionContext(fastifyGraphQl.schema, document, root, context, variables, operationName)
if (Array.isArray(executionContext)) {
const err = new MER_ERR_GQL_VALIDATION()
err.errors = executionContext
throw err
}
}
// Trigger preExecution hook
let modifiedDocument
if (context.preExecution !== null) {
({ modifiedDocument } = await preExecutionHandler({ schema: fastifyGraphQl.schema, document, context }))
}
// minJit is 0 by default
if (shouldCompileJit) {
cached.jit = compileQuery(fastifyGraphQl.schema, modifiedDocument || document, operationName)
}
if (cached && cached.jit !== null) {
const execution = await cached.jit.query(root, context, variables || {})
return maybeFormatErrors(execution, context)
}
const execution = await execute(
fastifyGraphQl.schema,
modifiedDocument || document,
root,
context,
variables,
operationName
)
return maybeFormatErrors(execution, context)
}
async function maybeFormatErrors (execution, context) {
execution = addErrorsToExecutionResult(execution, context.errors)
if (execution.errors) {
const { reply } = context
const { statusCode, response: { data, errors } } = errorFormatter(execution, context)
execution.data = data
execution.errors = errors
if (reply) {
reply.code(statusCode)
}
}
// Trigger onResolution hook
if (context.onResolution !== null) {
await onResolutionHandler({ execution, context })
}
return execution
}
}, {
name: 'mercurius',
fastify: '>=3.x'
})
plugin.ErrorWithProps = ErrorWithProps
plugin.defaultErrorFormatter = defaultErrorFormatter
plugin.persistedQueryDefaults = persistedQueryDefaults
plugin.buildFederationSchema = buildFederationSchema
plugin.withFilter = withFilter
module.exports = plugin