Class: GraphQL::Field

Inherits:
Object
  • Object
show all
Extended by:
Definable
Defined in:
lib/graph_ql/field.rb

Instance Method Summary collapse

Methods included from Definable

attr_definable

Constructor Details

#initialize {|_self, GraphQL::TypeDefiner.instance, GraphQL::FieldDefiner.instance, GraphQL::ArgumentDefiner.instance| ... } ⇒ Field

Returns a new instance of Field.

Yields:

Yield Parameters:



5
6
7
8
9
# File 'lib/graph_ql/field.rb', line 5

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

Instance Method Details

#arguments(new_arguments = nil) ⇒ Object



11
12
13
14
15
16
# File 'lib/graph_ql/field.rb', line 11

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

#arguments=(new_arguments) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/graph_ql/field.rb', line 18

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

#resolve(proc_or_object, arguments = nil, ctx = nil) ⇒ Object

Used when defining:

resolve -> (obj, args, ctx) { obj.get_value }

Also used when executing queries:

field.resolve(obj, args, ctx)


31
32
33
34
35
36
37
# File 'lib/graph_ql/field.rb', line 31

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

#type(type_or_proc = nil) ⇒ Object

You can pass a proc which will cause the type to be lazy-evaled, That’s nice if you have load-order issues



41
42
43
44
45
46
47
48
49
# File 'lib/graph_ql/field.rb', line 41

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