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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Define::InstanceDefinable

included

Constructor Details

#initializeField

Returns a new instance of Field.



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

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:



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

def arguments
  @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`



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

def complexity
  @complexity
end

#deprecation_reasonObject

Returns the value of attribute deprecation_reason.



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

def deprecation_reason
  @deprecation_reason
end

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#nameString

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

Returns:



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

def name
  @name
end

#propertyObject

Returns the value of attribute property.



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

def property
  @property
end

#resolve_procObject (readonly)

Returns the value of attribute resolve_proc.



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

def resolve_proc
  @resolve_proc
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)


88
89
90
# File 'lib/graphql/field.rb', line 88

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

#resolve=(resolve_proc) ⇒ Object



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

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

#to_sObject



118
119
120
# File 'lib/graphql/field.rb', line 118

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

#typeObject

Get the return type for this field.



102
103
104
# File 'lib/graphql/field.rb', line 102

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

#type=(new_return_type) ⇒ Object



96
97
98
99
# File 'lib/graphql/field.rb', line 96

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