Skip to content

Commit

Permalink
docs: added full example
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane Froebel committed Nov 10, 2023
1 parent 2c41d91 commit 38c1140
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions examples/result-filter-replace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict'

const Fastify = require('fastify')
const mercurius = require('mercurius')
const mercuriusAuth = require('..')

const app = Fastify()

const schema = `
directive @filterData (disallow: String!) on FIELD_DEFINITION
type Message {
message: String!
notes: String @filterData (disallow: "no-read-notes")
}
type Query {
publicMessages: [Message!]
}
`

const messages = [
{
title: 'one',
message: 'acme one',
notes: 'acme one',
password: 'acme-one'
},
{
title: 'two',
message: 'acme two',
notes: 'acme two',
password: 'acme-two'
}
]

const resolvers = {
Query: {
publicMessages: async (parent, args, context, info) => {
return messages
}
}
}

app.register(mercurius, {
schema,
resolvers
})

app.register(mercuriusAuth, {
authContext (context) {
const headerValue = context.reply.request.headers['x-permission']
return { permission: headerValue ? headerValue.split(',') : [] }
},
async applyPolicy (authDirectiveAST, parent, args, context, info) {
const notNeeded = authDirectiveAST.arguments.find(arg => arg.name.value === 'disallow').value.value

return !context.auth.permission.includes(notNeeded)
},
outputPolicyErrors: {
enabled: false
},
filterSchema: true,
authDirective: 'filterData'
})

app.listen({ port: 3000 })

0 comments on commit 38c1140

Please sign in to comment.