Class: GraphQL::Field

Inherits:
Object
  • Object
show all
Includes:
Define::InstanceDefinable
Defined in:
lib/graphql/field.rb

Overview

Fields belong to ObjectTypes and InterfaceTypes.

They’re usually created with the ‘field` helper. If you create it by hand, make sure #name is a String.

Examples:

creating a field

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

handling a circular reference

# If the field's type isn't defined yet, you have two options:

GraphQL::ObjectType.define do
  # If you pass a Proc, it will be evaluated at schema build-time
  field :city, -> { CityType }
  # If you pass a String, it will be looked up in the global namespace at schema build-time
  field :country, "CountryType"
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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Define::InstanceDefinable

included

Constructor Details

#initializeField

Returns a new instance of Field.



54
55
56
57
# File 'lib/graphql/field.rb', line 54

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

Instance Attribute Details

#argumentsHash<String, GraphQL::Argument>

Returns Map String argument names to their Argument implementations.

Returns:



52
53
54
# File 'lib/graphql/field.rb', line 52

def arguments
  @arguments
end

#deprecation_reasonObject

Returns the value of attribute deprecation_reason.



45
46
47
# File 'lib/graphql/field.rb', line 45

def deprecation_reason
  @deprecation_reason
end

#descriptionObject

Returns the value of attribute description.



45
46
47
# File 'lib/graphql/field.rb', line 45

def description
  @description
end

#nameString

Returns The name of this field on its ObjectType (or InterfaceType).

Returns:



49
50
51
# File 'lib/graphql/field.rb', line 49

def name
  @name
end

#propertyObject

Returns the value of attribute property.



45
46
47
# File 'lib/graphql/field.rb', line 45

def property
  @property
end

#resolve_procObject (readonly)

Returns the value of attribute resolve_proc.



46
47
48
# File 'lib/graphql/field.rb', line 46

def resolve_proc
  @resolve_proc
end

#typeObject

Get the return type for this field.



75
76
77
# File 'lib/graphql/field.rb', line 75

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)


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

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

#resolve=(resolve_proc) ⇒ Object



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

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

#to_sObject



100
101
102
# File 'lib/graphql/field.rb', line 100

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