Skip to content
This repository has been archived by the owner on Oct 7, 2021. It is now read-only.

anigenero/aws-cognito-graphql-directive

Repository files navigation

aws-cognito-graphql-directive

Build Status codecov

Directive to check authentication against AWS cognito

Installation

> npm install

Setup

Configuration

Using the Context Function

import { AuthDirective, authTypeDefs, getAuthContext } from 'aws-cognito-graphql-drective';
import { merge } from 'lodash';

export const handler = new ApolloServer({
	context: async ({headers}) => ({ 
            auth: await getAuthContext(headers, configuration) 
        }),
	typeDefs: merge(authTypeDefs, myTypeDefs),
	// ..
	schemaDirectives: {
		auth: AuthDirective
	}
}).createHandler();

Requiring authentication

type Query {

    anonymousQuery: MyResult
    authRequiredQuery: MyResult @auth
    adminGroupOnlyQuery: MyResult @auth(groups: ["admin"])

}

Utilities

This library includes utilities for easy, quick setup in common environments.

Apollo Lambda w/ APIGateway

import {generateLambdaContextFromAPIGateway} from 'aws-cognito-graphql-directive';

new ApolloServer({
    context: generateLambdaContextFromAPIGateway(configuration, {
        // ... other context
    })
})