Class: Rails::GraphQL::Request::Component::Operation

Inherits:
Rails::GraphQL::Request::Component show all
Extended by:
ActiveSupport::Autoload
Includes:
Directives, SelectionSet
Defined in:
lib/rails/graphql/request/component/operation.rb

Overview

GraphQL Request Component Operation

This class holds information about a given operation. This will guide the validation and execution of it.

Direct Known Subclasses

Subscription

Defined Under Namespace

Classes: Subscription

Constant Summary collapse

Query =

Query is default behavior, so it doesn’t need a whole class

Class.new(self) { redefine_singleton_method(:query?)    { true } }
Mutation =
Class.new(self) { redefine_singleton_method(:mutation?) { true } }

Instance Attribute Summary collapse

Attributes included from SelectionSet

#selection

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Directives

#directive_events, #directive_listeners, #using?

Methods inherited from Rails::GraphQL::Request::Component

#assignable?, #invalid?, #invalidate!, #skip!, #skipped?, #unresolvable?

Methods included from Resolvable

#resolve!

Methods included from Preparable

#prepare!, #prepared_data!, #prepared_data?

Methods included from Organizable

#organize!

Constructor Details

#initialize(request, node) ⇒ Operation

Returns a new instance of Operation.



63
64
65
66
67
68
69
70
# File 'lib/rails/graphql/request/component/operation.rb', line 63

def initialize(request, node)
  @name = node[1]
  @request = request

  super(node)

  check_invalid_operation!
end

Instance Attribute Details

#argumentsObject (readonly) Also known as: all_arguments

Returns the value of attribute arguments.



56
57
58
# File 'lib/rails/graphql/request/component/operation.rb', line 56

def arguments
  @arguments
end

#nameObject (readonly) Also known as: gql_name

Returns the value of attribute name.



56
57
58
# File 'lib/rails/graphql/request/component/operation.rb', line 56

def name
  @name
end

#requestObject (readonly)

Returns the value of attribute request.



56
57
58
# File 'lib/rails/graphql/request/component/operation.rb', line 56

def request
  @request
end

#variablesObject (readonly) Also known as: vars

Returns the value of attribute variables.



56
57
58
# File 'lib/rails/graphql/request/component/operation.rb', line 56

def variables
  @variables
end

Class Method Details

.build(request, node) ⇒ Object

Helper method to initialize an operation given the node



20
21
22
23
24
# File 'lib/rails/graphql/request/component/operation.rb', line 20

def build(request, node)
  request.build(const_get(node.type.to_s.classify, false), request, node)
rescue ::NameError
  raise NameError, +%[Unable to initialize "#{node.type}" operation.]
end

.kindObject

Rewrite the kind to always return :operation



27
28
29
# File 'lib/rails/graphql/request/component/operation.rb', line 27

def kind
  :operation
end

.mutation?Boolean

Defines if the current operation is a mutation type

Returns:

  • (Boolean)


37
38
39
# File 'lib/rails/graphql/request/component/operation.rb', line 37

def mutation?
  false
end

.query?Boolean

Defines if the current operation is a query type

Returns:

  • (Boolean)


32
33
34
# File 'lib/rails/graphql/request/component/operation.rb', line 32

def query?
  false
end

.subscription?Boolean

Defines if the current operation is a subscription type

Returns:

  • (Boolean)


42
43
44
# File 'lib/rails/graphql/request/component/operation.rb', line 42

def subscription?
  false
end

.typeObject



17
# File 'lib/rails/graphql/request/component/operation.rb', line 17

alias type kind

Instance Method Details

#cache_dumpObject

Build the cache object



126
127
128
# File 'lib/rails/graphql/request/component/operation.rb', line 126

def cache_dump
  super.merge(type: self.class)
end

#cache_load(data) ⇒ Object

Organize from cache data



131
132
133
134
135
# File 'lib/rails/graphql/request/component/operation.rb', line 131

def cache_load(data)
  @name = data[:node][1]

  super
end

#fields_sourceObject

The list of fields comes from the fields_for of the same type as the type of the operation



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

def fields_source
  schema.fields_for(type)
end

#hashObject

The hash of operations must take into consideration the used fragments



117
118
119
120
121
122
123
# File 'lib/rails/graphql/request/component/operation.rb', line 117

def hash
  return super unless defined?(@used_fragments)

  super ^ used_fragments.reduce(0) do |value, fragment|
    value ^ request.fragments[fragment].hash
  end
end

#log_sourceObject

A fast way to access the correct display name for log or errors



112
113
114
# File 'lib/rails/graphql/request/component/operation.rb', line 112

def log_source
  @log_source ||= name.blank? ? type : +"#{name} #{type}"
end

#memoObject

Support memory object to save information across the iteration



92
93
94
# File 'lib/rails/graphql/request/component/operation.rb', line 92

def memo
  @memo ||= OpenStruct.new
end

#resolve_invalidObject

Add a empty entry if the operation has a name



97
98
99
# File 'lib/rails/graphql/request/component/operation.rb', line 97

def resolve_invalid
  response.safe_add(name, nil) if stacked_selection?
end

#type_klassObject

Allow accessing the fake type form the schema. It’s used for inline spreads without a specified type



80
81
82
83
# File 'lib/rails/graphql/request/component/operation.rb', line 80

def type_klass
  return @type_klass if defined?(@type_klass)
  @type_klass = schema.public_send("#{type}_type")
end

#typenameObject

The typename is always based on the fake name used for the set of schema fields



87
88
89
# File 'lib/rails/graphql/request/component/operation.rb', line 87

def typename
  schema.type_name_for(type)
end

#used_fragmentsObject

Stores all the used fragments



107
108
109
# File 'lib/rails/graphql/request/component/operation.rb', line 107

def used_fragments
  @used_fragments ||= Set.new
end

#used_variablesObject

Stores all the used variables to report not used ones



102
103
104
# File 'lib/rails/graphql/request/component/operation.rb', line 102

def used_variables
  @used_variables ||= Set.new
end