Class: Ralphql::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/ralphql/node.rb

Overview

A Node represents a query type object in GraphQL. It has name, attributes, arguments and other associated nodes. It can be paginated, which includes rails GraphQL default pagination attributes and the edges, nodes schema

Constant Summary collapse

PAGINATION_ATTS =
%i[endCursor startCursor hasPreviousPage hasNextPage].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ Node

Returns a new instance of Node.



14
15
16
17
18
19
20
21
22
23
# File 'lib/ralphql/node.rb', line 14

def initialize(name, opts = {})
  @name = name
  @args = opts[:args] || {}
  @atts = [opts[:atts]].flatten.compact
  @nodes = [opts[:nodes]].flatten.compact
  @parent_node = opts[:parent_node]
  @paginated = opts[:paginated] || false

  @nodes.each { |node| node.parent_node = self }
end

Instance Attribute Details

#parent_nodeObject

Returns the value of attribute parent_node.



10
11
12
# File 'lib/ralphql/node.rb', line 10

def parent_node
  @parent_node
end

Instance Method Details

#add(obj) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/ralphql/node.rb', line 45

def add(obj)
  case obj
  when Symbol then @atts << obj.to_s
  when String then @atts << obj
  when Array then @atts += obj.map(&:to_s)
  when Node then obj.parent_node = self && @nodes << obj
  else raise AttributeNotSupportedError
  end
  self
end

#add_node(name, args: {}, atts: [], nodes: [], paginated: false) ⇒ Object



56
57
58
59
60
# File 'lib/ralphql/node.rb', line 56

def add_node(name, args: {}, atts: [], nodes: [], paginated: false)
  node = self.class.new(name, args: args, atts: atts, nodes: nodes, paginated: paginated)
  add(node)
  node
end

#empty_body?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/ralphql/node.rb', line 62

def empty_body?
  @nodes.empty? && @atts.empty?
end

#queryObject

Raises:



25
26
27
28
29
30
31
32
33
34
# File 'lib/ralphql/node.rb', line 25

def query
  raise EmptyNodeError if empty_body? && @args.empty?

  args = resolve_args
  body = resolve_body

  result = camelize(@name) + args + body
  result = "{#{result}}" if @parent_node.nil?
  result
end

#replace(opts) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/ralphql/node.rb', line 36

def replace(opts)
  @name = opts[:name] if opts[:name]
  @args = opts[:args] if opts[:args]
  @atts = [opts[:atts]].flatten if opts[:atts]
  @nodes = [opts[:nodes]].flatten if opts[:nodes]
  @parent_node = opts[:parent_node] if opts[:parent_node]
  @paginated = opts[:paginated] if opts[:paginated]
end