Class: GraphQL::Field
- Inherits:
-
Object
- Object
- GraphQL::Field
- 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.
A field must have a return type, but if you want to defer the return type calculation until later, you can pass a proc for the return type. That proc will be called when the schema is defined.
For complex field definition, you can pass a block to the field helper, eg field :name do ... end.
This block is equivalent to calling GraphQL::Field.define { ... }.
Resolve
Fields have resolve functions to determine their values at query-time.
The default implementation is to call a method on the object based on the field name.
You can specify a custom proc with the resolve helper.
There are some shortcuts for common resolve implementations:
- Provide
property:to call a method with a different name than the field name - Provide
hash_key:to resolve the field by doing a key lookup, egobj[:my_hash_key]
Arguments
Fields can take inputs; they're called arguments. You can define them with the argument helper.
They can have default values which will be provided to resolve if the query doesn't include a value.
Only certain types maybe used for inputs:
- Scalars
- Enums
- Input Objects
- Lists of those types
Input types may also be non-null -- in that case, the query will fail if the input is not present.
Complexity
Fields can have complexity values which describe the computation cost of resolving the field.
You can provide the complexity as a constant with complexity: or as a proc, with the complexity helper.
Defined Under Namespace
Modules: Resolve
Instance Attribute Summary collapse
-
#arguments ⇒ Hash<String => GraphQL::Argument>
Map String argument names to their Argument implementations.
-
#complexity ⇒ Numeric, Proc
The complexity for this field (default: 1), as a constant or a proc like
->(query_ctx, args, child_complexity) { } # Numeric. -
#deprecation_reason ⇒ Object
Returns the value of attribute deprecation_reason.
-
#description ⇒ Object
Returns the value of attribute description.
-
#hash_key ⇒ Object
Returns the value of attribute hash_key.
-
#mutation ⇒ GraphQL::Relay::Mutation?
The mutation this field was derived from, if it was derived from a mutation.
-
#name ⇒ String
The name of this field on its ObjectType (or InterfaceType).
-
#property ⇒ Object
Returns the value of attribute property.
-
#relay_node_field ⇒ Boolean
True if this is the Relay find-by-id field.
-
#resolve_proc ⇒ Object
readonly
Returns the value of attribute resolve_proc.
Instance Method Summary collapse
-
#initialize ⇒ Field
constructor
A new instance of Field.
-
#resolve(object, arguments, context) ⇒ Object
Get a value for this field.
-
#resolve=(new_resolve_proc) ⇒ Object
Provide a new callable for this field's resolve function.
- #to_s ⇒ Object
-
#type ⇒ Object
Get the return type for this field.
- #type=(new_return_type) ⇒ Object
Methods included from Define::InstanceDefinable
Constructor Details
#initialize ⇒ Field
Returns a new instance of Field.
157 158 159 160 161 162 |
# File 'lib/graphql/field.rb', line 157 def initialize @complexity = 1 @arguments = {} @resolve_proc = build_default_resolver @relay_node_field = false end |
Instance Attribute Details
#arguments ⇒ Hash<String => GraphQL::Argument>
Returns Map String argument names to their Argument implementations.
148 149 150 |
# File 'lib/graphql/field.rb', line 148 def arguments @arguments end |
#complexity ⇒ Numeric, Proc
Returns The complexity for this field (default: 1), as a constant or a proc like ->(query_ctx, args, child_complexity) { } # Numeric.
157 158 159 |
# File 'lib/graphql/field.rb', line 157 def complexity @complexity end |
#deprecation_reason ⇒ Object
Returns the value of attribute deprecation_reason.
130 131 132 |
# File 'lib/graphql/field.rb', line 130 def deprecation_reason @deprecation_reason end |
#description ⇒ Object
Returns the value of attribute description.
130 131 132 |
# File 'lib/graphql/field.rb', line 130 def description @description end |
#hash_key ⇒ Object
Returns the value of attribute hash_key.
130 131 132 |
# File 'lib/graphql/field.rb', line 130 def hash_key @hash_key end |
#mutation ⇒ GraphQL::Relay::Mutation?
Returns The mutation this field was derived from, if it was derived from a mutation.
151 152 153 |
# File 'lib/graphql/field.rb', line 151 def mutation @mutation end |
#name ⇒ String
Returns The name of this field on its ObjectType (or InterfaceType).
145 146 147 |
# File 'lib/graphql/field.rb', line 145 def name @name end |
#property ⇒ Object
Returns the value of attribute property.
130 131 132 |
# File 'lib/graphql/field.rb', line 130 def property @property end |
#relay_node_field ⇒ Boolean
Returns True if this is the Relay find-by-id field.
133 134 135 |
# File 'lib/graphql/field.rb', line 133 def relay_node_field @relay_node_field end |
#resolve_proc ⇒ Object (readonly)
Returns the value of attribute resolve_proc.
143 144 145 |
# File 'lib/graphql/field.rb', line 143 def resolve_proc @resolve_proc end |
Instance Method Details
#resolve(object, arguments, context) ⇒ Object
Get a value for this field
171 172 173 |
# File 'lib/graphql/field.rb', line 171 def resolve(object, arguments, context) resolve_proc.call(object, arguments, context) end |
#resolve=(new_resolve_proc) ⇒ Object
178 179 180 |
# File 'lib/graphql/field.rb', line 178 def resolve=(new_resolve_proc) @resolve_proc = new_resolve_proc || build_default_resolver end |
#to_s ⇒ Object
216 217 218 |
# File 'lib/graphql/field.rb', line 216 def to_s "<Field name:#{name || "not-named"} desc:#{description} resolve:#{resolve_proc}>" end |
#type ⇒ Object
Get the return type for this field.
188 189 190 |
# File 'lib/graphql/field.rb', line 188 def type @clean_type ||= GraphQL::BaseType.(@dirty_type) end |
#type=(new_return_type) ⇒ Object
182 183 184 185 |
# File 'lib/graphql/field.rb', line 182 def type=(new_return_type) @clean_type = nil @dirty_type = new_return_type end |