forked from mercurius-js/auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
33 lines (26 loc) · 835 Bytes
/
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
'use strict'
const fp = require('fastify-plugin')
const Auth = require('./lib/auth')
const { validateOpts } = require('./lib/validation')
const plugin = fp(
async function (app, opts) {
validateOpts(opts)
// Start auth and register hooks
const auth = new Auth(opts)
// Override resolvers with auth handlers
auth.registerAuthHandlers(app.graphql.schema)
// Add hook to regenerate the resolvers when the schema is refreshed
app.graphql.addHook('onGatewayReplaceSchema', async (instance, schema) => {
auth.registerAuthHandlers(schema)
})
if (typeof opts.authContext !== 'undefined') {
app.graphql.addHook('preExecution', auth.authContextHook.bind(auth))
}
},
{
name: 'mercurius-auth',
fastify: '>=3.x',
dependencies: ['mercurius']
}
)
module.exports = plugin