The auth context is used to load authentication data onto the MercuriusContext
. The authContext
promise is called within the preExecution
Mercurius GraphQL request hook and when called, the returned data is assigned to MercuriusContext.auth
.
'use strict'
const Fastify = require('fastify')
const mercurius = require('mercurius')
const mercuriusAuth = require('mercurius-auth')
const schema = `
directive @auth(
requires: Role = ADMIN,
) on OBJECT | FIELD_DEFINITION
enum Role {
ADMIN
REVIEWER
USER
UNKNOWN
}
type Query {
add(x: Int, y: Int): Int @auth(requires: USER)
}
`
const resolvers = {
Query: {
add: async (_, { x, y }) => x + y
}
}
const app = Fastify()
app.register(mercurius, {
schema,
resolvers
})
app.register(mercuriusAuth, {
authContext (context) {
return {
identity: context.reply.request.headers['x-user']
}
},
async applyPolicy (authDirectiveAST, parent, args, context, info) {
return context.auth.identity === 'admin'
},
authDirective: 'auth'
})
app.listen({ port: 3000 })
Using a custom preExecution
hook instead of authContext
.
'use strict'
const Fastify = require('fastify')
const mercurius = require('mercurius')
const mercuriusAuth = require('mercurius-auth')
const schema = `
directive @auth(
requires: Role = ADMIN,
) on OBJECT | FIELD_DEFINITION
enum Role {
ADMIN
REVIEWER
USER
UNKNOWN
}
type Query {
add(x: Int, y: Int): Int @auth(requires: USER)
}
`
const resolvers = {
Query: {
add: async (_, { x, y }) => x + y
}
}
async function start () {
const app = Fastify()
app.register(mercurius, {
schema,
resolvers
})
await app.register(mercuriusAuth, {
async applyPolicy (authDirectiveAST, parent, args, context, info) {
return context.other.identity === 'admin'
},
authDirective: 'auth'
})
app.graphql.addHook('preExecution', async (schema, document, context) => {
context.other = {
identity: context.reply.request.headers['x-user']
}
})
app.listen({ port: 3000 })
}
start()