Class: GraphQL::DirectiveChain

Inherits:
Object
  • Object
show all
Defined in:
lib/graph_ql/directives/directive_chain.rb

Constant Summary collapse

DIRECTIVE_ON =
{
  GraphQL::Nodes::Field =>          GraphQL::Directive::ON_FIELD,
  GraphQL::Nodes::InlineFragment => GraphQL::Directive::ON_FRAGMENT,
  GraphQL::Nodes::FragmentSpread => GraphQL::Directive::ON_FRAGMENT,
}
GET_DIRECTIVES =
{
  GraphQL::Nodes::Field =>          Proc.new { |n, f| n.directives },
  GraphQL::Nodes::InlineFragment => Proc.new { |n, f| n.directives },
  GraphQL::Nodes::FragmentSpread => Proc.new { |n, f| n.directives + f[n.name].directives }, # get directives from definition too
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ast_node, operation_resolver, &block) ⇒ DirectiveChain

Returns a new instance of DirectiveChain.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/graph_ql/directives/directive_chain.rb', line 15

def initialize(ast_node, operation_resolver, &block)
  directives = operation_resolver.query.schema.directives
  on_what = DIRECTIVE_ON[ast_node.class]
  ast_directives = GET_DIRECTIVES[ast_node.class].call(ast_node, operation_resolver.query.fragments)
  applicable_directives = ast_directives
    .map { |ast_directive| [ast_directive, directives[ast_directive.name]] }
    .select { |directive_pair| directive_pair.last.on.include?(on_what) }

  if applicable_directives.none?
    @result = block.call
  else
    applicable_directives.map do |(ast_directive, directive)|
      args = GraphQL::Query::Arguments.new(ast_directive.arguments, operation_resolver.variables).to_h
      @result = directive.resolve(args, block)
    end
    @result ||= {}
  end
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



14
15
16
# File 'lib/graph_ql/directives/directive_chain.rb', line 14

def result
  @result
end