Class: Rails::GraphQL::Request::Component::Typename

Inherits:
Rails::GraphQL::Request::Component show all
Includes:
Directives, ValueWriters
Defined in:
lib/rails/graphql/request/component/typename.rb

Overview

GraphQL Request Component Typename

Extra component which simulates a field that its only purpose is to return the name of the parent GraphQL object

Constant Summary

Constants included from ValueWriters

ValueWriters::KIND_WRITERS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Directives

#cache_dump, #directive_events, #directive_listeners, #using?

Methods included from ValueWriters

#format_array_exception, #write_array, #write_array!

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

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

Methods included from Resolvable

#resolve!

Methods included from Preparable

#prepared_data!, #prepared_data?

Methods included from Organizable

#cache_dump, #organize!

Constructor Details

#initialize(parent, node) ⇒ Typename

Returns a new instance of Typename.



24
25
26
27
28
29
30
31
# File 'lib/rails/graphql/request/component/typename.rb', line 24

def initialize(parent, node)
  @parent = parent

  @name = node[0]
  @alias_name = node[1]

  super(node)
end

Instance Attribute Details

#alias_nameObject (readonly)

Returns the value of attribute alias_name.



17
18
19
# File 'lib/rails/graphql/request/component/typename.rb', line 17

def alias_name
  @alias_name
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/rails/graphql/request/component/typename.rb', line 17

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



17
18
19
# File 'lib/rails/graphql/request/component/typename.rb', line 17

def parent
  @parent
end

Class Method Details

.kindObject

Rewrite the kind to always return :field



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

def self.kind
  :field
end

Instance Method Details

#broadcastable?Boolean

Typename is always broadcastable

Returns:

  • (Boolean)


52
53
54
# File 'lib/rails/graphql/request/component/typename.rb', line 52

def broadcastable?
  true
end

#cache_load(data) ⇒ Object

Organize from cache data



61
62
63
64
65
66
# File 'lib/rails/graphql/request/component/typename.rb', line 61

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

  super
end

#gql_nameObject

Return the name of the field to be used on the response



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

def gql_name
  alias_name || name
end

#prepare!Object

Prepare is not necessary for this field



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

def prepare!
end

#resolve_with!(object) ⇒ Object

Set the value that the field will be resolved as



34
35
36
37
38
39
# File 'lib/rails/graphql/request/component/typename.rb', line 34

def resolve_with!(object)
  @typename = object.gql_name
  resolve!
ensure
  @typename = nil
end

#write_value(value) ⇒ Object

Write the typename information



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

def write_value(value)
  response.serialize(Type::Scalar::StringScalar, gql_name, value.itself)
end