class GraphQL::Field

@api deprecated

Attributes

arguments[RW]

@return [Hash<String => GraphQL::Argument>] Map String argument names to their {GraphQL::Argument} implementations

arguments_class[RW]
ast_node[RW]
complexity[RW]

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

connection[W]
connection_max_page_size[RW]

@return [nil, Integer]

deprecation_reason[RW]

@return [String, nil] The client-facing reason why this field is deprecated (if present, the field is deprecated)

description[RW]

@return [String, nil] The client-facing description of this field

edge_class[RW]

@return [nil, Class] @api private

function[RW]

@return [Object, GraphQL::Function] The function used to derive this field

graphql_name[R]

@return [String] The name of this field on its {GraphQL::ObjectType} (or {GraphQL::InterfaceType})

hash_key[R]

@return [Object, nil] The key to access with `obj.[]` to resolve this field (overrides {#name} if present)

introspection[W]
lazy_resolve_proc[R]

@return [<#call(obj, args, ctx)>] A proc-like object which can be called trigger a lazy resolution

mutation[RW]

@return [GraphQL::Relay::Mutation, nil] The mutation this field was derived from, if it was derived from a mutation

name[R]

@return [String] The name of this field on its {GraphQL::ObjectType} (or {GraphQL::InterfaceType})

property[R]

@return [Symbol, nil] The method to call on `obj` to return this field (overrides {#name} if present)

relay_node_field[RW]

@return [Boolean] True if this is the Relay find-by-id field

relay_nodes_field[RW]

@return [Boolean] True if this is the Relay find-by-ids field

resolve_proc[R]

@return [<#call(obj, args, ctx)>] A proc-like object which can be called to return the field's value

subscription_scope[RW]

@return [nil, String] Prefix for subscription names from this field

trace[RW]

@return [Boolean] True if this field should be traced. By default, fields are only traced if they are not a ScalarType or EnumType.

Public Class Methods

new() click to toggle source
# File lib/graphql/field.rb, line 104
def initialize
  @complexity = 1
  @arguments = {}
  @resolve_proc = build_default_resolver
  @lazy_resolve_proc = DefaultLazyResolve
  @relay_node_field = false
  @connection = false
  @connection_max_page_size = nil
  @edge_class = nil
  @trace = nil
  @introspection = false
end

Public Instance Methods

connection?() click to toggle source

@return [Boolean]

# File lib/graphql/field.rb, line 88
def connection?
  @connection
end
edges?() click to toggle source

@return [Boolean]

# File lib/graphql/field.rb, line 97
def edges?
  !!@edge_class
end
get_argument(argument_name) click to toggle source
# File lib/graphql/field.rb, line 210
def get_argument(argument_name)
  arguments[argument_name]
end
hash_key=(new_hash_key) click to toggle source

@param new_hash_key [Symbol] A key to access with `#[key]` to resolve this field. Overrides the existing resolve proc.

# File lib/graphql/field.rb, line 174
def hash_key=(new_hash_key)
  @hash_key = new_hash_key
  self.resolve = nil # reset resolve proc
end
initialize_copy(other) click to toggle source
# File lib/graphql/field.rb, line 117
def initialize_copy(other)
  ensure_defined
  super
  @arguments = other.arguments.dup
end
introspection?() click to toggle source

@return [Boolean] Is this field a predefined introspection field?

# File lib/graphql/field.rb, line 124
def introspection?
  @introspection
end
lazy_resolve(obj, args, ctx) click to toggle source

If {#resolve} returned an object which should be handled lazily, this method will be called later to force the object to return its value. @param obj [Object] The {#resolve}-provided object, registered with {Schema#lazy_resolve} @param args [GraphQL::Query::Arguments] Arguments to this field @param ctx [GraphQL::Query::Context] Context for this field @return [Object] The result of calling the registered method on `obj`

# File lib/graphql/field.rb, line 189
def lazy_resolve(obj, args, ctx)
  @lazy_resolve_proc.call(obj, args, ctx)
end
lazy_resolve=(new_lazy_resolve_proc) click to toggle source

Assign a new resolve proc to this field. Used for {#lazy_resolve}

# File lib/graphql/field.rb, line 194
def lazy_resolve=(new_lazy_resolve_proc)
  @lazy_resolve_proc = new_lazy_resolve_proc
end
name=(new_name) click to toggle source
# File lib/graphql/field.rb, line 156
def name=(new_name)
  old_name = defined?(@name) ? @name : nil
  @name = new_name

  if old_name != new_name && @resolve_proc.is_a?(Field::Resolve::NameResolve)
    # Since the NameResolve would use the old field name,
    # reset resolve proc when the name has changed
    self.resolve = nil
  end
end
prepare_lazy(obj, args, ctx) click to toggle source

Prepare a lazy value for this field. It may be `then`-ed and resolved later. @return [GraphQL::Execution::Lazy] A lazy wrapper around `obj` and its registered method name

# File lib/graphql/field.rb, line 200
def prepare_lazy(obj, args, ctx)
  GraphQL::Execution::Lazy.new {
    lazy_resolve(obj, args, ctx)
  }
end
property=(new_property) click to toggle source

@param new_property [Symbol] A method to call to resolve this field. Overrides the existing resolve proc.

# File lib/graphql/field.rb, line 168
def property=(new_property)
  @property = new_property
  self.resolve = nil # reset resolve proc
end
resolve(object, arguments, context) click to toggle source

Get a value for this field @example resolving a field value

field.resolve(obj, args, ctx)

@param object [Object] The object this field belongs to @param arguments [Hash] Arguments declared in the query @param context [GraphQL::Query::Context]

# File lib/graphql/field.rb, line 135
def resolve(object, arguments, context)
  resolve_proc.call(object, arguments, context)
end
resolve=(new_resolve_proc) click to toggle source

Provide a new callable for this field's resolve function. If `nil`, a new resolve proc will be build based on its {#name}, {#property} or {#hash_key}. @param new_resolve_proc [<#call(obj, args, ctx)>, nil]

# File lib/graphql/field.rb, line 142
def resolve=(new_resolve_proc)
  @resolve_proc = new_resolve_proc || build_default_resolver
end
to_s() click to toggle source
# File lib/graphql/field.rb, line 179
def to_s
  "<Field name:#{name || "not-named"} desc:#{description} resolve:#{resolve_proc}>"
end
type() click to toggle source

Get the return type for this field.

# File lib/graphql/field.rb, line 152
def type
  @clean_type ||= GraphQL::BaseType.resolve_related_type(@dirty_type)
end
type=(new_return_type) click to toggle source
# File lib/graphql/field.rb, line 146
def type=(new_return_type)
  @clean_type = nil
  @dirty_type = new_return_type
end
type_class() click to toggle source
# File lib/graphql/field.rb, line 206
def type_class
  metadata[:type_class]
end

Private Instance Methods

build_default_resolver() click to toggle source
# File lib/graphql/field.rb, line 216
def build_default_resolver
  GraphQL::Field::Resolve.create_proc(self)
end