Module: GraphQL::Current

Defined in:
lib/graphql/current.rb

Overview

This module exposes Fiber-level runtime information.

It won’t work across unrelated fibers, although it will work in child Fibers.

Examples:

Setting Up ActiveRecord::QueryLogs


config.active_record.query_log_tags = [
  :namespaced_controller,
  :action,
  :job,
  # ...
  {
    # GraphQL runtime info:
    current_graphql_operation: -> { GraphQL::Current.operation_name },
    current_graphql_field: -> { GraphQL::Current.field&.path },
    current_dataloader_source: -> { GraphQL::Current.dataloader_source_class },
    # ...
  },
]

Class Method Summary collapse

Class Method Details

.dataloader_source_classClass?

Returns The currently-running Dataloader::Source class, if there is one.

Returns:



48
49
50
# File 'lib/graphql/current.rb', line 48

def self.dataloader_source_class
  Fiber[:__graphql_current_dataloader_source]&.class
end

.fieldGraphQL::Field?

Returns The currently-running field, if there is one.

Returns:

  • (GraphQL::Field, nil)

    The currently-running field, if there is one.

See Also:

  • for a string identifying this field


43
44
45
# File 'lib/graphql/current.rb', line 43

def self.field
  Fiber[:__graphql_runtime_info]&.values&.first&.current_field
end

.operation_nameString?

Returns Comma-joined operation names for the currently-running Multiplex. nil if all operations are anonymous.

Returns:

  • (String, nil)

    Comma-joined operation names for the currently-running Multiplex. nil if all operations are anonymous.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/graphql/current.rb', line 26

def self.operation_name
  if (m = Fiber[:__graphql_current_multiplex])
    m.context[:__graphql_current_operation_name] ||= begin
      names = m.queries.map { |q| q.selected_operation_name }
      if names.all?(&:nil?)
        nil
      else
        names.join(",")
      end
    end
  else
    nil
  end
end