Using and implementing custom directives to transform schema types, fields, and arguments
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:
directive @deprecated(reason: String = "No longer supported") on FIELD_DEFINITION | ENUM_VALUEtype ExampleType { newField: String oldField: String @deprecated(reason: "Use `newField`.")}
As you can see, the usage of @deprecated(reason: ...){:graphql}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 ...{:graphql} syntax, and then used zero or more times throughout
the schema document, using the @deprecated(reason: ...){:graphql} 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.
Using Schema Directives
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 the schema generated by makeExecutableSchema:
import { renameDirective } from 'fake-rename-directive-package'import { makeExecutableSchema } from '@graphql-tools/schema'const typeDefs = /* GraphQL */ ` type Person @rename(to: "Human") { name: String! currentDateMinusDateOfBirth: Int @rename(to: "age") }`let schema = makeExecutableSchema({ typeDefs})schema = renameDirective('rename')(schema)
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.
For mapping multiple custom schemas you can use a reduce function like so:
import { authDirective } from 'fake-auth-directive-package'import { lowerDirective } from 'fake-lower-directive-package'import { renameDirective } from 'fake-rename-directive-package'import { makeExecutableSchema } from '@graphql-tools/schema'const typeDefs = /* GraphQL */ ` type Person @rename(to: "Human") { name: String! currentDateMinusDateOfBirth: Int @rename(to: "age") email: String! @auth(requires: "member") @lower phoneNumber: String! @auth(requires: "member") }`const directiveTransformers = [ renameDirective('rename').renameDirectiveTransformer, authDirective('auth').authDirectiveTransformer, lowerDirective('lower').lowerDirectiveTransformer]let schema = makeExecutableSchema({ typeDefs })schema = directiveTransformers.reduce((curSchema, transformer) => transformer(curSchema), schema)
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.
Implementing Schema Directives
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 and
getDirective functions. 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 getDirective 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:
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:
We suggest that creators of directive-based schema modification functions allow users to customize
the names of the relevant directives, to help users avoid the 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.
Examples
To appreciate the range of possibilities enabled by mapSchema, let’s examine a variety of
practical examples.
Uppercasing Strings
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 resolve
function:
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.
Formatting date strings
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 ADMIN. This @auth directive can appear on an OBJECT like User to set default
access permissions for all User fields, as well as appearing on individual fields, to enforce
field-specific @auth restrictions:
directive @auth(requires: Role = ADMIN) on OBJECT | FIELD_DEFINITIONenum Role { ADMIN REVIEWER USER UNKNOWN}type User @auth(requires: USER) { name: String banned: Boolean @auth(requires: ADMIN) canPost: Boolean @auth(requires: REVIEWER)}
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.
Enforcing Value Restrictions
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.
Synthesizing Unique IDs
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:
If you create your application with GraphQL modules please use the schemaBuilder parameter in
createApplication() to apply the directive implementation:
import { anyDirective } from 'any-directive-package'import { makeExecutableSchema } from '@graphql-tools/schema'const application = createApplication({ ... schemaBuilder: ({ typeDefs, resolvers }) => { // Build the schema from modules let schema = makeExecutableSchema({ typeDefs, resolvers: resolvers }) // Apply the directive transformer, i.e. rename directive const patchedSchema = anyDirective('rename')(schema); return patchedSchema; }})
Declaring Schema Directives
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 wish 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 Role type.
What about Query Directives?
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.
defer, stream, etc.
In theory, access to the query directives is available within the info resolver argument by
iterating through each fieldNode of info.fieldNodes, although, as above use of query directives
within standard resolvers is not necessarily recommended.
What about directiveResolvers?
The makeExecutableSchema function is used to take a directiveResolvers option that could 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. The old directiveResolvers API can be implemented with the
above new API as follows:
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.
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’s 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.TYPE will cause all types to be mapped with the specified mapper.
Specifying MapperKind.ABSTRACT_TYPE and MapperKind.MAPPER.QUERY mappers 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_VALUE mapper 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, mapSchema can do the
iteration for you. You can subspecify MapperKind.OBJECT_FIELD or MapperKind.ROOT_FIELD to
select a limited subset of fields to map.
Arguments are mapped. Similar to above, you can subspecify MapperKind.ARGUMENT if you want to
modify only an argument. mapSchema can iterate through the types and fields for you.
Directives are mapped if MapperKind.DIRECTIVE is specified.