Directives Plugin

A plugin for using schema directives with schemas generated by GiraphQL.

Schema Directives are not intended to be used with code first schemas, but there is a large existing community with several very useful directives based



yarn add @giraphql/plugin-directives


import DirectivePlugin from '@giraphql/plugin-directives';
import { SchemaDirectiveVisitor } from 'apollo-server';
import { createRateLimitDirective } from 'graphql-rate-limit-directive';
const builder = new SchemaBuilder<
Directives: {
rateLimit: {
args: { limit: number, duration: number };
useGraphQLToolsUnorderedDirectives: true,
plugins: [DirectivePlugin],
directives: {
rateLimit: { limit: 5, duration: 60 },
fields: (t) => ({
hello: t.string({ resolve: () => 'world' });
const schema = builder.toSchema();
SchemaDirectiveVisitor.visitSchemaDirectives(schema, {
rateLimit: createRateLimitDirective(),

The directives plugin allows you to define types for the directives your schema will use the SchemaTypes parameter. Each directive can define a set of locations the directive can appear, and an object type representing the arguments the directive accepts.

The valid locations for directives are:



  • ENUM









GiraphQL does not apply the directives itself, this plugin simply adds directive information to the extensions property of the underlying GraphQL type so that it can be consumed by other tools like graphql-tools.

By default this plugin uses the format that Gatsby uses (described here). This format is currently not supported by graphql-tools. To support graphql-tools based directives like the rate-limit directive from the example above you can set the useGraphQLToolsUnorderedDirectives option. This option does not preserve the order that directives are defined in. This will be okay for most cases, but may cause issues if your directives need to be applied in a specific order.

To define directives on your fields or types, you can add a directives property in any of the supported locations using one of the following 2 formats:

directives: [
name: "validation",
args: {
regex: "/abc+/"
name: "required",
args: {},
// or
directives: {
validation: {
regex: "/abc+/"
required: {}

Each of these applies the same 2 directives. The first format is preferred, especially when using directives that are sensitive to ordering, or can be repeated multiple times for the same location.