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.



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

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

#typeObject

Get the return type for this field.



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

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)


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

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

#resolve=(resolve_proc) ⇒ Object



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

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

#to_sObject



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

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