class GraphQL::Field


end
field :name, name_field
# The second argument may be a GraphQL::Field
NamedType = GraphQL::ObjectType.define do
end
resolve -> (object, arguments, context) { object.name }
description(“The name of this thing”)
type(!types.String)
name(“Name”)
name_field = GraphQL::Field.define do
@example Creating a field, then assigning it to a type
end
complexity -> (ctx, args, child_complexity) { child_complexity * args }
# Mulitply the child complexity by the possible items on the list
argument :limit, !types.Int
field :items, types do
@example Calculating the complexity of a list field
end
complexity -> (ctx, args, child_complexity) { ctx.staff? ? 0 : 10 }
field :expensive_calculation_2, !types.Int do
# Or inside the block:
field :expensive_calculation, !types.Int, complexity: 10
# Complexity can be defined with a keyword:
# Complexity can be a number or a proc.
@example Custom complexity values
You can provide the complexity as a constant with ‘complexity:` or as a proc, with the `complexity` helper.
Fields can have complexity values which describe the computation cost of resolving the field.
## Complexity
if the input is not present.
Input types may also be non-null – in that case, the query will fail
- Lists of those types
- Input Objects
- Enums
- Scalars
Only certain types maybe used for inputs:
end
}
# …
# => false if no value was provided in the query
resolve -> (obj, args, ctx) {
argument :includePast, types.Boolean, default_value: false
# by default, don’t include past events
field :events, types do
@example Argument with a default value
They can have default values which will be provided to ‘resolve` if the query doesn’t include a value.
end
}
Student.where(grade: args)
resolve -> (obj, args, ctx) {
argument :grade, types.Int
field :students, types do
@example Create a field with an argument
Fields can take inputs; they’re called arguments. You can define them with the ‘argument` helper.
## Arguments
end
field :firstName, types.String, hash_key: :first_name
# use the `hash_key` keyword:
GraphQL::ObjectType.define do
@example Create a field looks up with `[hash_key]`
end
field :firstName, types.String, property: :first_name
# use the `property` keyword:
GraphQL::ObjectType.define do
@example Create a field that calls a different method on the object
- Provide `hash_key:` to resolve the field by doing a key lookup, eg `obj`
- Provide `property:` to call a method with a different name than the field name
There are some shortcuts for common `resolve` implementations:
You can specify a custom proc with the `resolve` helper.
end
field :name, types.String, “The name of this thing ”
GraphQL::ObjectType.define do
@example Create a field which calls a method with the same name.
The default implementation is to call a method on the object based on the field name.
Fields have `resolve` functions to determine their values at query-time.
## Resolve
end
# field definition continues inside the block
field :city, CityType do
@example Defining a field with a block
This block is equivalent to calling `GraphQL::Field.define { … }`.
For complex field definition, you can pass a block to the `field` helper, eg `field :name do … end`.
field :city, -> { TypeForModelName.find(“City”) }
# If the field’s type isn’t defined yet, you can pass a proc
@example Lazy type resolution
you can pass a proc for the return type. That proc will be called when the schema is defined.
A field must have a return type, but if you want to defer the return type calculation until later,
They’re usually created with the ‘field` helper. If you create it by hand, make sure {#name} is a String.
{Field}s belong to {ObjectType}s and {InterfaceType}s.

def arguments

Returns:
  • (Hash GraphQL::Argument>) - Map String argument names to their {GraphQL::Argument} implementations
def arguments
  ensure_defined
  @arguments
end

def build_default_resolver

def build_default_resolver
  GraphQL::Field::Resolve.create_proc(self)
end

def complexity

Returns:
  • (Numeric, Proc) - The complexity for this field (default: 1), as a constant or a proc like `-> (query_ctx, args, child_complexity) { } # Numeric`
def complexity
  ensure_defined
  @complexity
end

def hash_key=(new_hash_key)

Parameters:
  • new_hash_key (Symbol) -- A key to access with `#[key]` to resolve this field. Overrides the existing resolve proc.
def hash_key=(new_hash_key)
  ensure_defined
  @hash_key = new_hash_key
  self.resolve = nil # reset resolve proc
end

def initialize

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

def name

Returns:
  • (String) - The name of this field on its {GraphQL::ObjectType} (or {GraphQL::InterfaceType})
def name
  ensure_defined
  @name
end

def name=(new_name)

This is important because {#name} may be used by {#resolve}.

passing the same {Field} to multiple `.field` calls.
You can only set a field's name _once_ -- this to prevent
def name=(new_name)
  ensure_defined
  if @name.nil?
    @name = new_name
  elsif @name != new_name
    raise("Can't rename an already-named field. (Tried to rename \"#{@name}\" to \"#{new_name}\".) If you're passing a field with the `field:` argument, make sure it's an unused instance of GraphQL::Field.")
  end
end

def property=(new_property)

Parameters:
  • new_property (Symbol) -- A method to call to resolve this field. Overrides the existing resolve proc.
def property=(new_property)
  ensure_defined
  @property = new_property
  self.resolve = nil # reset resolve proc
end

def resolve(object, arguments, context)

Parameters:
  • context (GraphQL::Query::Context) --
  • arguments (Hash) -- Arguments declared in the query
  • object (Object) -- The object this field belongs to

Other tags:
    Example: resolving a field value -
def resolve(object, arguments, context)
  ensure_defined
  resolve_proc.call(object, arguments, context)
end

def resolve=(resolve_proc)

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

def to_s

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

def type

Get the return type for this field.
def type
  @clean_type ||= begin
    ensure_defined
    GraphQL::BaseType.resolve_related_type(@dirty_type)
  end
end

def type=(new_return_type)

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