Class: GraphQL::Directive

Inherits:
Object
  • Object
show all
Includes:
GraphQL::DefinitionHelpers::DefinedByConfig
Defined in:
lib/graphql/directive.rb

Overview

This implementation of `Directive` is … not robust. It seems like this area of the spec is still getting worked out, so Directive & DirectiveChain implement `@skip` and `@include` with minimal impact on query execution.

Constant Summary collapse

LOCATIONS =
[
  ON_OPERATION =  :on_operation?,
  ON_FRAGMENT =   :on_fragment?,
  ON_FIELD =      :on_field?,
]
SkipDirective =
GraphQL::Directive.define do
  name "skip"
  description "Ignore this part of the query if `if` is true"
  on([GraphQL::Directive::ON_FIELD, GraphQL::Directive::ON_FRAGMENT])

  argument :if, !GraphQL::BOOLEAN_TYPE

  resolve -> (arguments, proc) {
    if !arguments["if"]
      proc.call
    else
      nil
    end
  }
end
IncludeDirective =
GraphQL::Directive.define do
  name "include"
  description "Include this part of the query if `if` is true"
  on([GraphQL::Directive::ON_FIELD, GraphQL::Directive::ON_FRAGMENT])
  argument :if, !GraphQL::BOOLEAN_TYPE

  resolve -> (arguments, proc) {
    if arguments["if"]
      proc.call
    else
      nil
    end
  }
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GraphQL::DefinitionHelpers::DefinedByConfig

included

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments


7
8
9
# File 'lib/graphql/directive.rb', line 7

def arguments
  @arguments
end

#descriptionObject

Returns the value of attribute description


7
8
9
# File 'lib/graphql/directive.rb', line 7

def description
  @description
end

#nameObject

Returns the value of attribute name


7
8
9
# File 'lib/graphql/directive.rb', line 7

def name
  @name
end

#onObject

Returns the value of attribute on


7
8
9
# File 'lib/graphql/directive.rb', line 7

def on
  @on
end

Instance Method Details

#resolve(arguments, proc) ⇒ Object


20
21
22
# File 'lib/graphql/directive.rb', line 20

def resolve(arguments, proc)
  @resolve_proc.call(arguments, proc)
end

#resolve=(resolve_proc) ⇒ Object


24
25
26
# File 'lib/graphql/directive.rb', line 24

def resolve=(resolve_proc)
  @resolve_proc = resolve_proc
end

#to_sObject


28
29
30
# File 'lib/graphql/directive.rb', line 28

def to_s
  "<GraphQL::Directive #{name}>"
end