Class: Graphql::Generators::DetailedTraceGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/graphql/detailed_trace_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object



16
17
18
# File 'lib/generators/graphql/detailed_trace_generator.rb', line 16

def self.next_migration_number(dirname)
  ::ActiveRecord::Generators::Base.next_migration_number(dirname)
end

Instance Method Details

#install_detailed_tracesObject



20
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/generators/graphql/detailed_trace_generator.rb', line 20

def install_detailed_traces

  schema_glob = File.expand_path("app/graphql/*_schema.rb", destination_root)
  schema_file = Dir.glob(schema_glob).first
  if !schema_file
    raise ArgumentError, "Failed to find schema definition file (checked: #{schema_glob.inspect})"
  end
  schema_file_match = /( *)class ([A-Za-z:]+) < GraphQL::Schema/.match(File.read(schema_file))
  schema_name = schema_file_match[2]
  indent = schema_file_match[1] + "  "

  if !options.redis?
    migration_template 'create_graphql_detailed_traces.erb', 'db/migrate/create_graphql_detailed_traces.rb'
  end

  log :add_detailed_traces_plugin
  sentinel = /< GraphQL::Schema\s*\n/m
  code = <<-RUBY
#{indent}use GraphQL::Tracing::DetailedTrace#{options.redis? ? ", redis: raise(\"TODO: pass a connection to a persistent redis database\")" : ""}, limit: 50

#{indent}# When this returns true, DetailedTrace will trace the query
#{indent}# Could use `query.context`, `query.selected_operation_name`, `query.query_string` here
#{indent}# Could call out to Flipper, etc
#{indent}def self.detailed_trace?(query)
#{indent}  rand <= 0.000_1 # one in ten thousand
#{indent}end

  RUBY

  in_root do
    inject_into_file schema_file, code, after: sentinel, force: false
  end

  routes_source = File.read(File.expand_path("config/routes.rb", destination_root))
  already_has_dashboard = routes_source.include?("GraphQL::Dashboard") ||
    routes_source.include?("Schema.dashboard") ||
    routes_source.include?("GraphQL::Pro::Routes::Lazy")

  if (!already_has_dashboard || behavior == :revoke)
    log :route, "GraphQL::Dashboard"
    shell.mute do
      route <<~RUBY
      # TODO: add authorization to this route and expose it in production
      # See https://graphql-ruby.org/pro/dashboard.html#authorizing-the-dashboard
      if Rails.env.development?
        mount GraphQL::Dashboard, at: "/graphql/dashboard", schema: #{schema_name.inspect}
      end

      RUBY
    end

    gem("google-protobuf")

  end
end