Class: Rails::GraphQL::Request::Event

Inherits:
Event
  • Object
show all
Defined in:
lib/rails/graphql/request/event.rb

Overview

GraphQL Request Event

A small extension of the original event to support extra methods and helpers when performing events during a request

Constant Summary collapse

OBJECT_BASED_READERS =
%i[fragment spread].freeze

Constants inherited from Event

Event::TRIGGER_TYPES

Instance Attribute Summary collapse

Attributes inherited from Event

#data, #event_name, #last_result, #object, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Event

#call_next, #parameter, #parameter?, #set_on, #stop, #trigger, #trigger_all, #trigger_object

Constructor Details

#initialize(name, strategy, source = nil, **data) ⇒ Event

Returns a new instance of Event.



27
28
29
30
31
32
33
34
35
# File 'lib/rails/graphql/request/event.rb', line 27

def initialize(name, strategy, source = nil, **data)
  @request = strategy.request
  @strategy = strategy

  source ||= request.stack.first
  @index, source = source, request.stack[1] if source.is_a?(Numeric)

  super(name, source, **data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, **xargs, &block) ⇒ Object (private)

If the method_name matches the kind of the current object, then it will return the object



115
116
117
118
119
120
121
122
123
# File 'lib/rails/graphql/request/event.rb', line 115

def method_missing(method_name, *args, **xargs, &block)
  if OBJECT_BASED_READERS.include?(method_name)
    object if object.kind == method_name
  elsif current_value&.respond_to?(method_name)
    current_value&.public_send(method_name, *args, **xargs, &block)
  else
    super
  end
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



18
19
20
# File 'lib/rails/graphql/request/event.rb', line 18

def index
  @index
end

#requestObject (readonly)

Returns the value of attribute request.



18
19
20
# File 'lib/rails/graphql/request/event.rb', line 18

def request
  @request
end

#strategyObject (readonly)

Returns the value of attribute strategy.



18
19
20
# File 'lib/rails/graphql/request/event.rb', line 18

def strategy
  @strategy
end

Class Method Details

.trigger(event_name, object, *args, **xargs, &block) ⇒ Object

Enhance the trigger settings based on the default for a request event



21
22
23
24
25
# File 'lib/rails/graphql/request/event.rb', line 21

def self.trigger(event_name, object, *args, **xargs, &block)
  xargs[:phase] ||= :execution
  xargs[:fallback_trigger!] ||= :trigger_all unless block.present?
  super(event_name, object, *args, **xargs, &block)
end

Instance Method Details

#argument(name) ⇒ Object Also known as: arg

Provide access to field arguments



84
85
86
# File 'lib/rails/graphql/request/event.rb', line 84

def argument(name)
  args_source.try(:[], name.to_sym)
end

#current_valueObject Also known as: current

Provide a way to access the current field value TODO: Maybe change this to current to get the value by reference



47
48
49
# File 'lib/rails/graphql/request/event.rb', line 47

def current_value
  resolver&.current_value
end

#current_value=(value) ⇒ Object

Provide a way to set the current value



54
55
56
# File 'lib/rails/graphql/request/event.rb', line 54

def current_value=(value)
  resolver&.override_value(value)
end

#fieldObject

Return the actual field when the source is a request field



69
70
71
# File 'lib/rails/graphql/request/event.rb', line 69

def field
  source.field if source.try(:kind) == :field
end

#for?(type) ⇒ Boolean

Check if the event source is of the given type

Returns:

  • (Boolean)


74
75
76
# File 'lib/rails/graphql/request/event.rb', line 74

def for?(type)
  source.of_type?(type)
end

#on?(item) ⇒ Boolean

Check if the current object is of the given item

Returns:

  • (Boolean)


79
80
81
# File 'lib/rails/graphql/request/event.rb', line 79

def on?(item)
  object.of_type?(item)
end

#on_instance(object, &block) ⇒ Object

A combined helper for instance_for and set_on



91
92
93
# File 'lib/rails/graphql/request/event.rb', line 91

def on_instance(object, &block)
  set_on(object.is_a?(Class) ? instance_for(object) : object, &block)
end

#operationObject

Get the operation for the current source



59
60
61
# File 'lib/rails/graphql/request/event.rb', line 59

def operation
  (object.kind == :operation) ? object : source.operation
end

#resolverObject

Return the strategy context as the resolver



64
65
66
# File 'lib/rails/graphql/request/event.rb', line 64

def resolver
  strategy.context
end

#same_source?(other) ⇒ Boolean

If the source is a field, than also compare to the actual field

Returns:

  • (Boolean)


41
42
43
# File 'lib/rails/graphql/request/event.rb', line 41

def same_source?(other)
  super || (source.try(:kind) == :field && source.field == other)
end