Class: Rails::GraphQL::Field::SubscriptionField

Inherits:
OutputField show all
Defined in:
lib/rails/graphql/field/subscription_field.rb

Overview

GraphQL Subscription Field

This is an extension of a normal output field, which will sign the request for updates of the field when the scope and arguments are the same.

Defined Under Namespace

Modules: Proxied

Constant Summary

Constants included from Helpers::WithCallbacks

Helpers::WithCallbacks::DEFAULT_EVENT_TYPES

Instance Attribute Summary

Attributes included from TypedField

#type

Attributes inherited from Rails::GraphQL::Field

#gql_name, #name, #owner

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OutputField

#=~, #all_events, #all_listeners, #apply_changes, #broadcastable?, #entry_point?, #events?, #listeners?, #valid_output?, #validate!, #validate_output!

Methods included from TypedField

#=~, #all_events, #all_listeners, #as_json, #deserialize, #events?, #initialize_copy, #listeners?, #of_type?, #to_json, #type_klass, #valid_field_types, #validate!

Methods included from ResolvedField

#dynamic_resolver?, #resolve, #resolver, #validate!

Methods included from AuthorizedField

#authorizable?, #authorize, #authorizer

Methods included from Helpers::WithGlobalID

#to_gid_param, #to_global_id

Methods included from Helpers::WithCallbacks

extended, #on

Methods included from Helpers::WithEvents

extended, #on

Methods included from Helpers::WithArguments

#=~, #argument, extended, #has_argument?, #has_arguments?, #id_argument, #initialize_copy, #ref_argument, #validate!

Methods inherited from Rails::GraphQL::Field

#=~, #all_owners, #apply_changes, #array?, #as_json, #configure, #description, #deserialize, #disable!, #disabled?, #enable!, #enabled?, #initialize_copy, input_type?, #inspect, #internal?, leaf_type?, #method_name, mutation?, #null?, #nullable?, output_type?, proxy?, proxyable_methods, #required!, #required?, #required_items!, subscription?, #to_json, #to_proxy, #valid?, #valid_input?, #valid_output?, #validate!

Methods included from Helpers::WithDescription

#desc, #description, #description=, #description?

Methods included from Helpers::WithDirectives

#all_directive_events, #all_directive_listeners, #directive_events?, #directive_listeners?, extended, #initialize_copy, #use, #using?, #validate!

Constructor Details

#initialize(*args, scope: nil, **xargs, &block) ⇒ SubscriptionField

Intercept the initializer to maybe set the scope



26
27
28
29
# File 'lib/rails/graphql/field/subscription_field.rb', line 26

def initialize(*args, scope: nil, **xargs, &block)
  @scope = Array.wrap(scope).freeze unless scope.nil?
  super(*args, **xargs, &block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rails::GraphQL::Field

Class Method Details

.included(other) ⇒ Object

Just add the callbacks setup to the field



21
22
23
# File 'lib/rails/graphql/field/subscription_field.rb', line 21

def self.included(other)
  other.send(:expose_events!, :subscribed)
end

Instance Method Details

#full_scopeObject

Get the full scope of the field



43
44
45
# File 'lib/rails/graphql/field/subscription_field.rb', line 43

def full_scope
  defined?(@scope) ? @scope : EMPTY_ARRAY
end

#schema_typeObject

Change the schema type of the field



32
33
34
# File 'lib/rails/graphql/field/subscription_field.rb', line 32

def schema_type
  :subscription
end

#scope(*parts) ⇒ Object

Set the parts of the scope of the subscription



37
38
39
40
# File 'lib/rails/graphql/field/subscription_field.rb', line 37

def scope(*parts)
  (defined?(@scope) ? (@scope += parts) : (@scope = parts)).freeze
  self
end

#trigger(**xargs) ⇒ Object

Trigger an update to the subscription



67
68
69
70
# File 'lib/rails/graphql/field/subscription_field.rb', line 67

def trigger(**xargs)
  provider = owner.subscription_provider
  provider.search_and_update(field: self, **xargs)
end

#trigger_for(object, and_prepare: true, **xargs) ⇒ Object

A shortcut for trigger when everything is related to the provided object



48
49
50
51
52
53
54
55
56
57
# File 'lib/rails/graphql/field/subscription_field.rb', line 48

def trigger_for(object, and_prepare: true, **xargs)
  xargs[:args] ||= extract_args_from(object)

  if and_prepare
    xargs[:data_for] ||= {}
    xargs[:data_for][+"subscription.#{gql_name}"] = object
  end

  trigger(**xargs)
end

#unsubscribe(**xargs) ⇒ Object

Force matching subscriptions to be removed



73
74
75
76
# File 'lib/rails/graphql/field/subscription_field.rb', line 73

def unsubscribe(**xargs)
  provider = owner.subscription_provider
  provider.search_and_remove(field: self, **xargs)
end

#unsubscribe_from(object, **xargs) ⇒ Object

A shortcut for unsubscribe when everything is related to the provided object



61
62
63
64
# File 'lib/rails/graphql/field/subscription_field.rb', line 61

def unsubscribe_from(object, **xargs)
  xargs[:args] ||= extract_args_from(object)
  unsubscribe(**xargs)
end