class Graphiti::HashRenderer

Public Class Methods

new(resource, graphql: false) click to toggle source
# File lib/graphiti/hash_renderer.rb, line 134
def initialize(resource, graphql: false)
  @resource = resource
  @graphql = graphql
end

Public Instance Methods

render(options) click to toggle source
# File lib/graphiti/hash_renderer.rb, line 139
def render(options)
  serializers = options[:data]
  opts = options.slice(:fields, :include)
  opts[:graphql] = @graphql
  top_level_key = get_top_level_key(@resource, serializers.is_a?(Array))

  hash = {top_level_key => {}}
  nodes = get_nodes(serializers, opts)
  add_nodes(hash, top_level_key, options, nodes, @graphql)
  add_stats(hash, top_level_key, options, @graphql)
  if @graphql
    add_page_info(hash, serializers, top_level_key, options)
  end

  hash
end

Private Instance Methods

add_nodes(hash, top_level_key, opts, nodes, graphql) click to toggle source
# File lib/graphiti/hash_renderer.rb, line 179
def add_nodes(hash, top_level_key, opts, nodes, graphql)
  payload = nodes
  if graphql && nodes.is_a?(Array)
    payload = {nodes: nodes}
  end

  # Don't render nodes if we only requested stats
  unless graphql && opts[:fields].values == [[:stats]]
    hash[top_level_key] = payload
  end
end
add_page_info(hash, serializers, top_level_key, options) click to toggle source

NB - this is only for top-level right now The casing here is GQL-specific, we can update later if needed.

# File lib/graphiti/hash_renderer.rb, line 209
def add_page_info(hash, serializers, top_level_key, options)
  if (fields = options[:fields].try(:[], :page_info))
    info = {}

    if fields.include?(:has_next_page)
      info[:hasNextPage] = options[:proxy].pagination.has_next_page?
    end

    if fields.include?(:has_previous_page)
      info[:hasPreviousPage] = options[:proxy].pagination.has_previous_page?
    end

    if fields.include?(:start_cursor)
      info[:startCursor] = serializers.first.try(:cursor)
    end

    if fields.include?(:end_cursor)
      info[:endCursor] = serializers.last.try(:cursor)
    end

    hash[top_level_key][:pageInfo] = info
  end
end
add_stats(hash, top_level_key, options, graphql) click to toggle source
# File lib/graphiti/hash_renderer.rb, line 191
def add_stats(hash, top_level_key, options, graphql)
  if options[:meta] && !options[:meta].empty?
    if @graphql
      if (stats = options[:meta][:stats])
        camelized = {}
        stats.each_pair do |key, value|
          camelized[key.to_s.camelize(:lower).to_sym] = value
        end
        hash[top_level_key][:stats] = camelized
      end
    else
      hash.merge!(options.slice(:meta))
    end
  end
end
get_nodes(serializers, opts) click to toggle source
# File lib/graphiti/hash_renderer.rb, line 169
def get_nodes(serializers, opts)
  if serializers.is_a?(Array)
    serializers.each_with_index.map do |s, index|
      s.to_hash(**opts)
    end
  else
    serializers.to_hash(**opts)
  end
end
get_top_level_key(resource, is_many) click to toggle source
# File lib/graphiti/hash_renderer.rb, line 158
def get_top_level_key(resource, is_many)
  key = :data

  if @graphql
    key = @resource.graphql_entrypoint
    key = key.to_s.singularize.to_sym unless is_many
  end

  key
end