Docs
Launch GraphOS Studio
Apollo Server 3 is officially deprecated, with end-of-life scheduled for 22 October 2024. Learn more about upgrading to a supported Apollo Server version.

Directives

Configure GraphQL types, fields, and arguments


Looking for ? See Federation-specific GraphQL directives.

A directive decorates part of a or with additional configuration. Tools like (and Apollo Client) can read a 's and perform custom logic as appropriate.

are preceded by the @ character, like so:

schema.graphql
type ExampleType {
oldField: String @deprecated(reason: "Use `newField`.")
newField: String
}

This example shows the @deprecated , which is a default directive (i.e., it's part of the GraphQL specification). It demonstrates the following about :

  • can take of their own (reason in this case).
  • appear after the declaration of what they decorate (the oldField in this case)

Valid locations

Each can only appear in certain locations within a or . These locations are listed in the 's definition.

For example, here's the spec's definition of the @deprecated :

directive @deprecated(
reason: String = "No longer supported"
) 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 is optional and provides a default value. Usage examples of each location are provided below:

schema.graphql
# ARGUMENT_DEFINITION
# Note: @deprecated arguments _must_ be optional.
directive @withDeprecatedArgs(
deprecatedArg: String @deprecated(reason: "Use `newArg`"),
newArg: String
) on FIELD
type MyType {
# ARGUMENT_DEFINITION (alternate example on a field's args)
fieldWithDeprecatedArgs(name: String! @deprecated): String
# FIELD_DEFINITION
deprecatedField: String @deprecated
}
enum MyEnum {
# ENUM_VALUE
OLD_VALUE @deprecated(reason: "Use `NEW_VALUE`.")
NEW_VALUE
}
input SomeInputType {
nonDeprecated: String
# INPUT_FIELD_DEFINITION
deprecated: String @deprecated
}

If @deprecated appears elsewhere in a , it produces an error.

If you create a custom , 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 appears exclusively in or exclusively in (rarely both, although the spec allows this).

For example, among the default directives, @deprecated is a schema-exclusive and @skip and @include are -exclusive .

The GraphQL spec lists all possible locations. Schema locations are listed under TypeSystemDirectiveLocation, and locations are listed under ExecutableDirectiveLocation.

Default directives

The GraphQL specification defines the following default :

DirectiveDescription
@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.
Previous
Custom scalars
Next
Creating directives
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company