class GraphQL::Schema::FieldExtension
Extend this class to make field-level customizations to resolve behavior.
When a extension is added to a field with extension(MyExtension), a MyExtension instance is created, and its hooks are applied whenever that field is called.
The instance is frozen so that instance variables arenβt modified during query execution, which could cause all kinds of issues due to race conditions.
Attributes
Returns
-
Array<Symbol>, nilβdefault_arguments added, if any were added (otherwise,nil)
Returns
Returns
Public Class Methods
# File lib/graphql/schema/field_extension.rb, line 66 def default_argument(*argument_args, **argument_kwargs) configs = @own_default_argument_configurations ||= [] configs << [argument_args, argument_kwargs] end
See the GraphQL::Schema::Argument API. See HasArguments#argument for argument configuration.
# File lib/graphql/schema/field_extension.rb, line 52 def default_argument_configurations args = superclass.respond_to?(:default_argument_configurations) ? superclass.default_argument_configurations : nil if @own_default_argument_configurations if args args.concat(@own_default_argument_configurations) else args = @own_default_argument_configurations.dup end end args end
Returns
-
Array(Array, Hash), nilβ A list of default argument configs, ornilif there arenβt any
# File lib/graphql/schema/field_extension.rb, line 85 def extras(new_extras = nil) if new_extras @own_extras = new_extras end inherited_extras = self.superclass.respond_to?(:extras) ? superclass.extras : nil if @own_extras if inherited_extras inherited_extras + @own_extras else @own_extras end elsif inherited_extras inherited_extras else GraphQL::EmptyObjects::EMPTY_ARRAY end end
If configured, these extras will be added to the field if they arenβt already present, but removed by from arguments before the fieldβs resolve is called. (The extras will be present for other extensions, though.)
Parameters
-
new_extras(Array<Symbol>) β If provided, assign extras used by this extension
Returns
-
Array<Symbol>β any extras assigned to this extension
# File lib/graphql/schema/field_extension.rb, line 38 def initialize(field:, options:) @field = field @options = options || {} @added_default_arguments = nil apply end
Called when the extension is mounted with extension(name, options). The instance will be frozen to avoid improper use of state during execution.
Parameters
-
field(GraphQL::Schema::Field) β The field where this extension was mounted -
options(Object) β The second argument toextension, or{}if nothing was passed.
Public Instance Methods
Source
# File lib/graphql/schema/field_extension.rb, line 126 def after_define end
Called after the fieldβs definition block has been executed. (Any arguments from the block are present on field)
Returns
-
void
# File lib/graphql/schema/field_extension.rb, line 206 def after_resolve(object: nil, objects: nil, arguments:, context:, values: nil, value: nil, memo:) value.nil? ? values : value end
Called after field was resolved, and after any lazy values (like Promises) were synced, but before the value was added to the GraphQL response.
Whatever this hook returns will be used as the return value.
Parameters
-
object(Object) β The object the field is being resolved on (not passed by new execution) -
objects(Array<Object>) β The object the field is being resolved on (passed by new execution) -
arguments(Hash) β Ruby keyword arguments for resolving this field -
context(Query::Context) β the context for this query -
value(Object) β Whatever the field previously returned (not passed by new execution) -
values(Array<Object>) β Whatever the field previously returned (passed by new execution) -
memo(Object) β The third value yielded by resolve, ornilif there wasnβt one
Returns
-
Objectβ The return value for this field.
Source
# File lib/graphql/schema/field_extension.rb, line 114 def apply end
Called when this extension is attached to a field. The field definition may be extended during this method.
Returns
-
void
Source
# File lib/graphql/schema/field_extension.rb, line 181 def resolve(object: nil, objects: nil, arguments:, context:) yield(object.nil? ? objects : object, arguments, nil) end
Called before resolving field. It should either:
-
yieldvalues to continue execution; OR -
return something else to shortcut field execution.
Whatever this method returns will be used for execution.
Parameters
-
object(Object) β The object the field is being resolved on (not passed by new execution) -
objects(Array<Object>) β The objects the field is being resolved on (passed by new execution) -
arguments(Hash) β Ruby keyword arguments for resolving this field -
context(Query::Context) β the context for this query
Yields
-
object_or_objects(Object, Array<Object>) β The object or objects (new execution) to continue resolving the field on -
arguments(Hash) β The keyword arguments to continue resolving with -
memo(Object) β Any extension-specific value which will be passed to after resolve later
Returns
-
Objectβ The return value for this field.