Class: GraphQL::Field

Inherits:
Object
  • Object
show all
Includes:
Define::InstanceDefinable
Defined in:
lib/graphql/field.rb,
lib/graphql/field/resolve.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

Custom complexity values

# Complexity can be a number or a proc.

# Complexity can be defined with a keyword:
field :expensive_calculation, !types.Int, complexity: 10

# Or inside the block:
field :expensive_calculation_2, !types.Int do
  complexity -> (ctx, args, child_complexity) { ctx[:current_user].staff? ? 0 : 10 }
end

Calculating the complexity of a list field

field :items, types[ItemType] do
  argument :limit, !types.Int
  # Mulitply the child complexity by the possible items on the list
  complexity -> (ctx, args, child_complexity) { child_complexity * args[:limit] }
end

Defined Under Namespace

Modules: Resolve

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Define::InstanceDefinable

#definition_proc=, included, #metadata

Constructor Details

#initializeField

Returns a new instance of Field.



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

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

Instance Attribute Details

#argumentsHash<String => GraphQL::Argument>

Returns Map String argument names to their Argument implementations.

Returns:



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

def arguments
  ensure_defined
  @arguments
end

#complexityNumeric, Proc

Returns The complexity for this field (default: 1), as a constant or a proc like ‘-> (query_ctx, args, child_complexity) { } # Numeric`.

Returns:

  • (Numeric, Proc)

    The complexity for this field (default: 1), as a constant or a proc like ‘-> (query_ctx, args, child_complexity) { } # Numeric`



85
86
87
88
# File 'lib/graphql/field.rb', line 85

def complexity
  ensure_defined
  @complexity
end

#nameString

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

Returns:



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

def name
  ensure_defined
  @name
end

#resolve_procObject (readonly)

Returns the value of attribute resolve_proc.



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

def resolve_proc
  @resolve_proc
end

Instance Method Details

#hash_key=(new_hash_key) ⇒ Object

Parameters:

  • new_hash_key (Symbol)

    A key to access with ‘#[key]` to resolve this field. Overrides the existing resolve proc.



150
151
152
153
154
# File 'lib/graphql/field.rb', line 150

def hash_key=(new_hash_key)
  ensure_defined
  @hash_key = new_hash_key
  self.resolve = nil # reset resolve proc
end

#property=(new_property) ⇒ Object

Parameters:

  • new_property (Symbol)

    A method to call to resolve this field. Overrides the existing resolve proc.



143
144
145
146
147
# File 'lib/graphql/field.rb', line 143

def property=(new_property)
  ensure_defined
  @property = new_property
  self.resolve = nil # reset resolve proc
end

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


105
106
107
108
# File 'lib/graphql/field.rb', line 105

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

#resolve=(resolve_proc) ⇒ Object



110
111
112
113
# File 'lib/graphql/field.rb', line 110

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

#to_sObject



156
157
158
# File 'lib/graphql/field.rb', line 156

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

#typeObject

Get the return type for this field.



122
123
124
125
126
127
# File 'lib/graphql/field.rb', line 122

def type
  @clean_type ||= begin
    ensure_defined
    GraphQL::BaseType.resolve_related_type(@dirty_type)
  end
end

#type=(new_return_type) ⇒ Object



115
116
117
118
119
# File 'lib/graphql/field.rb', line 115

def type=(new_return_type)
  ensure_defined
  @clean_type = nil
  @dirty_type = new_return_type
end