Class: GraphQL::Directive

Inherits:
Object
  • Object
show all
Extended by:
GraphQL::DefinitionHelpers::Definable
Defined in:
lib/graph_ql/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.new do |d, type, field, arg|
  d.name "skip"
  d.description "Ignore this part of the query if `if` is true"
  d.on([GraphQL::Directive::ON_FIELD, GraphQL::Directive::ON_FRAGMENT])
  d.arguments({
    if: arg.build({type: !GraphQL::BOOLEAN_TYPE})
  })
  d.resolve -> (arguments, proc) {
    if !arguments["if"]
      proc.call
    else
      nil
    end
  }
end
IncludeDirective =
GraphQL::Directive.new do |d, type, field, arg|
  d.name "include"
  d.description "Include this part of the query if `if` is true"
  d.on([GraphQL::Directive::ON_FIELD, GraphQL::Directive::ON_FRAGMENT])
  d.arguments({
    if: arg.build({type: !GraphQL::BOOLEAN_TYPE})
  })
  d.resolve -> (arguments, proc) {
    if arguments["if"]
      proc.call
    else
      nil
    end
  }
end

Instance Method Summary collapse

Methods included from GraphQL::DefinitionHelpers::Definable

attr_definable

Constructor Details

#initialize {|_self, GraphQL::DefinitionHelpers::TypeDefiner.instance, GraphQL::DefinitionHelpers::FieldDefiner.instance, GraphQL::DefinitionHelpers::ArgumentDefiner.instance| ... } ⇒ Directive

Returns a new instance of Directive.

Yields:

Yield Parameters:



19
20
21
22
23
24
25
26
27
28
# File 'lib/graph_ql/directive.rb', line 19

def initialize
  @arguments = {}
  @on = []
  yield(
    self,
    GraphQL::DefinitionHelpers::TypeDefiner.instance,
    GraphQL::DefinitionHelpers::FieldDefiner.instance,
    GraphQL::DefinitionHelpers::ArgumentDefiner.instance
  )
end

Instance Method Details

#arguments(new_arguments = nil) ⇒ Object



39
40
41
42
43
44
# File 'lib/graph_ql/directive.rb', line 39

def arguments(new_arguments=nil)
  if !new_arguments.nil?
    @arguments = GraphQL::DefinitionHelpers::StringNamedHash.new(new_arguments).to_h
  end
  @arguments
end

#resolve(proc_or_arguments, proc = nil) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/graph_ql/directive.rb', line 30

def resolve(proc_or_arguments, proc=nil)
  if proc.nil?
    # resolve is being defined, just set it
    @resolve_proc = proc_or_arguments
  else
    @resolve_proc.call(proc_or_arguments, proc)
  end
end

#to_sObject



46
47
48
# File 'lib/graph_ql/directive.rb', line 46

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