Directives
Configure GraphQL types, fields, and arguments
Looking for Apollo Federation directives? See Federation-specific GraphQL directives.
A directive decorates part of a GraphQL schema or operation with additional configuration. Tools like Apollo Server (and Apollo Client) can read a GraphQL document's directives and perform custom logic as appropriate.
Directives are preceded by the @
character, like so:
1type ExampleType {
2 oldField: String @deprecated(reason: "Use `newField`.")
3 newField: String
4}
This example shows the @deprecated
directive, which is a default directive (i.e., it's part of the GraphQL specification). It demonstrates the following about directives:
Directives can take arguments of their own (
reason
in this case).Directives appear after the declaration of what they decorate (the
oldField
field in this case)
Valid locations
Each directive can only appear in certain locations within a GraphQL schema or operation. These locations are listed in the directive's definition.
For example, here's the GraphQL spec's definition of the @deprecated
directive:
1directive @deprecated(
2 reason: String = "No longer supported"
3) on FIELD_DEFINITION | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION | ENUM_VALUE
This indicates that @deprecated
can decorate any of the four listed locations. Also note that its reason
argument is optional and provides a default value. Usage examples of each location are provided below:
1# ARGUMENT_DEFINITION
2# Note: @deprecated arguments _must_ be optional.
3directive @withDeprecatedArgs(
4 deprecatedArg: String @deprecated(reason: "Use `newArg`")
5 newArg: String
6) on FIELD
7
8type MyType {
9 # ARGUMENT_DEFINITION (alternate example on a field's args)
10 fieldWithDeprecatedArgs(name: String @deprecated): String
11 # FIELD_DEFINITION
12 deprecatedField: String @deprecated
13}
14
15enum MyEnum {
16 # ENUM_VALUE
17 OLD_VALUE @deprecated(reason: "Use `NEW_VALUE`.")
18 NEW_VALUE
19}
20
21input SomeInputType {
22 nonDeprecated: String
23 # INPUT_FIELD_DEFINITION
24 deprecated: String @deprecated
25}
If @deprecated
appears elsewhere in a GraphQL document, it produces an error.
If you create a custom directive, you need to define it (and its valid locations) in your schema. You don't need to define default directives like
@deprecated
.
Schema directives vs. operation directives
Usually, a given directive appears exclusively in GraphQL schemas or exclusively in GraphQL operations (rarely both, although the spec allows this).
For example, among the default directives, @deprecated
is a schema-exclusive directive and @skip
and @include
are operation-exclusive directives.
The GraphQL spec lists all possible directive locations. Schema locations are listed under TypeSystemDirectiveLocation
, and operation locations are listed under ExecutableDirectiveLocation
.
Default directives
The GraphQL specification defines the following default directives:
Directive | Description |
---|---|
@deprecated(reason: String) | Marks the schema definition of a field or enum value as deprecated with an optional reason. |
@skip(if: Boolean!) | If true , the decorated field or fragment in an operation is not resolved by the GraphQL server. |
@include(if: Boolean!) | If false , the decorated field or fragment in an operation is not resolved by the GraphQL server. |
Custom directives
⚠️ Apollo Server does not provide built-in support for custom directives that transform a schema.
Your schema can define custom directives that can then decorate other parts of your schema:
1# Definition
2directive @uppercase on FIELD_DEFINITION
3
4type Query {
5 # Usage
6 hello: String @uppercase
7}
If you want to define a custom schema directive to transform your executable schema's behavior before providing that schema to Apollo Server, we recommend using the @graphql-tools
library. See our example of using a custom directive to transform a schema.
In subgraphs
Before you use custom directives in a federated graph, make sure to consider the following:
If multiple subgraphs can resolve a particular field, each subgraph should almost always apply the exact same set of custom directives (with the exact same definition) to that field. Otherwise, the behavior of that field might vary depending on which subgraph resolves it.
Because directives are specific to individual subgraphs, it's technically valid for different subgraphs to define the same directive with different logic. As stated in the previous point, if a custom directive is used in multiple subgraphs to resolve a particular field, you should define the same directive with the same logic across subgraphs. Composition does not detect or warn about such inconsistencies.
The composition process treats executable (client-side) and type system (server-side) directives differently:
An executable directive is composed into the supergraph schema if:
All subgraphs define the directive identically
The directive is not included in any
@composeDirective
directives
Type system directives are not composed into the supergraph schema, but they can provide information to the router via the
@composeDirective
directive.
Transformer functions
As our example shows, in Apollo Server 3 and 4 you can define a transformer function for each of your subgraph schema's custom directives.
To apply transformer functions to your executable subgraph schema, you first generate the subgraph schema with buildSubgraphSchema
as usual:
1let subgraphSchema = buildSubgraphSchema({ typeDefs, resolvers });
But instead of passing the result directly to the ApolloServer
constructor, you first apply all of your transformer functions to it:
1// Transformer function for an @upper directive
2subgraphSchema = upperDirectiveTransformer(subgraphSchema, 'upper');
After applying all transformer functions, you provide your final subgraph schema to the ApolloServer
constructor as usual:
1const server = new ApolloServer({
2 schema: subgraphSchema,
3 // ...other options...
4});