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::SubscriptionRoot::Extension
Instance Attribute Summary collapse
-
#field ⇒ GraphQL::Schema::Field
readonly
-
#options ⇒ Object
readonly
Instance Method Summary collapse
-
#after_resolve(object:, arguments:, context:, value:, memo:) ⇒ Object
Called after #field was resolved, and after any lazy values (like
Promise
s) were synced, but before the value was added to the GraphQL response. -
#apply ⇒ void
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:, arguments:, context:) {|object, arguments, memo| ... } ⇒ Object
Called before resolving #field.
Constructor Details
#initialize(field:, options:) ⇒ FieldExtension
Called when the extension is mounted with extension(name, options)
.
The instance is frozen to avoid improper use of state during execution.
22 23 24 25 26 27 |
# File 'lib/graphql/schema/field_extension.rb', line 22 def initialize(field:, options:) @field = field @options = apply freeze end |
Instance Attribute Details
#field ⇒ GraphQL::Schema::Field (readonly)
13 14 15 |
# File 'lib/graphql/schema/field_extension.rb', line 13 def field @field end |
#options ⇒ Object (readonly)
16 17 18 |
# File 'lib/graphql/schema/field_extension.rb', line 16 def @options end |
Instance Method Details
#after_resolve(object:, arguments:, context:, value:, memo:) ⇒ Object
Called after #field was resolved, and after any lazy values (like Promise
s) were synced,
but before the value was added to the GraphQL response.
Whatever this hook returns will be used as the return value.
64 65 66 |
# File 'lib/graphql/schema/field_extension.rb', line 64 def after_resolve(object:, arguments:, context:, value:, memo:) value end |
#apply ⇒ void
This method returns an undefined value.
Called when this extension is attached to a field. The field definition may be extended during this method.
32 33 |
# File 'lib/graphql/schema/field_extension.rb', line 32 def apply end |
#resolve(object:, arguments:, context:) {|object, arguments, memo| ... } ⇒ Object
Called before resolving #field. It should either:
yield
values to continue execution; OR- return something else to shortcut field execution.
Whatever this method returns will be used for execution.
49 50 51 |
# File 'lib/graphql/schema/field_extension.rb', line 49 def resolve(object:, arguments:, context:) yield(object, arguments, nil) end |