-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shane Froebel
committed
Nov 10, 2023
1 parent
2c41d91
commit 38c1140
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) |