Skip to content

Commit fa91dea

Browse files
committed
Merge pull request #32 from facebook/directives
Directives improvements
2 parents b73c268 + 9e68777 commit fa91dea

6 files changed

+162
-123
lines changed

Section 2 -- Language.md

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ query getZuck {
8686
}
8787
```
8888

89-
## Field Arguments
89+
## Arguments
9090

91-
Fields may take arguments. These often map directly to function arguments
92-
within the GraphQL server implementation. We already saw arguments used
93-
in the global field above.
91+
Fields and directives may take arguments.
92+
93+
These often map directly to function arguments within the GraphQL server
94+
implementation. We already saw arguments used in the global field above.
9495

9596
In this example, we want to query a user's profile picture of a
9697
specific size:
@@ -119,7 +120,7 @@ Many arguments can exist for a given field:
119120

120121
**Arguments are unordered**
121122

122-
Field arguments may be provided in any syntactic order and maintain identical
123+
Arguments may be provided in any syntactic order and maintain identical
123124
semantic meaning.
124125

125126
These two queries are semantically identical:
@@ -195,7 +196,7 @@ the field's name otherwise.
195196

196197
## Input Values
197198

198-
Both field arguments and directives accept input values. Input values can be
199+
Field and directive arguments accept input values. Input values can be
199200
specified as a variable or represented inline as literals. Input values can
200201
be scalars, enumerations, or input objects. List and inputs objects may also
201202
contain variables.
@@ -270,26 +271,6 @@ could run this query and request profilePic of size 60 with:
270271
}
271272
```
272273

273-
## Directives
274-
275-
In some cases, you need to provide options to alter GraphQL's execution
276-
behavior in ways field arguments will not suffice, such as conditionally
277-
skipping a field. Directives provide this with a `@name` and can be
278-
specified to be used without an argument or with a value argument.
279-
280-
Directives can be used to conditionally include fields in a query based
281-
on a provided boolean value. In this contrived example experimentalField
282-
will be queried and controlField will not.
283-
284-
```graphql
285-
query myQuery($someTest: Boolean) {
286-
experimentalField @if: $someTest,
287-
controlField @unless: $someTest
288-
}
289-
```
290-
291-
As future versions of GraphQL adopts new configurable execution capabilities,
292-
they may be exposed via directives.
293274

294275
## Fragments
295276

@@ -446,3 +427,59 @@ query InlineFragmentTyping {
446427
}
447428
```
448429

430+
431+
## Directives
432+
433+
In some cases, you need to provide options to alter GraphQL's execution
434+
behavior in ways field arguments will not suffice, such as conditionally
435+
including or skipping a field. Directives provide this by describing additional information to the executor.
436+
437+
Directives have a name along with a list of arguments which may accept values
438+
of any input type.
439+
440+
Directives can be used to describe additional information for fields, fragments,
441+
and operations.
442+
443+
As future versions of GraphQL adopts new configurable execution capabilities,
444+
they may be exposed via directives.
445+
446+
### Fragment Directives
447+
448+
Fragments may include directives to alter their behavior. At runtime, the directives provided on a fragment spread override those described on the
449+
definition.
450+
451+
For example, the following query:
452+
453+
```graphql
454+
query HasConditionalFragment($condition: Boolean) {
455+
...MaybeFragment @include(if: $condition)
456+
}
457+
458+
fragment MaybeFragment on Query {
459+
me {
460+
name
461+
}
462+
}
463+
```
464+
465+
Will have identical runtime behavior as
466+
467+
```graphql
468+
query HasConditionalFragment($condition: Boolean) {
469+
...MaybeFragment
470+
}
471+
472+
fragment MaybeFragment on Query @include(if: $condition) {
473+
me {
474+
name
475+
}
476+
}
477+
```
478+
479+
FragmentSpreadDirectives(fragmentSpread) :
480+
* Let {directives} be the set of directives on {fragmentSpread}
481+
* Let {fragmentDefinition} be the FragmentDefinition in the document named {fragmentSpread} refers to.
482+
* For each {directive} in directives on {fragmentDefinition}
483+
* If {directives} does not contain a directive named {directive}.
484+
* Add {directive} into {directives}
485+
* Return {directives}

Section 3 -- Type System.md

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Scalar type.
100100

101101
**Input Coercion**
102102

103-
If a GraphQL server expects a scalar type as input to a field argument, coercion
103+
If a GraphQL server expects a scalar type as input to an argument, coercion
104104
is observable and the rules must be well defined. If an input value does not
105105
match a coercion rule, a query error must be raised.
106106

@@ -354,9 +354,9 @@ Objects are never valid inputs.
354354
#### Object Field Arguments
355355

356356
Object fields are conceptually functions which yield values. Occasionally object
357-
fields can accept arguments to further specify the return value. Field arguments
358-
are defined as a list of all possible argument names and their expected input
359-
types.
357+
fields can accept arguments to further specify the return value. Object field
358+
arguments are defined as a list of all possible argument names and their
359+
expected input types.
360360

361361
For example, a `Person` type with a `picture` field could accept an argument to
362362
determine what size of an image to return.
@@ -389,9 +389,7 @@ May yield the result:
389389
}
390390
```
391391

392-
The type of a field argument can be a Scalar, as it was in this example, or an
393-
Enum. It can also be an Input Object, covered later in this document, or it can
394-
be any wrapping type whose underlying base type is one of those three.
392+
The type of an object field argument can be any Input type.
395393

396394
#### Object Field deprecation
397395

@@ -739,14 +737,42 @@ value.
739737

740738
## Directives
741739

742-
A GraphQL schema includes a list of supported directives, each of which has
743-
a name. Directives can apply to operations, fragments, or fields; each directive
744-
indicates which of those it applies to.
740+
A GraphQL schema includes a list of the directives the execution
741+
engine supports.
742+
743+
GraphQL implementations should provide the `@skip` and `@include` directives.
744+
745+
### @skip
746+
747+
The `@skip` directive may be provided for fields or fragments, and allows
748+
for conditional exclusion during execution as described by the if argument.
749+
750+
In this example `experimentalField` will be queried only if the `$someTest` is
751+
provided a `false` value.
752+
753+
```graphql
754+
query myQuery($someTest: Boolean) {
755+
experimentalField @skip(if: $someTest)
756+
}
757+
```
758+
759+
### @include
760+
761+
The `@include` directive may be provided for fields or fragments, and allows
762+
for conditional inclusion during execution as described by the if argument.
763+
764+
In this example `experimentalField` will be queried only if the `$someTest` is
765+
provided a `true` value.
766+
767+
```graphql
768+
query myQuery($someTest: Boolean) {
769+
experimentalField @include(if: $someTest)
770+
}
771+
```
772+
773+
The `@skip` directive has precidence over the `@include` directive should both
774+
be provided in the same context.
745775

746-
Directives can optionally take an argument. The type of the argument to
747-
a directive has the same restrictions as the type of an argument to a field. It
748-
can be a Scalar, an Enum, an Input Object, or any wrapping type whose underlying
749-
base type is one of those three.
750776

751777
## Starting types
752778

Section 4 -- Introspection.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ enum __TypeKind {
170170
type __Directive {
171171
name: String!
172172
description: String
173-
type: __Type
173+
args: [__InputValue!]!
174174
onOperation: Boolean!
175175
onFragment: Boolean!
176176
onField: Boolean!

0 commit comments

Comments
 (0)