Class: GraphQL::Schema::Subscription
- Extended by:
- Member::HasFields, Resolver::HasPayloadType
- Defined in:
- lib/graphql/schema/subscription.rb
Overview
This class can be extended to create fields on your subscription root.
It provides hooks for the different parts of the subscription lifecycle:
#authorized?
: called before initial subscription and subsequent updates#subscribe
: called for the initial subscription#update
: called for subsequent update
Also, #unsubscribe
terminates the subscription.
Defined Under Namespace
Classes: EarlyTerminationError, NoUpdateError, UnsubscribedError
Constant Summary collapse
- READING_SCOPE =
Call this method to provide a new subscription_scope; OR call it without an argument to get the subscription_scope
::Object.new
Constants included from Member::GraphQLTypeNames
Member::GraphQLTypeNames::Boolean, Member::GraphQLTypeNames::ID, Member::GraphQLTypeNames::Int
Instance Attribute Summary
Attributes inherited from Resolver
Class Method Summary collapse
-
.field_options ⇒ Object
Overriding Resolver#field_options to include subscription_scope.
-
.subscription_scope(new_scope = READING_SCOPE) ⇒ Object
Instance Method Summary collapse
-
#initialize(object:, context:) ⇒ Subscription
constructor
A new instance of Subscription.
-
#load_application_object_failed(err) ⇒ Object
If an argument is flagged with
loads:
and no object is found for it, remove this subscription (assuming that the object was deleted in the meantime, or that it became inaccessible). -
#resolve(**args) ⇒ Object
Implement the Resolve API.
-
#resolve_subscribe(args) ⇒ Object
Wrap the user-defined
#subscribe
hook. -
#resolve_update(args) ⇒ Object
Wrap the user-provided
#update
hook. -
#subscribe(args = {}) ⇒ Object
Default implementation returns the root object.
-
#unsubscribe ⇒ Object
Call this to halt execution and remove this subscription from the system.
-
#update(args = {}) ⇒ Object
The default implementation returns the root object.
Methods included from Member::HasFields
add_field, field, field_class, fields, get_field, global_id_field, own_fields
Methods included from Resolver::HasPayloadType
field_class, object_class, payload_type
Methods inherited from Resolver
argument, arguments_loads_as_type, #authorized?, complexity, extras, null, #ready?, resolve_method, #resolve_with_support, type, type_expr
Methods included from Member::HasPath
Methods included from Member::HasArguments
#add_argument, #argument, #argument_class, #argument_with_loads, #arguments, #own_arguments
Methods included from Member::BaseDSLMethods
#accessible?, #authorized?, #default_graphql_name, #description, #graphql_name, #introspection, #introspection?, #mutation, #name, #overridden_graphql_name, #to_graphql, #visible?
Constructor Details
#initialize(object:, context:) ⇒ Subscription
Returns a new instance of Subscription
32 33 34 35 36 |
# File 'lib/graphql/schema/subscription.rb', line 32 def initialize(object:, context:) super # Figure out whether this is an update or an initial subscription @mode = context.query.subscription_update? ? :update : :subscribe end |
Class Method Details
.field_options ⇒ Object
Overriding Resolver#field_options to include subscription_scope
112 113 114 115 116 |
# File 'lib/graphql/schema/subscription.rb', line 112 def self. super.merge( subscription_scope: subscription_scope ) end |
.subscription_scope(new_scope = READING_SCOPE) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/graphql/schema/subscription.rb', line 101 def self.subscription_scope(new_scope = READING_SCOPE) if new_scope != READING_SCOPE @subscription_scope = new_scope elsif defined?(@subscription_scope) @subscription_scope else find_inherited_method(:subscription_scope, nil) end end |
Instance Method Details
#load_application_object_failed(err) ⇒ Object
If an argument is flagged with loads:
and no object is found for it,
remove this subscription (assuming that the object was deleted in the meantime,
or that it became inaccessible).
84 85 86 87 88 89 |
# File 'lib/graphql/schema/subscription.rb', line 84 def load_application_object_failed(err) if @mode == :update unsubscribe end super end |
#resolve(**args) ⇒ Object
Implement the Resolve API
39 40 41 42 43 |
# File 'lib/graphql/schema/subscription.rb', line 39 def resolve(**args) # Dispatch based on `@mode`, which will raise a `NoMethodError` if we ever # have an unexpected `@mode` public_send("resolve_#{@mode}", args) end |
#resolve_subscribe(args) ⇒ Object
Wrap the user-defined #subscribe
hook
46 47 48 49 50 51 52 53 |
# File 'lib/graphql/schema/subscription.rb', line 46 def resolve_subscribe(args) ret_val = args.any? ? subscribe(args) : subscribe if ret_val == :no_response context.skip else ret_val end end |
#resolve_update(args) ⇒ Object
Wrap the user-provided #update
hook
65 66 67 68 69 70 71 72 |
# File 'lib/graphql/schema/subscription.rb', line 65 def resolve_update(args) ret_val = args.any? ? update(args) : update if ret_val == :no_update raise NoUpdateError else ret_val end end |
#subscribe(args = {}) ⇒ Object
Default implementation returns the root object.
Override it to return an object or
:no_response
to return nothing.
The default is :no_response
.
60 61 62 |
# File 'lib/graphql/schema/subscription.rb', line 60 def subscribe(args = {}) :no_response end |
#unsubscribe ⇒ Object
Call this to halt execution and remove this subscription from the system
92 93 94 |
# File 'lib/graphql/schema/subscription.rb', line 92 def unsubscribe raise UnsubscribedError end |
#update(args = {}) ⇒ Object
The default implementation returns the root object.
Override it to return :no_update
if you want to
skip updates sometimes. Or override it to return a different object.
77 78 79 |
# File 'lib/graphql/schema/subscription.rb', line 77 def update(args = {}) object end |