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

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 DefinitionHelpers::DefinedByConfig

included

Constructor Details

#initializeField

Returns a new instance of Field.



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

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

Instance Attribute Details

#arguments(new_arguments = nil) ⇒ Object

Returns the value of attribute arguments.



42
43
44
# File 'lib/graphql/field.rb', line 42

def arguments
  @arguments
end

#deprecation_reasonObject

Returns the value of attribute deprecation_reason.



42
43
44
# File 'lib/graphql/field.rb', line 42

def deprecation_reason
  @deprecation_reason
end

#descriptionObject

Returns the value of attribute description.



42
43
44
# File 'lib/graphql/field.rb', line 42

def description
  @description
end

#nameObject

Returns the value of attribute name.



42
43
44
# File 'lib/graphql/field.rb', line 42

def name
  @name
end

#propertyObject

Returns the value of attribute property.



42
43
44
# File 'lib/graphql/field.rb', line 42

def property
  @property
end

#resolve_procObject (readonly)

Returns the value of attribute resolve_proc.



43
44
45
# File 'lib/graphql/field.rb', line 43

def resolve_proc
  @resolve_proc
end

#typeObject

Get the return type for this field.



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

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)


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

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

#resolve=(resolve_proc) ⇒ Object



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

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

#to_sObject



92
93
94
# File 'lib/graphql/field.rb', line 92

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