class Graphql::LogHelper

Constants

VERSION

Public Class Methods

log_details(request_params) click to toggle source
# File lib/graphql/log_helper.rb, line 9
def log_details(request_params)
  selections = parse_query(request_params[:query])&.definitions&.first&.selections
  return {} if selections.blank?

  query_params =
    if request_params[:variables].present?
      query_params_given_variables(selections, request_params[:variables])
    else
      query_params_given_no_variables(selections)
    end

  {
    params: to_deep_a(query_params),
    resolvers: resolver_names(selections)
  }
end

Private Class Methods

parse_query(query) click to toggle source
# File lib/graphql/log_helper.rb, line 58
def parse_query(query)
  return nil if query.blank?

  GraphQL.parse(query)
rescue GraphQL::ParseError
  nil
end
query_params_given_no_variables(selections) click to toggle source
# File lib/graphql/log_helper.rb, line 37
def query_params_given_no_variables(selections)
  selections.map { |selection| selection_to_array(selection) }
end
query_params_given_variables(selections, variables) click to toggle source
# File lib/graphql/log_helper.rb, line 28
def query_params_given_variables(selections, variables)
  selections.map do |selection|
    [
      selection.name,
      to_deep_a(variables)
    ]
  end
end
resolver_names(selections) click to toggle source
# File lib/graphql/log_helper.rb, line 41
def resolver_names(selections)
  selections.map(&:name)
end
selection_to_array(selection) click to toggle source
# File lib/graphql/log_helper.rb, line 45
def selection_to_array(selection)
  [
    selection.name,
    if !selection.respond_to?(:value)
      selection.arguments.map { |arg| selection_to_array(arg) }
    elsif selection.value.respond_to?(:arguments)
      selection.value.arguments.map { |arg| selection_to_array(arg) }
    else
      selection.value
    end
  ]
end
to_deep_a(hash) click to toggle source
# File lib/graphql/log_helper.rb, line 66
def to_deep_a(hash)
  hash.map { |key, value| [key, value.is_a?(Hash) ? to_deep_a(value) : value] }
end