Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

graphql-shield

Package Overview
Dependencies
Maintainers
4
Versions
163
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-shield

GraphQL Server permissions as another layer of abstraction!

  • 7.6.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
114K
decreased by-17.32%
Maintainers
4
Weekly downloads
 
Created

What is graphql-shield?

graphql-shield is a library for creating permission layers for your GraphQL server. It allows you to define rules and apply them to your schema to protect your data from unauthorized access.

What are graphql-shield's main functionalities?

Defining Permissions

This feature allows you to define rules for your GraphQL schema. In this example, the `isAuthenticated` rule checks if the user is authenticated before allowing access to the `user` query.

const { rule, shield, and, or, not } = require('graphql-shield');

const isAuthenticated = rule()((parent, args, ctx, info) => {
  return ctx.user !== null;
});

const permissions = shield({
  Query: {
    user: isAuthenticated,
  },
});

Combining Rules

This feature allows you to combine multiple rules using logical operators like `and`, `or`, and `not`. In this example, the `adminData` query is protected by both `isAuthenticated` and `isAdmin` rules.

const { rule, shield, and, or, not } = require('graphql-shield');

const isAuthenticated = rule()((parent, args, ctx, info) => {
  return ctx.user !== null;
});

const isAdmin = rule()((parent, args, ctx, info) => {
  return ctx.user.role === 'admin';
});

const permissions = shield({
  Query: {
    adminData: and(isAuthenticated, isAdmin),
  },
});

Error Handling

This feature allows you to handle errors gracefully. You can specify custom error messages for individual rules or provide a fallback error message for the entire permission layer.

const { rule, shield } = require('graphql-shield');

const isAuthenticated = rule({
  cache: 'contextual',
  error: 'Not Authenticated',
})((parent, args, ctx, info) => {
  return ctx.user !== null;
});

const permissions = shield({
  Query: {
    user: isAuthenticated,
  },
}, {
  fallbackError: 'You are not authorized to access this resource',
});

Other packages similar to graphql-shield

FAQs

Package last updated on 22 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc