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, eg `obj[: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. -
#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).
-
#resolve_proc ⇒ <#call(obj, args,ctx)>
readonly
A proc-like object which can be called to return the field's value.
Instance Method Summary collapse
- #hash_key=(new_hash_key) ⇒ Object
-
#initialize ⇒ Field
constructor
A new instance of Field.
- #property=(new_property) ⇒ Object
-
#resolve(object, arguments, context) ⇒ Object
Get a value for this field.
- #resolve=(resolve_proc) ⇒ Object
- #to_s ⇒ Object
-
#type ⇒ Object
Get the return type for this field.
- #type=(new_return_type) ⇒ Object
Methods included from Define::InstanceDefinable
#definition_proc=, included, #metadata
Constructor Details
#initialize ⇒ Field
Returns a new instance of Field.
161 162 163 164 165 |
# File 'lib/graphql/field.rb', line 161 def initialize @complexity = 1 @arguments = {} @resolve_proc = build_default_resolver end |
Instance Attribute Details
#arguments ⇒ Hash<String => GraphQL::Argument>
Returns Map String argument names to their Argument implementations.
143 144 145 146 |
# File 'lib/graphql/field.rb', line 143 def arguments ensure_defined @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.
154 155 156 157 |
# File 'lib/graphql/field.rb', line 154 def complexity ensure_defined @complexity end |
#mutation ⇒ GraphQL::Relay::Mutation?
Returns The mutation this field was derived from, if it was derived from a mutation.
|
|
# File 'lib/graphql/field.rb', line 150
|
#name ⇒ String
Returns The name of this field on its ObjectType (or InterfaceType).
135 136 137 138 |
# File 'lib/graphql/field.rb', line 135 def name ensure_defined @name end |
#resolve_proc ⇒ <#call(obj, args,ctx)> (readonly)
Returns A proc-like object which can be called to return the field's value.
132 133 134 |
# File 'lib/graphql/field.rb', line 132 def resolve_proc @resolve_proc end |
Instance Method Details
#hash_key=(new_hash_key) ⇒ Object
219 220 221 222 223 |
# File 'lib/graphql/field.rb', line 219 def hash_key=(new_hash_key) ensure_defined @hash_key = new_hash_key self.resolve = nil # reset resolve proc end |
#property=(new_property) ⇒ Object
212 213 214 215 216 |
# File 'lib/graphql/field.rb', line 212 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
174 175 176 177 |
# File 'lib/graphql/field.rb', line 174 def resolve(object, arguments, context) ensure_defined resolve_proc.call(object, arguments, context) end |
#resolve=(resolve_proc) ⇒ Object
179 180 181 182 |
# File 'lib/graphql/field.rb', line 179 def resolve=(resolve_proc) ensure_defined @resolve_proc = resolve_proc || build_default_resolver end |
#to_s ⇒ Object
225 226 227 |
# File 'lib/graphql/field.rb', line 225 def to_s "<Field name:#{name || "not-named"} desc:#{description} resolve:#{resolve_proc}>" end |
#type ⇒ Object
Get the return type for this field.
191 192 193 194 195 196 |
# File 'lib/graphql/field.rb', line 191 def type @clean_type ||= begin ensure_defined GraphQL::BaseType.(@dirty_type) end end |
#type=(new_return_type) ⇒ Object
184 185 186 187 188 |
# File 'lib/graphql/field.rb', line 184 def type=(new_return_type) ensure_defined @clean_type = nil @dirty_type = new_return_type end |