|
| 1 | +--- |
| 2 | +layout: guide |
| 3 | +doc_stub: false |
| 4 | +search: true |
| 5 | +section: Type Definitions |
| 6 | +title: Directives |
| 7 | +desc: Special instructions for the GraphQL runtime |
| 8 | +index: 10 |
| 9 | +class_based_api: true |
| 10 | +experimental: true |
| 11 | +--- |
| 12 | + |
| 13 | + |
| 14 | +Directives are system-defined keywords that modify execution. All GraphQL systems have at least _two_ directives, `@skip` and `@include`. For example: |
| 15 | + |
| 16 | +```ruby |
| 17 | +query ProfileView($renderingDetailedProfile: Boolean!){ |
| 18 | + viewer { |
| 19 | + handle |
| 20 | + # These fields will be included only if the check passes: |
| 21 | + ... @include(if: $renderingDetailedProfile) { |
| 22 | + location |
| 23 | + homepageUrl |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +Here's how the two built-in directives work: |
| 30 | + |
| 31 | +- `@skip(if: ...)` skips the selection if the `if: ...` value is truthy ({{ "GraphQL::Schema::Directive::Skip" | api_doc }}) |
| 32 | +- `@include(if: ...)` includes the selection if the `if: ...` value is truthy ({{ "GraphQL::Schema::Directive::Include" | api_doc }}) |
| 33 | + |
| 34 | +GraphQL-Ruby also supports custom directives for use with the {% internal_link "interpreter runtime", "/queries/interpreter" %}. |
| 35 | + |
| 36 | +## Custom Directives |
| 37 | + |
| 38 | +Custom directives extend {{ "GraphQL::Schema::Directive" | api_doc }}: |
| 39 | + |
| 40 | +```ruby |
| 41 | +# app/graphql/directives/my_directive.rb |
| 42 | +class Directives::MyDirective < GraphQL::Schema::Directive |
| 43 | + description "A nice runtime customization" |
| 44 | +end |
| 45 | +``` |
| 46 | + |
| 47 | +Then, they're hooked up to the schema using `directive(...)`: |
| 48 | + |
| 49 | +```ruby |
| 50 | +class MySchema < GraphQL::Schema |
| 51 | + # Custom directives are only supported by the Interpreter runtime |
| 52 | + use GraphQL::Execution::Interpreter |
| 53 | + # Attach the custom directive to the schema |
| 54 | + directive(Directives::MyDirective) |
| 55 | +end |
| 56 | +``` |
| 57 | + |
| 58 | +{{ "GraphQL::Schema::Directive::Feature" | api_doc }} and {{ "GraphQL::Schema::Directive::Transform" | api_doc }} are included in the library as examples. |
| 59 | + |
| 60 | +### Arguments |
| 61 | + |
| 62 | +Like fields, directives may have {% internal_link "arguments", "/fields/arguments" %} : |
| 63 | + |
| 64 | +```ruby |
| 65 | +argument :if, Boolean, required: true, |
| 66 | + description: "Skips the selection if this condition is true" |
| 67 | +``` |
| 68 | + |
| 69 | +### Runtime hooks |
| 70 | + |
| 71 | +Directive classes may implement the following class methods to interact with the runtime: |
| 72 | + |
| 73 | +- `def self.include?(obj, args, ctx)`: If this hook returns `false`, the nodes flagged by this directive will be skipped at runtime. |
| 74 | +- `def self.resolve(obj, args, ctx)`: Wraps the resolution of flagged nodes. Resolution is passed as a __block__, so `yield` will continue resolution. The return value of this method will be treated as the return value of the flagged field. |
| 75 | + |
| 76 | +Looking for a runtime hook that isn't listed here? {% open_an_issue "New directive hook: @something", "<!-- Describe how the directive would be used and then how you might implement it --> " %} to start the conversation! |
| 77 | + |
| 78 | +### Directive object lifecycle |
| 79 | + |
| 80 | +Currently, Directive classes are never initialized. A later version of GraphQL may initialize these objects for runtime application. |
0 commit comments