-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
35 lines (28 loc) · 1.01 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
'use strict'
const fp = require('fastify-plugin')
const Validation = require('./lib/validation')
const { validateOpts } = require('./lib/utils')
const { validationDirective, validationTypeDefs } = require('./lib/directive')
const mercuriusValidation = fp(
async function (app, opts) {
// Validate options
const validatedOpts = validateOpts(opts)
// Start validation and register hooks
const validation = new Validation(app, validatedOpts)
validation.registerValidationSchema(app.graphql.schema)
// Add hook to regenerate the resolvers when the schema is refreshed
if (app.graphqlGateway) {
app.graphqlGateway.addHook('onGatewayReplaceSchema', async (instance, schema) => {
validation.registerValidationSchema(schema)
})
}
},
{
name: 'mercurius-validation',
fastify: '5.x',
dependencies: ['mercurius']
}
)
mercuriusValidation.graphQLDirective = validationDirective
mercuriusValidation.graphQLTypeDefs = validationTypeDefs
module.exports = mercuriusValidation