A directive is an identifier preceded by a
@ character, optionally followed by a list of named arguments, which can appear after almost any form of syntax in the GraphQL query or schema languages. Here's an example from the GraphQL draft specification that illustrates several of these possibilities:
As you can see, the usage of
@deprecated(reason: ...) follows the field that it pertains to (
oldField), though the syntax might remind you of "decorators" in other languages, which usually appear on the line above. Directives are typically declared once, using the
directive @deprecated ... on ... syntax, and then used zero or more times throughout the schema document, using the
@deprecated(reason: ...) syntax.
The possible applications of directive syntax are numerous: enforcing access permissions, formatting date strings, auto-generating resolver functions for a particular backend API, marking strings for internationalization, synthesizing globally unique object identifiers, specifying caching behavior, skipping or including or deprecating fields, and just about anything else you can imagine.
This document focuses on directives that appear in GraphQL schemas (as opposed to queries) written in Schema Definition Language, or SDL for short. In the following sections, you will see how custom directives can be implemented and used to modify the structure and behavior of a GraphQL schema in ways that would not be possible using SDL syntax alone.
Earlier versions of
graphql-tools provides a class-based mechanism for directive-based schema modification. The documentation for the class-based version is still available, but the remainder of this document describes the newer functional mechanism. We believe the newer approach is easier to reason about, but older class-based schema directives are still supported.
Most of this document is concerned with implementing schema directives, and some of the examples may seem quite complicated. No matter how many tools and best practices you have at your disposal, it can be difficult to implement a non-trivial schema directive in a reliable, reusable way. Exhaustive testing is essential, and using a typed language like TypeScript is recommended, because there are so many different schema types to worry about.
However, the API we provide for using a schema directive is extremely simple. Just import the implementation of the directive, then pass it to
makeExecutableSchema via the
schemaTransforms argument, which is an array of schema transformation functions:
That's it. The implementation of
renameDirective takes care of everything else. If you understand what the directive is supposed to do to your schema, then you do not have to worry about how it works.
Everything you read below addresses some aspect of how a directive like
@rename(to: ...) could be implemented. If that's not something you care about right now, feel free to skip the rest of this document. When you need it, it will be here.
Since the GraphQL specification does not discuss any specific implementation strategy for directives, it's up to each GraphQL server framework to expose an API for implementing new directives.
GraphQL Tools provides convenient yet powerful tools for implementing directive syntax: the
mapSchema takes two arguments: the original schema, and an object map -- pardon the pun -- of functions that can be used to transform each GraphQL object within the original schema.
mapSchema is a powerful tool, in that it creates a new copy of the original schema, transforms GraphQL objects as specified, and then rewires the entire schema such that all GraphQL objects that refer to other GraphQL objects correctly point to the new set. The
getDirectives function is straightforward; it extracts any directives (with their arguments) from the SDL originally used to create any GraphQL object.
Here is one possible implementation of the
@deprecated directive we saw above:
In order to apply this implementation to a schema that contains
@deprecated directives, simply pass the necessary typeDefs and schema transformation function to the
makeExecutableSchema function in the appropriate positions:
Alternatively, if you want to modify an existing schema object, you can use the function interface directly:
We suggest that creators of directive-based schema modification functions allow users to customize the names of the relevant directives, to help users avoid collision of directive names with existing directives within their schema or other external schema modification functions. Of course, you could hard-code the name of the directive into the function, further simplifying the above examples.
To appreciate the range of possibilities enabled by
mapSchema, let's examine a variety of practical examples.
Suppose you want to ensure a string-valued field is converted to uppercase. Though this use case is simple, it's a good example of a directive implementation that works by wrapping a field's
Notice how easy it is to handle both
@upperCase with the same
Suppose you've defined an object type that corresponds to a REST resource, and you want to avoid implementing resolver functions for every field:
There are many more issues to consider when implementing a real GraphQL wrapper over a REST endpoint (such as how to do caching or pagination), but this example demonstrates the basic structure.
Suppose your resolver returns a
Date object but you want to return a formatted string to the client:
Of course, it would be even better if the schema author did not have to decide on a specific
Date format, but could instead leave that decision to the client. To make this work, the directive just needs to add an additional argument to the field:
Now the client can specify a desired
format argument when requesting the
Query.today field, or omit the argument to use the
defaultFormat string specified in the schema:
Imagine a hypothetical
@auth directive that takes an argument
requires of type
Role, which defaults to
@auth directive can appear on an
User to set default access permissions for all
User fields, as well as appearing on individual fields, to enforce field-specific
One drawback of this approach is that it does not guarantee fields will be wrapped if they are added to the schema after
AuthDirective is applied, and the whole
getUser(context.headers.authToken) is a made-up API that would need to be fleshed out. In other words, we’ve glossed over some of the details that would be required for a production-ready implementation of this directive, though we hope the basic structure shown here inspires you to find clever solutions to the remaining problems.
Suppose you want to enforce a maximum length for a string-valued field:
Note that new types can be added to the schema with ease, but that each type must be uniquely named.
Suppose your database uses incrementing IDs for each resource type, so IDs are not unique across all resource types. Here’s how you might synthesize a field called
uid that combines the object type with various field values to produce an ID that’s unique across your schema:
SDL syntax requires declaring the names, argument types, default argument values, and permissible locations of any available directives. We have shown one approach above to doing so. If you're implementing a reusable directive for public consumption, you will probably want to either guide your users as to how properly declare their directives, or export the required SDL syntax as above so that users can pass it to
makeExecutableSchema. These techniques can be used in combination, i.e. you may with to export the directive syntax and provide instructions on how to structure any dependent types. Take a second look at the auth example above to see how this may be done and note the interplay between the directive definition and the
Directive syntax can also appear in GraphQL queries sent from the client. Query directive implementation can be performed within graphql resolver using similar techniques as the above. In general, however, schema authors should consider using field arguments wherever possible instead of query directives, with query directives most useful for annotating the query with metadata affecting the execution algorithm itself, e.g.
In theory, access to the query directives is available within the
info resolver argument by iterating through each
info.fieldNodes, although, as above, use of query directives within standard resolvers is not necessarily recommended.
makeExecutableSchema function also takes a
directiveResolvers option that can be used for implementing certain kinds of
@directives on fields that have resolver functions.
The new abstraction is more general, since it can visit any kind of schema syntax, and do much more than just wrap resolver functions. However, the old
directiveResolvers API has been left in place for backwards compatibility, though it is now implemented in terms of
Existing code that uses
directiveResolvers could consider migrating to direct usage of
mapSchema, though we have no immediate plans to deprecate
You can use schema transformation functions with code-first schemas as well. By default, if a
directives key exists within the
extensions field for a given GraphQL entity, the
getDirectives function will retrieve the directive data from the GraphQL entity's
extensions.directives data rather than from the SDL. This, of course, allows schemas created without SDL to use any schema transformation functions created for directive use, as long as they define the necessary data within the GraphQL entity extensions.
This behavior can be customized! The
getDirectives function takes a third argument,
pathToDirectivesInExtensions, an array of strings, that allows customization of this path to directive data within extensions, which is set to
['directives'] by default. We recommend allowing end users to customize this path similar to how the directive name can be customized above.
graphql-js issue for more information on directives with code-first schemas. We follow the Gatsby and graphql-compose convention of reading directives from the
extensions field, but allow customization as above.
How can you customize schema mapping? The second argument provided to mapSchema is an object of type
SchemaMapper that can specify individual mapping functions.
GraphQL objects are mapped according to the following algorithm:
- Types are mapped. The most general matching mapping function available will be used, i.e. inclusion of a
MapperKind.TYPEwill cause all types to be mapped with the specified mapper. Specifying
MapperKind.MAPPER.QUERYmappers will cause the first mapper to be used for interfaces and unions, the latter to be used for the root query object type, and all other types to be ignored.
- Enum values are mapped. If all you want to do to an enum is to change one value, it is more convenient to use a
MapperKind.ENUM_VALUEmapper than to iterate through all values on your own and recreate the type -- although that would work!
- Fields are mapped. Similar to above, if you want to modify a single field,
mapSchemacan do the iteration for you. You can subspecify
MapperKind.ROOT_FIELDto select a limited subset of fields to map.
- Arguments are mapped. Similar to above, you can subspecify
MapperKind.ARGUMENTif you want to modify only an argument.
mapSchemacan iterate through the types and fields for you.
- Directives are mapped if