Class: GraphQL::RailsLogger::Subscriber

Inherits:
ActionController::LogSubscriber
  • Object
show all
Defined in:
lib/graphql/rails_logger/subscriber.rb

Instance Method Summary collapse

Instance Method Details

#start_processing(event) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/graphql/rails_logger/subscriber.rb', line 21

def start_processing(event)
  return unless logger.info?

  payload = event.payload
  params  = payload[:params].except(*INTERNAL_PARAMS)
  format  = payload[:format]
  format  = format.to_s.upcase if format.is_a?(Symbol)

  config = GraphQL::RailsLogger.configuration

  info "Processing by #{payload[:controller]}##{payload[:action]} as #{format}"

  if config.white_list.fetch(payload[:controller], []).include?(payload[:action])
    formatter = Rouge::Formatters::Terminal256.new(config.theme)
    query_lexer = Rouge::Lexers::GraphQL.new
    variables_lexer = Rouge::Lexers::Ruby.new

    (params['_json'] || [params.slice('query', 'variables', 'extensions')]).each do |data|

      next if config.skip_introspection_query && data['query']&.index(/query IntrospectionQuery/)

      # Cleanup and indent params for logging
      query = indent(data.fetch('query', ''))
      variables = indent(pretty(data.fetch('variables', '')))
      extensions = indent(pretty(data.fetch('extensions', '')))

      info "\nVariables:\n#{formatter.format(variables_lexer.lex(variables))}" if variables.present?
      info "\nQuery:\n#{formatter.format(query_lexer.lex(query))}" if query.present?
      info "\nExtensions:\n#{formatter.format(variables_lexer.lex(extensions))}" if extensions.present?
    end
  else
    info "\nParameters:\n#{params.inspect}" unless params.empty?
  end
end