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

@graphql-directive/auth

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@graphql-directive/auth

GraphQL authorization directive

  • 0.0.1-alpha.36
  • canary
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

GraphQL Authorization Directive

Node.js CI Coverage Status

A TypeScript/JavaScript library that provides an easy way to add authorization logic to your Node.js GraphQL API using directives.

Motivation

GraphQL Authorization Directive aims to simplify the process of adding authorization to your GraphQL API. By using directives, you can easily apply authorization logic to your schema without having to manually implement complex middleware functions.

Example Usage

import auth from "@graphql-directive/auth"
import { ApolloServer } from "@apollo/server"
import { startStandaloneServer } from "@apollo/server/standalone"
import { makeExecutableSchema } from "@graphql-tools/schema"

const typeDefs = `
    type User {
        name: String!
        email: String!
        role: String @authorize(policy: "admin")
    }
    input UserInput {
        name: String!
        email: String! 
        role: String @authorize(policy: "admin")
    }
    type Query {
        getUsers: [User]!
    }
    type Mutation {
        addUser(user:UserInput!): Boolean!
        modifyUser(user: UserInput!): Boolean!
    }
`

const transform = auth.createTransformer({
    policies: {
        admin: ({ contextValue }) => contextValue.user.role === "admin"
        isLogin: ({ contextValue }) => !!contextValue.user
    }
})

const schema = transform(makeExecutableSchema({
    typeDefs: [auth.typeDefs, typeDefs],
    resolver: {
        Query: { getUsers: () => ([]) },
        Mutation: {
            addUser: () => true,
            modifyUser: ()=> true
        }
    }
}))

const server = new ApolloServer({ schema })
startStandaloneServer(server, { context: async ({ req, res }) => ({}) }).then(x => console.log(x.url))

API Documentation

Keywords

FAQs

Package last updated on 06 Apr 2023

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