Class: GraphQL::Field

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

Overview

These are valid values for a type’s ‘fields` hash.

You can also use FieldDefiner#build to create fields.

Examples:

creating a field

name_field = GraphQL::Field.new do |f, types|
  f.name("Name")
  f.type(!types.String)
  f.description("The name of this thing")
  f.resolve -> (object, arguments, context) { object.name }
end

Instance Method Summary collapse

Methods included from DefinitionHelpers::Definable

attr_definable

Constructor Details

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

Returns a new instance of Field.

Yields:

Yield Parameters:



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

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

Instance Method Details

#arguments(new_arguments = nil) ⇒ Object



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

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

#arguments=(new_arguments) ⇒ Object

Define the arguments for this field using StringNamedHash



36
37
38
# File 'lib/graph_ql/field.rb', line 36

def arguments=(new_arguments)
  @arguments = GraphQL::DefinitionHelpers::StringNamedHash.new(new_arguments).to_h
end

#resolve(definition_proc) ⇒ Object #resolve(object, arguments, context) ⇒ Object

Overloads:

  • #resolve(definition_proc) ⇒ Object

    Define this field to return a value with ‘definition_proc`

    Examples:

    defining the resolve method

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

    Parameters:

    • definition_proc (Proc)

      The proc to evaluate to get a value

  • #resolve(object, arguments, context) ⇒ Object

    Get a value for this field

    Examples:

    resolving a field value

    field.resolve(obj, args, ctx)

    Parameters:

    • object (Object)

      The object this field belongs to

    • arguments (Hash)

      Arguments declared in the query

    • context (GraphQL::Query::Context)


54
55
56
57
58
59
60
# File 'lib/graph_ql/field.rb', line 54

def resolve(proc_or_object, arguments=nil, context=nil)
  if arguments.nil? && context.nil?
    @resolve_proc = proc_or_object
  else
    @resolve_proc.call(proc_or_object, arguments, context)
  end
end

#type(return_type) ⇒ Object #type(return_type_proc) ⇒ Object

Overloads:

  • #type(return_type) ⇒ Object

    Define the return type for this field

    Parameters:

  • #type(return_type_proc) ⇒ Object

    Wrap the return type in a proc,which will cause the type to be lazy-evaled,

    That’s nice if you have load-order issues.

    Examples:

    lazy-evaled return type

    field.type(-> { MyCircularDependentType })

    Parameters:

    • return_type_proc (Proc)

      A proc which returns the return type for this field



73
74
75
76
77
78
79
80
81
# File 'lib/graph_ql/field.rb', line 73

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