Sha256: 49534496fdbb20396d8797d7cbcdf77eeb6137170c7751ed8e9e4119079dafae

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

module Giraph
  # Wrapped methods on GraphQL::Field class
  module Extensions
    module Field
      # Wrap the 'resolve' method on Field so that we can
      # intercept the registered resolution Proc and resolve
      # on already-resolved values from the JSON returned
      # to the sub-query through the remote endpoint
      def resolve(object, arguments, context)
        # Giraph always parses a remote response with a special Hash
        # class called 'Giraph::Remote::Response', which is a no-op sub-class
        # of Hash. This way we can easily recognize this special
        # "resolve on already resolved response" case while still
        # allowing regular Hash to be resolved normally.
        if object.instance_of? Giraph::Remote::Response
          # If the field was aliased, response will be keyed by the alias
          field = context.ast_node.alias || context.ast_node.name
          object[field.to_sym]
        else
          # Not Giraph, let it through...
          super
        end
      end
    end
  end
end

# Monkey-patch GraphQL Field class to wrap the default 'resolve'
module GraphQL
  class Field
    prepend Giraph::Extensions::Field
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
giraph-0.1.1 lib/giraph/extensions/field.rb
giraph-0.1.0 lib/giraph/extensions/field.rb