Giter VIP home page Giter VIP logo

simple-auth-directive's Introduction

Simple GraphQL Auth Directive


Apollo GraphOS now has built-in authentication and authorization features. Checkout our launch blog post for more details.


A simple example of how you might use an authentication and authorization directive in GraphQL. This is NOT a production ready-library.

The code in this repository is experimental and has been provided for reference purposes only. Community feedback is welcome but this project may not be supported in the same way that repositories in the official Apollo GraphQL GitHub organization are. If you need help you can file an issue on this repository, contact Apollo to talk to an expert, or create a ticket directly in Apollo Studio.

Installation

This package is NOT published npm. It is only available as a direct installation from GitHub.

npm i github:apollosolutions/simple-auth-directive

Usage

Define the following directives with these specific roles in your GraphQL typedefs.

directive @authenticated repeatable on OBJECT | FIELD_DEFINITION
directive @hasRole(requires: Role = ADMIN) on OBJECT | FIELD_DEFINITION

enum Role {
  ADMIN
  PARTNER
  USER
  UNKNOWN
}

Add the directive to types or fields that you want to restrict access to

type Query {
  getProducts: [Product] @hasRole(requires: PARTNER)
  getUser: User @authenticated
}

type User @hasRole(requires: USER) {
  name: String
  accounts: [Account] @hasRole(requires: PARTNER)
  ssn: String @hasRole(requires: ADMIN)
}

Import the function to get a schema transformer and modify your schema resolvers

import { authDirectiveTransformer } from "@apollosolutions/simple-auth-directive";

const newSchema = authDirectiveTransformer(schema);

Make sure the headers are included in the GraphQL context so that the directive can parse them. They must be saved at context.headers.

const { url } = await startStandaloneServer(server, {
    listen: { port: 4000 },
    context: async ({ req }) => ({ headers: req.headers })
});

Known Limitations

  • No custom roles
  • No custom authentication logic
  • No custom directive name

simple-auth-directive's People

Contributors

smyrick avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

gabrieldeleles

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.