Class: GraphitiGql::LogSubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/graphiti_gql/log_subscriber.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogSubscriber

Returns a new instance of LogSubscriber.



19
20
21
# File 'lib/graphiti_gql/log_subscriber.rb', line 19

def initialize
  @chunks = {}
end

Class Method Details

.subscribe!(activerecord: false) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/graphiti_gql/log_subscriber.rb', line 4

def self.subscribe!(activerecord: false)
  instance = LogSubscriber.new
  instance.subscribe!('resolve', :on_data)
  instance.subscribe!('schema.before_execute', :on_schema_before_execute)
  instance.subscribe!('schema.execute', :on_schema_execute)
  instance.subscribe!('resource.all', :on_resource_all)
  instance.subscribe!('association', :on_association)
  instance.subscribe!('before_stats', :on_before_stats)
  instance.subscribe!('after_stats', :on_after_stats)
  if activerecord
    ActiveSupport::Notifications
      .subscribe("sql.active_record", instance.method(:on_activerecord))
  end
end

Instance Method Details

#on_activerecord(name, start, stop, id, payload) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/graphiti_gql/log_subscriber.rb', line 123

def on_activerecord(name, start, stop, id, payload)
  if @resolving || @stats
    sql = payload[:sql]
    unless sql.starts_with?('SHOW ')
      add_chunk("#{indent}#{sql}", :blue, true)
    end
  end
end

#on_after_stats(name, start, stop, id, payload) ⇒ Object



117
118
119
120
121
# File 'lib/graphiti_gql/log_subscriber.rb', line 117

def on_after_stats(name, start, stop, id, payload)
  @stats = false
  took = ((stop - start) * 1000.0).round(2)
  add_chunk("#{indent}🔢 Done! Took #{took}ms", :yellow, true)
end

#on_association(name, start, stop, id, payload) ⇒ Object



106
107
108
109
110
# File 'lib/graphiti_gql/log_subscriber.rb', line 106

def on_association(name, start, stop, id, payload)
  @last_association_path = thin_path
  sideload = payload[:sideload]
  add_chunk("#{indent}🔗 #{sideload.type} :#{sideload.name}", :white, true)
end

#on_before_stats(name, start, stop, id, payload) ⇒ Object



112
113
114
115
# File 'lib/graphiti_gql/log_subscriber.rb', line 112

def on_before_stats(name, start, stop, id, payload)
  @stats = true
  add_chunk("#{indent}🔢 Calculating Statistics...", :yellow, true)
end

#on_data(name, start, stop, id, payload) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/graphiti_gql/log_subscriber.rb', line 28

def on_data(name, start, stop, id, payload)
  @resolving = false
  if payload[:exception]
    @error_on_resolve = true
    return
  end

  num_results = payload[:results].length
  klasses = payload[:results].map(&:class).map(&:name).uniq
  color = num_results == 0 ? :yellow : :green
  stmt = "#{indent}   #{num_results} #{"result".pluralize(num_results)}"
  stmt << " of #{"type".pluralize(klasses.length)} #{klasses.to_sentence}" if num_results > 0
  add_chunk(stmt, color, true)

  took = ((stop - start) * 1000.0).round(2)
  add_chunk("#{indent}   Took: #{took}ms", :magenta, true)
end

#on_resource_all(name, start, stop, id, payload) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/graphiti_gql/log_subscriber.rb', line 96

def on_resource_all(name, start, stop, id, payload)
  @resolving = true
  params = payload[:params].inspect
  resource = payload[:resource].name
  if thin_path.length == 1
    add_chunk("Query.#{thin_path.first}", :yellow, true)
  end
  add_chunk("#{indent}\\_ #{resource}.all(#{params})", :cyan, true)
end

#on_schema_before_execute(name, start, stop, id, payload) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/graphiti_gql/log_subscriber.rb', line 46

def on_schema_before_execute(name, start, stop, id, payload)
  Graphiti.debug(payload[:query].strip_heredoc, :white, true)
  unless payload[:variables].empty?
    Graphiti.debug("✨ Variables: #{payload[:variables].inspect}", :yellow, true)
  end
  unless payload[:context].empty?
    Graphiti.debug("✨ Context: #{payload[:context].inspect}", :blue, true)
  end
  Graphiti.debug(%|💡 Debug tip! Override Resource#resolve:

class YourResource < ApplicationResource
  # ... code ...
  def resolve(scope)
debugger
# if activerecord, call scope.to_sql/scope.to_a
super
  end
end|, :white, true)
  Graphiti.debug("🤠🚀🤠🚀🤠🚀🤠🚀🤠🚀🤠🚀🤠🚀🤠 Executing! 🤠🚀🤠🚀🤠🚀🤠🚀🤠🚀🤠🚀🤠🚀🤠", :white, true)
end

#on_schema_execute(name, start, stop, id, payload) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/graphiti_gql/log_subscriber.rb', line 67

def on_schema_execute(name, start, stop, id, payload)
  if payload[:exception] || (response_errors = payload[:result]["errors"])
    indent = indent(path: @last_association_path)
    add_chunk("#{indent}❌🚨❌🚨❌🚨❌ ERROR! ❌🚨❌🚨❌🚨❌", :red, true, path: @last_association_path)
    if @error_on_resolve
      add_chunk("#{indent}This error occurred while executing the above query, so it's likely not caused by Graphiti itself. Maybe bad SQL? Try running again and putting a debugger in this Resource's #resolve, or try to run the query independent of Graphiti/GraphQL.",
        :red, true, path: @last_association_path)
    end
    flush_chunks(@chunks)
    if response_errors
      Graphiti.info("❌🚨 Response contained errors!", :red, true)
      response_errors.each do |err|
        if err['extensions']
          Graphiti.info("#{err['extensions']['code']} - #{err['message']}", :red, true)
        else
          Graphiti.info(err['message'], :red, true)
        end
        if err['path']
          Graphiti.info("#{err['path'].join(".")}", :red, false) if err['path']
        end
      end
    end
  else
    flush_chunks(@chunks)
    took = ((stop - start) * 1000.0).round(2)
    Graphiti.info("✅ Completed successfully in #{took}ms", :magenta, true)
  end
end

#subscribe!(name, method_name) ⇒ Object



23
24
25
26
# File 'lib/graphiti_gql/log_subscriber.rb', line 23

def subscribe!(name, method_name)
  ActiveSupport::Notifications
    .subscribe("#{name}.graphiti", method(method_name))
end