Skip to content

Commit 0238408

Browse files
Stephen BarlowStephen Barlow
authored andcommitted
Incorporate feedback from glasser
1 parent c580bd8 commit 0238408

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

docs/source/schema/directives.md

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
22
title: Directives
33
sidebar_title: Directives
4-
description: Configure schema types, fields, and arguments
4+
description: Configure GraphQL types, fields, and arguments
55
---
66

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.
88

99
Directives are preceded by the `@` character, like so:
1010

11-
```graphql{2}
11+
```graphql{2}:title=schema.graphql
1212
type ExampleType {
1313
oldField: String @deprecated(reason: "Use `newField`.")
1414
newField: String
@@ -22,32 +22,44 @@ This example shows the `@deprecated` directive, which is a [default directive](#
2222

2323
## Valid locations
2424

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+
Each directive can only appear in _certain_ locations within a GraphQL schema 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:
2628

2729
```graphql
2830
directive @deprecated(
2931
reason: String = "No longer supported"
3032
) on FIELD_DEFINITION | ENUM_VALUE
3133
```
3234

33-
This indicates that `@deprecated` can decorate either a schema 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):
3436

35-
```graphql
37+
```graphql:title=schema.graphql
3638
enum MyEnum {
3739
OLD_VALUE @deprecated(reason: "Use `NEW_VALUE`.")
3840
NEW_VALUE
3941
}
4042
```
4143

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`.
4355

4456
## Default directives
4557

4658
The [GraphQL specification](http://spec.graphql.org/June2018/#sec-Type-System.Directives) defines the following default directives:
4759

4860
| Directive | Description |
4961
|-----------|-------------|
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. |
5163
| `@skip(if: Boolean!)` | If `true`, the decorated field or fragment in an operation is _not_ resolved by the GraphQL server. |
5264
| `@include(if: Boolean!)` | If `false`, the decorated field or fragment in an operation is _not_ resolved by the GraphQL server. |
5365

@@ -63,7 +75,9 @@ You can extend Apollo Server with custom schema directives created by you or a t
6375

6476
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`).
6577

66-
```js{40-42}
78+
Also make sure the directive is defined in your schema with all valid locations listed.
79+
80+
```js{20,40-42}
6781
const { ApolloServer, gql, SchemaDirectiveVisitor } = require('apollo-server');
6882
const { defaultFieldResolver } = require('graphql');
6983

0 commit comments

Comments
 (0)