Unlock the power of graphql-shield in your nexus app
npm install nexus-plugin-shield
- fragments not supported
// app.ts
import { use } from 'nexus'
import { shield, rule, deny, not, and, or } from 'nexus-plugin-shield'
const isAuthenticated = rule({ cache: 'contextual' })(
async (parent, args, ctx: NexusContext, info) => {
return ctx.user !== null
}
)
const isAdmin = rule({ cache: 'contextual' })(
async (parent, args, ctx: NexusContext, info) => {
return ctx.user.role === 'admin'
}
)
const isEditor = rule({ cache: 'contextual' })(
async (parent, args, ctx: NexusContext, info) => {
return ctx.user.role === 'editor'
}
)
const permissions = shield({
rules: {
Query: {
frontPage: not(isAuthenticated),
fruits: and(isAuthenticated, or(isAdmin, isEditor)),
customers: and(isAuthenticated, isAdmin),
},
Mutations: {
addFruitToBasket: isAuthenticated,
},
Fruit: isAuthenticated,
Customer: isAdmin,
},
options: {
fallbackRule: deny,
},
})
use(permissions)
A rule map must match your schema definition.
Nexus provide a global NexusContext
interface you can use in your rules:
rule()(async (parent, args, context: NexusContext, info) => {
// logic
})