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
# File 'lib/graph_ql/field.rb', line 18

def arguments=(new_arguments)
  @arguments = GraphQL::StringNamedHash.new(new_arguments).to_h
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)


27
28
29
30
31
32
33
# File 'lib/graph_ql/field.rb', line 27

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



37
38
39
40
41
42
43
44
45
# File 'lib/graph_ql/field.rb', line 37

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