Class: GraphQL::Analyzer::Instrumentation::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/analyzer/instrumentation/base.rb

Direct Known Subclasses

Mysql, Postgresql, Sqlite3

Instance Method Summary collapse

Instance Method Details

#instrument(type, field) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/graphql/analyzer/instrumentation/base.rb', line 5

def instrument(type, field)
  ->(obj, args, ctx) do
    result = nil
    queries = ::ActiveRecord::Base.collecting_queries_for_explain do
      result = field.resolve_proc.call(obj, args, ctx)
      result.to_a if result.respond_to?(:to_a)
    end

    if queries.any?
      explain_output = ::ActiveRecord::Base.exec_explain(queries)
      parsed_output = parser.parse(explain_output)

      # TODO: Merge results when a field makes two types of queries
      # e.g. path: ['user', 'name'] makes a SQL and ES Query.
      ctx['graphql-analyzer']['resolvers'] << {
        'path' => ctx.path,
        'adapter' => adapter,
        'parentType' => type.name,
        'fieldName' => field.name,
        'returnType' => field.type.to_s,
        'details' => parsed_output
      }
    end

    result
  end
end