class Ralphql::Node

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

Constants

PAGINATION_ATTS

Attributes

parent_node[RW]

Public Class Methods

new(name, opts = {}) click to toggle source
# 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

Public Instance Methods

add(obj) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/ralphql/node.rb, line 62
def empty_body?
  @nodes.empty? && @atts.empty?
end
query() click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

camelize(term) click to toggle source
# File lib/ralphql/node.rb, line 90
def camelize(term)
  term.to_s.gsub(%r{(?:_|(/))([a-z\d]*)}i) { "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" }
end
resolve_args() click to toggle source
# File lib/ralphql/node.rb, line 68
def resolve_args
  args = @args.map { |arg, value| "#{camelize(arg)}:#{value.to_ralphql}" }
  args = args.to_a.join(',')
  args = "(#{args})" if args.size > 1
  args
end
resolve_body() click to toggle source
# File lib/ralphql/node.rb, line 75
def resolve_body
  body = @atts.map { |att| camelize(att) }.join(' ')
  body += ' ' if @atts.any? && @nodes.any?
  body += @nodes.map(&:query).join(' ')
  body = resolve_pagination(body)

  empty_body? ? body : "{#{body}}"
end
resolve_pagination(body) click to toggle source
# File lib/ralphql/node.rb, line 84
def resolve_pagination(body)
  return body unless @paginated

  "pageInfo{#{PAGINATION_ATTS.join(' ')}}edges{cursor node{#{body}}}"
end