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 = "\#{indent}use GraphQL::Tracing::DetailedTrace\#{options.redis? ? \", redis: raise(\\\"TODO: pass a connection to a persistent redis database\\\")\" : \"\"}, limit: 50\n\n\#{indent}# When this returns true, DetailedTrace will trace the query\n\#{indent}# Could use `query.context`, `query.selected_operation_name`, `query.query_string` here\n\#{indent}# Could call out to Flipper, etc\n\#{indent}def self.detailed_trace?(query)\n\#{indent} rand <= 0.000_1 # one in ten thousand\n\#{indent}end\n\n RUBY\n\n in_root do\n inject_into_file schema_file, code, after: sentinel, force: false\n end\n\n routes_source = File.read(File.expand_path(\"config/routes.rb\", destination_root))\n already_has_dashboard = routes_source.include?(\"GraphQL::Dashboard\") ||\n routes_source.include?(\"Schema.dashboard\") ||\n routes_source.include?(\"GraphQL::Pro::Routes::Lazy\")\n\n if (!already_has_dashboard || behavior == :revoke)\n log :route, \"GraphQL::Dashboard\"\n shell.mute do\n route <<~RUBY\n # TODO: add authorization to this route and expose it in production\n # See https://graphql-ruby.org/pro/dashboard.html#authorizing-the-dashboard\n if Rails.env.development?\n mount GraphQL::Dashboard, at: \"/graphql/dashboard\", schema: \#{schema_name.inspect}\n end\n\n RUBY\n end\n\n gem(\"google-protobuf\")\n\n end\nend\n"
|