You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/schema/directives.md
+23-9Lines changed: 23 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
---
2
2
title: Directives
3
3
sidebar_title: Directives
4
-
description: Configure schema types, fields, and arguments
4
+
description: Configure GraphQL types, fields, and arguments
5
5
---
6
6
7
-
A **directive** decorates part of a GraphQL document with additional configuration. Tools like Apollo Server (and [Apollo Client](https://www.apollographql.com/docs/react/local-state/managing-state-with-field-policies/#querying)) can read a GraphQL document's directives and perform custom logic as appropriate.
7
+
A **directive** decorates part of a GraphQL schema or operation with additional configuration. Tools like Apollo Server (and [Apollo Client](https://www.apollographql.com/docs/react/local-state/managing-state-with-field-policies/#querying)) can read a GraphQL document's directives and perform custom logic as appropriate.
8
8
9
9
Directives are preceded by the `@` character, like so:
@@ -22,32 +22,44 @@ This example shows the `@deprecated` directive, which is a [default directive](#
22
22
23
23
## Valid locations
24
24
25
-
Each directive's definition specifies _where_ it can appear in a GraphQL document. For example, here's the GraphQL spec's definition of the `@deprecated` directive:
25
+
Eachdirectivecanonlyappearin_certain_locationswithinaGraphQLschema or operation. These locations are listed in the directive's definition.
26
+
27
+
For example, here's the GraphQL spec's definition of the `@deprecated` directive:
26
28
27
29
```graphql
28
30
directive @deprecated(
29
31
reason: String = "No longer supported"
30
32
) on FIELD_DEFINITION | ENUM_VALUE
31
33
```
32
34
33
-
Thisindicatesthat `@deprecated` candecorateeitheraschema field definition (as shown in the example above) or an enum value definition (as shown here):
35
+
This indicates that `@deprecated` can decorate either a `SCHEMA_FIELD` definition (as shown at the top of the article) or a schema `ENUM_VALUE` definition (as shown here):
34
36
35
-
```graphql
37
+
```graphql:title=schema.graphql
36
38
enum MyEnum {
37
39
OLD_VALUE @deprecated(reason: "Use `NEW_VALUE`.")
38
40
NEW_VALUE
39
41
}
40
42
```
41
43
42
-
If `@deprecated` appears elsewhere in a GraphQL schema, it produces an error.
44
+
If `@deprecated` appears elsewhere in a GraphQL document, it produces an error.
45
+
46
+
> If you [create a custom directive](#creating), you need to define it (and its valid locations) in your schema. You don't need to define [default directives](#default-directives) like `@deprecated`.
47
+
48
+
### Schema directives vs. operation directives
49
+
50
+
Usually, a given directive appears _exclusively_ in GraphQL schemas or _exclusively_ in GraphQL operations (rarely both, although the spec allows this).
51
+
52
+
For example, among the [default directives](#default-directives), `@deprecated` is a schema-exclusive directive and `@skip` and `@include` are operation-exclusive directives.
53
+
54
+
The [GraphQL spec](https://spec.graphql.org/June2018/#sec-Type-System.Directives) lists all possible directive locations. Schema locations are listed under `TypeSystemDirectiveLocation`, and operation locations are listed under `ExecutableDirectiveLocation`.
43
55
44
56
## Default directives
45
57
46
58
The [GraphQL specification](http://spec.graphql.org/June2018/#sec-Type-System.Directives) defines the following default directives:
47
59
48
60
| Directive | Description |
49
61
|-----------|-------------|
50
-
|`@deprecated(reason: String)`| Marks the definition of a field or enum value as deprecated with an optional reason. |
62
+
|`@deprecated(reason: String)`| Marks the schema definition of a field or enum value as deprecated with an optional reason. |
51
63
|`@skip(if: Boolean!)`| If `true`, the decorated field or fragment in an operation is _not_ resolved by the GraphQL server. |
52
64
|`@include(if: Boolean!)`| If `false`, the decorated field or fragment in an operation is _not_ resolved by the GraphQL server. |
53
65
@@ -63,7 +75,9 @@ You can extend Apollo Server with custom schema directives created by you or a t
63
75
64
76
To use a custom directive, pass its associated `SchemaDirectiveVisitor` subclass to Apollo Server via the `schemaDirectives` argument. This object maps the name of a directive (e.g., `upper`) to the class that implements its behavior (e.g., `UpperCaseDirective`).
65
77
66
-
```js{40-42}
78
+
Also make sure the directive is defined in your schema with all valid locations listed.
0 commit comments