Class: Rails::GraphQL::LogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/rails/graphql/railties/log_subscriber.rb

Overview

GraphQL Log Subscriber

This is the log tracker that works the same way as ActiveRecord when it has to report on logs that a query was performed

Constant Summary collapse

REMOVE_COMMENTS =
/#(?=(?:[^"]*"[^"]*")*[^"]*$).*/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.runtimeObject



16
17
18
# File 'lib/rails/graphql/railties/log_subscriber.rb', line 16

def self.runtime
  RuntimeRegistry.gql_runtime ||= 0
end

.runtime=(value) ⇒ Object



20
21
22
# File 'lib/rails/graphql/railties/log_subscriber.rb', line 20

def self.runtime=(value)
  RuntimeRegistry.gql_runtime = value
end

Instance Method Details

#compile(event) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rails/graphql/railties/log_subscriber.rb', line 38

def compile(event)
  return unless logger.debug?

  payload = event.payload
  doc = payload[:document].gsub(REMOVE_COMMENTS, '').squish

  helper = ActiveSupport::NumberHelper::NumberToHumanSizeConverter
  total = helper.convert(payload[:total], EMPTY_HASH)

  debug(+"#{header(event, 'Compile')} #{total}  #{doc}")
end

#request(event) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rails/graphql/railties/log_subscriber.rb', line 24

def request(event)
  self.class.runtime += event.duration
  return unless logger.debug?

  payload = event.payload
  cached = '[CACHE]' if payload[:cached]
  doc = payload[:document]&.gsub(REMOVE_COMMENTS, '')&.squish

  desc = +"#{header(event, cached)}  #{doc}"
  desc << debug_variables(payload[:variables]) unless payload[:variables].blank?

  debug(desc)
end

#subscription(event) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rails/graphql/railties/log_subscriber.rb', line 60

def subscription(event)
  return unless logger.info?

  item, type, provider = event.payload.values_at(:item, :type, :provider)
  provider = provider.class.name.sub(/\ARails::GraphQL::Subscription::/, '')
  duration = event.duration.round(1)

  desc = +"#{header(event, provider)} Subscription #{type}"

  unless item.nil?
    hex = { added: GREEN, removed: RED, updated: BLUE }[type]

    if type == :updated
      desc << +": #{color(item.sid, hex)}"
    else
      desc << +": [#{color(item.sid, hex)}] #{item.schema}.#{item.field.gql_name}"
      desc << +" [#{(item.scope === null_subscription_scope ? nil : item.scope).inspect}"
      desc << +", #{item.args.as_json.inspect}]"
    end
  end

  info(desc)
end

#validate(event) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/rails/graphql/railties/log_subscriber.rb', line 50

def validate(event)
  return unless logger.debug?

  payload = event.payload
  doc = payload[:document].gsub(REMOVE_COMMENTS, '').squish
  valid = payload[:result] ? color('YES', GREEN) : color('NO', RED)

  debug(+"#{header(event, 'Valid?')} #{valid}  #{doc}")
end