forked from mercurius-js/validation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
33 lines (26 loc) · 990 Bytes
/
index.js
File metadata and controls
33 lines (26 loc) · 990 Bytes
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
'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
app.graphql.addHook('onGatewayReplaceSchema', async (instance, schema) => {
validation.registerValidationSchema(schema)
})
},
{
name: 'mercurius-validation',
fastify: '>=3.x',
dependencies: ['mercurius']
}
)
mercuriusValidation.graphQLDirective = validationDirective
mercuriusValidation.graphQLTypeDefs = validationTypeDefs
module.exports = mercuriusValidation