Class: GraphQL::Field

Inherits:
Object
  • Object
show all
Includes:
DefinitionHelpers::DefinedByConfig
Defined in:
lib/graphql/field.rb

Overview

Fields belong to ObjectTypes and InterfaceTypes.

They’re usually created with the ‘field` helper.

Examples:

creating a field

GraphQL::ObjectType.define do
  field :name, types.String, "The name of this thing "
end

creating a field that accesses a different property on the object

GraphQL::ObjectType.define do
  # use the `property` option:
  field :firstName, types.String, property: :first_name
end

defining a field, then attaching it to a type

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

NamedType = GraphQL::ObjectType.define do
  # use the `field` option:
  field :name, field: name_field
end

Constant Summary collapse

DEFAULT_RESOLVE =
-> (o, a, c) { GraphQL::Query::DEFAULT_RESOLVE }

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DefinitionHelpers::DefinedByConfig

included

Constructor Details

#initializeField

Returns a new instance of Field.



37
38
39
40
# File 'lib/graphql/field.rb', line 37

def initialize
  @arguments = {}
  @resolve_proc = DEFAULT_RESOLVE
end

Instance Attribute Details

#arguments(new_arguments = nil) ⇒ Object

Returns the value of attribute arguments.



33
34
35
# File 'lib/graphql/field.rb', line 33

def arguments
  @arguments
end

#deprecation_reasonObject

Returns the value of attribute deprecation_reason.



33
34
35
# File 'lib/graphql/field.rb', line 33

def deprecation_reason
  @deprecation_reason
end

#descriptionObject

Returns the value of attribute description.



33
34
35
# File 'lib/graphql/field.rb', line 33

def description
  @description
end

#nameObject

Returns the value of attribute name.



33
34
35
# File 'lib/graphql/field.rb', line 33

def name
  @name
end

#resolve_procObject (readonly)

Returns the value of attribute resolve_proc.



34
35
36
# File 'lib/graphql/field.rb', line 34

def resolve_proc
  @resolve_proc
end

#typeObject

Get the return type for this field.



70
71
72
# File 'lib/graphql/field.rb', line 70

def type
  @type
end

Instance Method Details

#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)


61
62
63
# File 'lib/graphql/field.rb', line 61

def resolve(object, arguments, context)
  @resolve_proc.call(object, arguments, context)
end

#resolve=(resolve_proc) ⇒ Object



65
66
67
# File 'lib/graphql/field.rb', line 65

def resolve=(resolve_proc)
  @resolve_proc = resolve_proc || DEFAULT_RESOLVE
end

#to_sObject



78
79
80
# File 'lib/graphql/field.rb', line 78

def to_s
  "<Field: #{name || "not-named"}>"
end