Sha256: b7b09879a269be483f75aaed45d08abefa969c325e99d97d89f39da36b7d60d7

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

class GraphQL::Field
  extend GraphQL::Definable
  attr_definable(:arguments, :deprecation_reason, :name, :description, :type)

  def initialize
    @arguments = {}
    @resolve_proc = -> (o, a, c) { GraphQL::Query::DEFAULT_RESOLVE }
    yield(self, GraphQL::TypeDefiner.instance, GraphQL::FieldDefiner.instance, GraphQL::ArgumentDefiner.instance)
  end

  def arguments(new_arguments=nil)
    if !new_arguments.nil?
      self.arguments=(new_arguments)
    end
    @arguments
  end

  def arguments=(new_arguments)
    stringified_arguments = new_arguments
      .reduce({}) { |memo, (key, value)| memo[key.to_s] = value; memo }
    # Set the name from its context on this type:
    stringified_arguments.each {|k, v| v.respond_to?("name=") && v.name = k }
    @arguments = stringified_arguments
  end


  # Used when defining:
  #   resolve -> (obj, args, ctx) { obj.get_value }
  # Also used when executing queries:
  #   field.resolve(obj, args, ctx)
  def resolve(proc_or_object, arguments=nil, ctx=nil)
    if arguments.nil? && ctx.nil?
      @resolve_proc = proc_or_object
    else
      @resolve_proc.call(proc_or_object, arguments, ctx)
    end
  end

  # You can pass a proc which will cause the type to be lazy-evaled,
  # That's nice if you have load-order issues
  def type(type_or_proc=nil)
    if !type_or_proc.nil?
      @type = type_or_proc
    elsif @type.is_a?(Proc)
      # lazy-eval it
      @type = @type.call
    end
    @type
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphql-0.2.0 lib/graph_ql/field.rb