Automatically generate GraphQL Shield permissions from your GraphQL schema
Installation β’ Quick Start β’ Features β’ API Reference β’ Examples
β Star us on GitHub if this project helped you!
Transform your GraphQL schema into a complete permission system in seconds. No more manually writing repetitive shield configurationsβlet the generator handle it while you focus on your business logic.
// β¨ From this schema...
type User {
id: ID!
email: String!
posts: [Post!]!
}
type Query {
users: [User!]!
me: User
}
// π To this shield instantly
export const permissions = shield({
Query: {
users: hasAuth,
me: hasAuth,
},
User: {
posts: hasAuth,
},
});
# npm
npm install graphql-shield-generator
# yarn
yarn add graphql-shield-generator
# pnpm
pnpm add graphql-shield-generator
# bun
bun add graphql-shield-generator
import { generateGraphqlShield } from 'graphql-shield-generator';
await generateGraphqlShield({
schema: mySchema, // or { typeDefs, resolvers }
options: {
outputDir: './permissions',
fileName: 'shield',
extension: 'ts',
moduleSystem: 'ES modules',
},
});
await generateGraphqlShield({
schema: { typeDefs, resolvers },
options: {
outputDir: './permissions',
fileName: 'shield',
extension: 'ts',
moduleSystem: 'ES modules',
// π Use custom authentication rules
customrule: 'hasAuth',
customrulepath: './auth/rules',
// π‘οΈ Security-first approach
fallbackRule: 'deny', // Deny by default, allow explicitly
// π Organized output
groupbyobjects: true, // Order by Query β Mutation β Subscription
// βοΈ Shield configuration
shieldoptions: '{ allowExternalErrors: true }',
},
});
Replace default allow
with your custom rules:
customrule: 'hasAuth',
customrulepath: './auth/hasAuth'
Configure default permissions for unlisted fields:
fallbackRule: 'deny' // '*': deny (secure by default)
fallbackRule: 'allow' // '*': allow (permissive)
fallbackRule: false // No fallback (clean output)
Organize your shield with logical type priority:
groupbyobjects: true // Query β Mutation β Subscription β Custom Types
Pass options directly to the GraphQL Shield constructor:
shieldoptions: '{ allowExternalErrors: true, debug: true }'
Generate .ts
files with proper imports and types.
Support for both ES modules and CommonJS.
import { shield, deny } from 'graphql-shield';
import { hasAuth } from './auth/hasAuth';
export const permissions = shield({
Query: {
'*': deny,
users: hasAuth,
me: hasAuth,
},
Mutation: {
'*': deny,
createUser: hasAuth,
updateUser: hasAuth,
},
User: {
'*': deny,
posts: hasAuth,
},
}, { allowExternalErrors: true });
import { shield } from 'graphql-shield';
import { hasAuth } from './auth/hasAuth';
export const permissions = shield({
Query: {
users: hasAuth,
me: hasAuth,
},
User: {
posts: hasAuth,
},
});
Option | Type | Default | Description |
---|---|---|---|
schema |
GraphQLSchema | { typeDefs, resolvers } |
- | Your GraphQL schema |
options.outputDir |
string |
'.' |
Output directory |
options.fileName |
string |
'shield' |
Generated file name |
options.extension |
'js' | 'ts' |
'js' |
File extension |
options.moduleSystem |
'CommonJS' | 'ES modules' |
'CommonJS' |
Module system |
options.customrule |
string |
'allow' |
Custom rule function name |
options.customrulepath |
string |
- | Import path for custom rule |
options.fallbackRule |
'allow' | 'deny' | false |
false |
Default rule for unspecified fields |
options.groupbyobjects |
boolean |
false |
Order types by priority |
options.shieldoptions |
string |
- | Options passed to shield constructor |
Check out the /example
directory for comprehensive examples including:
- Basic shield generation
- Custom authentication rules
- Security-first configurations
- All feature combinations
- CommonJS and ES modules examples
Run the examples:
cd example
npm install
npm run test-fixes
- Secure by default with
fallbackRule: 'deny'
- Custom authentication rules
- Organized type structure
- Quick shield generation
- Permissive defaults for development
- Easy customization as you grow
- Automatic shield updates when schema changes
- Consistent permission structure
- No manual maintenance
We welcome contributions! Please see our Contributing Guide for details.
git clone https://github.com/omar-dulaimi/graphql-shield-generator.git
cd graphql-shield-generator
npm install
npm run build
MIT Β© Omar Dulaimi
Made with β€οΈ by Omar Dulaimi