@graphql-authz/apollo-server-plugin

Apollo Server plugin for graphql-authz

Usage no npm install needed!

<script type="module">
  import graphqlAuthzApolloServerPlugin from 'https://cdn.skypack.dev/@graphql-authz/apollo-server-plugin';
</script>

README

@graphql-authz/apollo-server-plugin

Configuring Apollo Server

Ensure context parser is configured to perform authentication and add user info to context

const server = new ApolloServer({
  ...
  context: ({ req }) => {
    return {
      user: someHowAuthenticateUser(req.get("authorization"))),
    };
  },
  ...
});

For Directives usage

Apply authZDirectiveTransformer to schema and add plugin to apollo-server

  import { makeExecutableSchema } from '@graphql-tools/schema';
  import { authZDirective } from '@graphql-authz/directive';
  import { authZApolloPlugin } from "@graphql-authz/apollo-server-plugin";

  const { authZDirectiveTransformer } = authZDirective();

  const schema = makeExecutableSchema({
    typeDefs,
    resolvers
  });
  
  const transformedSchema = authZDirectiveTransformer(schema);

  const server = new ApolloServer({
    ...
    schema: transformedSchema,
    plugins: [authZApolloPlugin({ rules })],
    ...
  });

For Extensions usage

Add plugin to apollo-server

  import { authZApolloPlugin } from "@graphql-authz/apollo-server-plugin";;

  const server = new ApolloServer({
    ...
    plugins: [authZApolloPlugin({ rules })],
    ...
  });

For AuthSchema usage

Add plugin to apollo-server with an additional parameter authSchema

  import { authZApolloPlugin } from "@graphql-authz/apollo-server-plugin";;

  const server = new ApolloServer({
    ...
    plugins: [authZApolloPlugin({ rules, authSchema })],
    ...
  });