Sha256: da9e68f344132709addf645b4b14aad02726301cf837b268bf29e7ca75e5fec9

Contents?: true

Size: 1.73 KB

Versions: 14

Compression:

Stored size: 1.73 KB

Contents

# TODO: directives on fragments: http://facebook.github.io/graphql/#sec-Fragment-Directives
class GraphQL::Query::DirectiveChain
  DIRECTIVE_ON = {
    GraphQL::Language::Nodes::Field =>          GraphQL::Directive::ON_FIELD,
    GraphQL::Language::Nodes::InlineFragment => GraphQL::Directive::ON_FRAGMENT,
    GraphQL::Language::Nodes::FragmentSpread => GraphQL::Directive::ON_FRAGMENT,
  }

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

  attr_reader :result
  def initialize(ast_node, query, &block)
    directives = query.schema.directives
    on_what = DIRECTIVE_ON[ast_node.class]
    ast_directives = GET_DIRECTIVES[ast_node.class].call(ast_node, query.fragments)

    if contains_skip?(ast_directives)
      ast_directives = ast_directives.reject { |ast_directive| ast_directive.name == 'include' }
    end

    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::LiteralInput.from_arguments(ast_directive.arguments, directive.arguments, query.variables)
        @result = directive.resolve(args, block)
      end
      @result ||= {}
    end
  end

  private
  def contains_skip?(directives)
    directives.any? { |directive| directive.name == 'skip' }
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
graphql-0.12.1 lib/graphql/query/directive_chain.rb
graphql-0.12.0 lib/graphql/query/directive_chain.rb
graphql-0.11.1 lib/graphql/query/directive_chain.rb
graphql-0.11.0 lib/graphql/query/directive_chain.rb
graphql-0.10.9 lib/graphql/query/directive_chain.rb
graphql-0.10.8 lib/graphql/query/directive_chain.rb
graphql-0.10.7 lib/graphql/query/directive_chain.rb
graphql-0.10.6 lib/graphql/query/directive_chain.rb
graphql-0.10.5 lib/graphql/query/directive_chain.rb
graphql-0.10.4 lib/graphql/query/directive_chain.rb
graphql-0.10.3 lib/graphql/query/directive_chain.rb
graphql-0.10.2 lib/graphql/query/directive_chain.rb
graphql-0.10.1 lib/graphql/query/directive_chain.rb
graphql-0.10.0 lib/graphql/query/directive_chain.rb