Sha256: a0091d52aabfd6a37c59331938aa6cd204e58bd22bbdd1cf86d4c79c53ed9060

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

module Giraph
  module Remote
    # Field resolver to plug a remote GraphQL query root into a local type
    class Query
      def self.bind(endpoint, &block)
        new(
          endpoint,
          Remote::Connector.new(endpoint),
          &block
        )
      end

      def initialize(endpoint, connector, &block)
        @endpoint = endpoint
        @evaluator = block || method(:default_evaluator)
        @connector = connector
      end

      # Reconstructs a valid GraphQL root-query from the current
      # field in question, including all variables and params,
      # hands over to connector to execute remotely.
      def call(obj, args, ctx)
        # Given an evaluator block, continue if only it evaluates to non-nil!
        return unless (remote_root = @evaluator.call(obj, args, ctx))

        subquery = Subquery.new(ctx)

        # Continue with remote query execution
        connector.resolve(
          ctx,
          query_string(subquery),
          query_variables(subquery, remote_root)
        )
      end

      private

      def query_type
        'query'
      end

      def query_string(subquery)
        # Full GraphQL query for remote
        "#{query_type} #{subquery.subquery_string}"
      end

      def query_variables(subquery, remote_root)
        # Variable hash to send along
        subquery.variable_string do |dict|
          dict.merge(__giraph_root__: remote_root)
        end
      end

      def default_evaluator(*args)
        {}
      end

      attr_reader :endpoint, :connector
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
giraph-0.1.1 lib/giraph/remote/query.rb
giraph-0.1.0 lib/giraph/remote/query.rb