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: documentation/directives.md
+138Lines changed: 138 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,16 @@ title: "SDL directives"
3
3
date: 2018-09-09T12:52:46+10:00
4
4
description: How SDL Directives can be used to adjust the behavior of your graphql API
5
5
---
6
+
# Directives
7
+
8
+
[Directives](https://spec.graphql.org/draft/#sec-Language.Directives) are a powerful feature of GraphQL, allowing you to declare additional data to a schema or document. This data can then be used to change runtime execution or type validation behavior.
9
+
10
+
There are two broad categories of directives, schema and operation directives. Schema directives are used on schema elements, and operation directives are used in operations within a GraphQL document.
11
+
12
+
Often, operation directives are also called "query directives", although they can be used in any GraphQL operation. Whilst both names can be used interchangeably, note that GraphQL Java class names use "query directives".
13
+
14
+
Note for those who love the details: the terminology of directives is a bit confusing. It is technically possible to define a directive that is both a schema and operation directive, in other words, defined for both schema and operation locations. However in practice, this is not common.
All custom schema and operation directives don't have any effect until we implement new custom behavior. For example, the operation above where `lastTimeOutside` has a `@cache` directive behaves exactly the same as without it, until we have implemented some new logic. We'll demonstrate implementation of behavior for directives in the next section of this page.
359
+
360
+
Here is an operation directive with all possible eight locations in a GraphQL document, which contains operations.
361
+
362
+
```graphql
363
+
type@fooonQUERY | MUTATION | SUBSCRIPTION |
364
+
FIELD | FRAGMENT_DEFINITION | FRAGMENT_SPREAD |
365
+
INLINE_FRAGMENT | VARIABLE_DEFINITION
366
+
367
+
querysomeQuery(
368
+
$var: String@foo # Variable definition
369
+
) @foo # Query
370
+
{
371
+
field@foo # Field
372
+
...onQuery@foo { # Inline fragment
373
+
field
374
+
}
375
+
...someFragment@foo # Fragment spread
376
+
}
377
+
378
+
fragmentsomeFragment@foo { # Fragment
379
+
field
380
+
}
381
+
382
+
mutationsomeMutation@foo { # Mutation
383
+
field
384
+
}
385
+
386
+
subscriptionsomeSubscription@foo { # Subscription
387
+
field
388
+
}
389
+
```
390
+
391
+
Although it is technically possible to define a directive that includes locations associated with schema and operation directives, in practice this is not common.
392
+
393
+
## Implementing logic for operation directives
394
+
395
+
Let's implement the logic for a `@cache` operation directive:
In GraphQL Java, operation directive definitions are represented as `GraphQLDirective`s. Operation directive usages are represented as `QueryAppliedDirective`s. Note that the word "query" here is misleading, as it actually refers to a directive that applies to any of the three GraphQL operations: queries, mutations, or subscriptions. Operation directives are still commonly referred to as "query" directives, hence the class name.
412
+
413
+
Usages of operation directives are represented in GraphQL Java as instances of `QueryAppliedDirective`, and provided argument values are represented as `QueryAppliedDirectiveArgument`.
414
+
415
+
We can access operation directives usage during execution via `getQueryDirectives()` in `DataFetchingEnvironment`. For example:
Copy file name to clipboardExpand all lines: versioned_docs/version-v22/directives.md
+138Lines changed: 138 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,16 @@ title: "SDL directives"
3
3
date: 2018-09-09T12:52:46+10:00
4
4
description: How SDL Directives can be used to adjust the behavior of your graphql API
5
5
---
6
+
# Directives
7
+
8
+
[Directives](https://spec.graphql.org/draft/#sec-Language.Directives) are a powerful feature of GraphQL, allowing you to declare additional data to a schema or document. This data can then be used to change runtime execution or type validation behavior.
9
+
10
+
There are two broad categories of directives, schema and operation directives. Schema directives are used on schema elements, and operation directives are used in operations within a GraphQL document.
11
+
12
+
Often, operation directives are also called "query directives", although they can be used in any GraphQL operation. Whilst both names can be used interchangeably, note that GraphQL Java class names use "query directives".
13
+
14
+
Note for those who love the details: the terminology of directives is a bit confusing. It is technically possible to define a directive that is both a schema and operation directive, in other words, defined for both schema and operation locations. However in practice, this is not common.
All custom schema and operation directives don't have any effect until we implement new custom behavior. For example, the operation above where `lastTimeOutside` has a `@cache` directive behaves exactly the same as without it, until we have implemented some new logic. We'll demonstrate implementation of behavior for directives in the next section of this page.
359
+
360
+
Here is an operation directive with all possible eight locations in a GraphQL document, which contains operations.
361
+
362
+
```graphql
363
+
type@fooonQUERY | MUTATION | SUBSCRIPTION |
364
+
FIELD | FRAGMENT_DEFINITION | FRAGMENT_SPREAD |
365
+
INLINE_FRAGMENT | VARIABLE_DEFINITION
366
+
367
+
querysomeQuery(
368
+
$var: String@foo # Variable definition
369
+
) @foo # Query
370
+
{
371
+
field@foo # Field
372
+
...onQuery@foo { # Inline fragment
373
+
field
374
+
}
375
+
...someFragment@foo # Fragment spread
376
+
}
377
+
378
+
fragmentsomeFragment@foo { # Fragment
379
+
field
380
+
}
381
+
382
+
mutationsomeMutation@foo { # Mutation
383
+
field
384
+
}
385
+
386
+
subscriptionsomeSubscription@foo { # Subscription
387
+
field
388
+
}
389
+
```
390
+
391
+
Although it is technically possible to define a directive that includes locations associated with schema and operation directives, in practice this is not common.
392
+
393
+
## Implementing logic for operation directives
394
+
395
+
Let's implement the logic for a `@cache` operation directive:
In GraphQL Java, operation directive definitions are represented as `GraphQLDirective`s. Operation directive usages are represented as `QueryAppliedDirective`s. Note that the word "query" here is misleading, as it actually refers to a directive that applies to any of the three GraphQL operations: queries, mutations, or subscriptions. Operation directives are still commonly referred to as "query" directives, hence the class name.
412
+
413
+
Usages of operation directives are represented in GraphQL Java as instances of `QueryAppliedDirective`, and provided argument values are represented as `QueryAppliedDirectiveArgument`.
414
+
415
+
We can access operation directives usage during execution via `getQueryDirectives()` in `DataFetchingEnvironment`. For example:
0 commit comments