Class: GraphQL::Directive
- Inherits:
-
Object
- Object
- GraphQL::Directive
- Includes:
- GraphQL::Define::InstanceDefinable
- Defined in:
- lib/graphql/directive.rb
Overview
Directives are server-defined hooks for modifying execution.
Two directives are included out-of-the-box:
-
‘@skip(if: …)` Skips the tagged field if the value of `if` is true
-
‘@include(if: …)` Includes the tagged field only if `if` is true
Constant Summary collapse
- LOCATIONS =
[ QUERY = :QUERY, MUTATION = :MUTATION, SUBSCRIPTION = :SUBSCRIPTION, FIELD = :FIELD, FRAGMENT_DEFINITION = :FRAGMENT_DEFINITION, FRAGMENT_SPREAD = :FRAGMENT_SPREAD, INLINE_FRAGMENT = :INLINE_FRAGMENT, ]
- SkipDirective =
GraphQL::Directive.define do name "skip" description "Ignore this part of the query if `if` is true" locations([GraphQL::Directive::FIELD, GraphQL::Directive::FRAGMENT_SPREAD, GraphQL::Directive::INLINE_FRAGMENT]) argument :if, !GraphQL::BOOLEAN_TYPE include_proc -> (arguments) { !arguments["if"] } end
- IncludeDirective =
GraphQL::Directive.define do name "include" description "Include this part of the query if `if` is true" locations([GraphQL::Directive::FIELD, GraphQL::Directive::FRAGMENT_SPREAD, GraphQL::Directive::INLINE_FRAGMENT]) argument :if, !GraphQL::BOOLEAN_TYPE include_proc -> (arguments) { arguments["if"] } end
Instance Method Summary collapse
- #include?(arguments) ⇒ Boolean
-
#initialize ⇒ Directive
constructor
A new instance of Directive.
- #on_field? ⇒ Boolean
- #on_fragment? ⇒ Boolean
- #on_operation? ⇒ Boolean
- #to_s ⇒ Object
Methods included from GraphQL::Define::InstanceDefinable
#definition_proc=, included, #metadata
Constructor Details
#initialize ⇒ Directive
Returns a new instance of Directive.
24 25 26 |
# File 'lib/graphql/directive.rb', line 24 def initialize @arguments = {} end |
Instance Method Details
#include?(arguments) ⇒ Boolean
28 29 30 |
# File 'lib/graphql/directive.rb', line 28 def include?(arguments) include_proc.call(arguments) end |
#on_field? ⇒ Boolean
36 37 38 |
# File 'lib/graphql/directive.rb', line 36 def on_field? locations.include?(FIELD) end |
#on_fragment? ⇒ Boolean
40 41 42 |
# File 'lib/graphql/directive.rb', line 40 def on_fragment? locations.include?(FRAGMENT_SPREAD) && locations.include?(INLINE_FRAGMENT) end |
#on_operation? ⇒ Boolean
44 45 46 |
# File 'lib/graphql/directive.rb', line 44 def on_operation? locations.include?(QUERY) && locations.include?(MUTATION) && locations.include?(SUBSCRIPTION) end |
#to_s ⇒ Object
32 33 34 |
# File 'lib/graphql/directive.rb', line 32 def to_s "<GraphQL::Directive #{name}>" end |