Class: GraphQL::Schema::FieldExtension
- Inherits:
-
Object
- Object
- GraphQL::Schema::FieldExtension
- Defined in:
- lib/graphql/schema/field_extension.rb
Overview
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.
Direct Known Subclasses
GraphQL::Schema::Field::ConnectionExtension, GraphQL::Schema::Field::ScopeExtension, GraphQL::Subscriptions::DefaultSubscriptionResolveExtension
Instance Attribute Summary collapse
-
#added_default_arguments ⇒ Object
readonly
Returns.
-
#added_extras ⇒ Object
readonly
:nodoc:.
-
#field ⇒ Object
readonly
Returns.
-
#options ⇒ Object
readonly
Returns.
Class Method Summary collapse
-
.default_argument(*argument_args, **argument_kwargs) ⇒ Object
See the GraphQL::Schema::Argument API.
-
.default_argument_configurations ⇒ Object
Returns.
-
.extras(new_extras = nil) ⇒ Object
If configured, these
extraswill be added to the field if they aren't already present, but removed by fromargumentsbefore the field'sresolveis called.
Instance Method Summary collapse
-
#after_define ⇒ Object
Called after the field's definition block has been executed.
-
#after_define_apply ⇒ Object
:nodoc:.
-
#after_resolve(object: nil, objects: nil, arguments:, context:, values: nil, value: nil, memo:) ⇒ Object
Called after field was resolved, and after any lazy values (like
Promises) were synced, but before the value was added to the GraphQL response. -
#apply ⇒ Object
Called when this extension is attached to a field.
-
#initialize(field:, options:) ⇒ FieldExtension
constructor
Called when the extension is mounted with
extension(name, options). -
#resolve(object: nil, objects: nil, arguments:, context:) {|object.nil? ? objects : object, arguments, nil| ... } ⇒ Object
Called before resolving field.
Constructor Details
#initialize(field:, options:) ⇒ FieldExtension
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 mountedoptions(Object) — The second argument toextension, or{}if nothing was passed.
:call-seq:
initialize(GraphQL::Schema::Field field:, Object options:)
39 40 41 42 43 44 |
# File 'lib/graphql/schema/field_extension.rb', line 39 def initialize(field:, options:) @field = field @options = || {} @added_default_arguments = nil apply end |
Instance Attribute Details
#added_default_arguments ⇒ Object (readonly)
Returns
Array<Symbol>, nil—default_arguments added, if any were added (otherwise,nil)
27 28 29 |
# File 'lib/graphql/schema/field_extension.rb', line 27 def added_default_arguments @added_default_arguments end |
#added_extras ⇒ Object (readonly)
:nodoc:
154 155 156 |
# File 'lib/graphql/schema/field_extension.rb', line 154 def added_extras @added_extras end |
#field ⇒ Object (readonly)
Returns
GraphQL::Schema::Field
17 18 19 |
# File 'lib/graphql/schema/field_extension.rb', line 17 def field @field end |
#options ⇒ Object (readonly)
Returns
Object
22 23 24 |
# File 'lib/graphql/schema/field_extension.rb', line 22 def @options end |
Class Method Details
.default_argument(*argument_args, **argument_kwargs) ⇒ Object
See the GraphQL::Schema::Argument API. See HasArguments#argument for argument configuration.
67 68 69 70 |
# File 'lib/graphql/schema/field_extension.rb', line 67 def default_argument(*argument_args, **argument_kwargs) configs = @own_default_argument_configurations ||= [] configs << [argument_args, argument_kwargs] end |
.default_argument_configurations ⇒ Object
Returns
Array(Array, Hash), nil— A list of default argument configs, ornilif there aren't any
:call-seq:
default_argument_configurations() -> Array(Array, Hash) | nil
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/graphql/schema/field_extension.rb', line 53 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 |
.extras(new_extras = nil) ⇒ Object
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
:call-seq:
extras(Array[Symbol] new_extras) -> Array[Symbol]
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/graphql/schema/field_extension.rb', line 86 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 |
Instance Method Details
#after_define ⇒ Object
Called after the field's definition block has been executed.
(Any arguments from the block are present on field)
Returns
void
:call-seq:
after_define() -> void
127 128 |
# File 'lib/graphql/schema/field_extension.rb', line 127 def after_define end |
#after_define_apply ⇒ Object
:nodoc:
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/graphql/schema/field_extension.rb', line 130 def after_define_apply # :nodoc: after_define if (configs = self.class.default_argument_configurations) existing_keywords = field.all_argument_definitions.map(&:keyword) existing_keywords.uniq! @added_default_arguments = [] configs.each do |config| argument_args, argument_kwargs = config arg_name = argument_args[0] if !existing_keywords.include?(arg_name) @added_default_arguments << arg_name field.argument(*argument_args, **argument_kwargs) end end end if !(extras = self.class.extras).empty? @added_extras = extras - field.extras field.extras(@added_extras) else @added_extras = nil end freeze end |
#after_resolve(object: nil, objects: nil, arguments:, context:, values: nil, value: nil, memo:) ⇒ Object
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 fieldcontext(Query::Context) — the context for this queryvalue(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.
:call-seq:
after_resolve(Object object:, Array[Object] objects:, Hash arguments:, Query::Context context:, Array[Object] values:, Object value:, Object memo:) -> Object
207 208 209 |
# File 'lib/graphql/schema/field_extension.rb', line 207 def after_resolve(object: nil, objects: nil, arguments:, context:, values: nil, value: nil, memo:) value.nil? ? values : value end |
#apply ⇒ Object
Called when this extension is attached to a field. The field definition may be extended during this method.
Returns
void
:call-seq:
apply() -> void
115 116 |
# File 'lib/graphql/schema/field_extension.rb', line 115 def apply end |
#resolve(object: nil, objects: nil, arguments:, context:) {|object.nil? ? objects : object, arguments, nil| ... } ⇒ Object
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 fieldcontext(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 onarguments(Hash) — The keyword arguments to continue resolving withmemo(Object) — Any extension-specific value which will be passed to after resolve later
Returns
Object— The return value for this field.
:call-seq:
resolve(Object object:, Array[Object] objects:, Hash arguments:, Query::Context context:) -> Object
182 183 184 |
# File 'lib/graphql/schema/field_extension.rb', line 182 def resolve(object: nil, objects: nil, arguments:, context:) yield(object.nil? ? objects : object, arguments, nil) end |