Class: GraphQL::Execution::Multiplex Private

Inherits:
Object
  • Object
show all
Includes:
Tracing::Traceable
Defined in:
lib/graphql/execution/multiplex.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Execute multiple queries under the same multiplex "umbrella". They can share a batching context and reduce redundant database hits.

The flow is:

  • Multiplex instrumentation setup
  • Query instrumentation setup
  • Analyze the multiplex + each query
  • Begin each query
  • Resolve lazy values, breadth-first across all queries
  • Finish each query (eg, get errors)
  • Query instrumentation teardown
  • Multiplex instrumentation teardown

If one query raises an application error, all queries will be in undefined states.

Validation errors and GraphQL::ExecutionErrors are handled in isolation: one of these errors in one query will not affect the other queries.

See Also:

  • for public API

Constant Summary collapse

NO_OPERATION =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Used internally to signal that the query shouldn't be executed

{}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Tracing::Traceable

#trace

Constructor Details

#initialize(schema:, queries:, context:, max_complexity:) ⇒ Multiplex

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Multiplex.



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/graphql/execution/multiplex.rb', line 33

def initialize(schema:, queries:, context:, max_complexity:)
  @schema = schema
  @queries = queries
  @queries.each { |q| q.multiplex = self }
  @context = context
  @dataloader = @context[:dataloader] ||= @schema.dataloader_class.new
  @tracers = schema.tracers + (context[:tracers] || [])
  # Support `context: {backtrace: true}`
  if context[:backtrace] && !@tracers.include?(GraphQL::Backtrace::Tracer)
    @tracers << GraphQL::Backtrace::Tracer
  end
  @max_complexity = max_complexity
end

Instance Attribute Details

#contextObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
# File 'lib/graphql/execution/multiplex.rb', line 32

def context
  @context
end

#dataloaderObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
# File 'lib/graphql/execution/multiplex.rb', line 32

def dataloader
  @dataloader
end

#max_complexityObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
# File 'lib/graphql/execution/multiplex.rb', line 32

def max_complexity
  @max_complexity
end

#queriesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
# File 'lib/graphql/execution/multiplex.rb', line 32

def queries
  @queries
end

#schemaObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
# File 'lib/graphql/execution/multiplex.rb', line 32

def schema
  @schema
end

Class Method Details

.begin_query(results, idx, query, multiplex) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/graphql/execution/multiplex.rb', line 78

def begin_query(results, idx, query, multiplex)
  operation = query.selected_operation
  result = if operation.nil? || !query.valid? || query.context.errors.any?
    NO_OPERATION
  else
    begin
      # These were checked to be the same in `#supports_multiplexing?`
      query.schema.query_execution_strategy.begin_query(query, multiplex)
    rescue GraphQL::ExecutionError => err
      query.context.errors << err
      NO_OPERATION
    end
  end
  results[idx] = result
  nil
end

.run_all(schema, query_options, **kwargs) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



48
49
50
51
# File 'lib/graphql/execution/multiplex.rb', line 48

def run_all(schema, query_options, **kwargs)
  queries = query_options.map { |opts| GraphQL::Query.new(schema, nil, **opts) }
  run_queries(schema, queries, **kwargs)
end

.run_queries(schema, queries, context: {}, max_complexity: schema.max_complexity) ⇒ Array<Hash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns One result per query.

Parameters:

  • schema (GraphQL::Schema)
  • queries (Array<GraphQL::Query>)
  • context (Hash) (defaults to: {})
  • max_complexity (Integer, nil) (defaults to: schema.max_complexity)

Returns:

  • (Array<Hash>)

    One result per query



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/graphql/execution/multiplex.rb', line 58

def run_queries(schema, queries, context: {}, max_complexity: schema.max_complexity)
  multiplex = self.new(schema: schema, queries: queries, context: context, max_complexity: max_complexity)
  multiplex.trace("execute_multiplex", { multiplex: multiplex }) do
    if supports_multiplexing?(schema)
      instrument_and_analyze(multiplex) do
        run_as_multiplex(multiplex)
      end
    else
      if queries.length != 1
        raise ArgumentError, "Multiplexing doesn't support custom execution strategies, run one query at a time instead"
      else
        instrument_and_analyze(multiplex) do
          [run_one_legacy(schema, queries.first)]
        end
      end
    end
  end
end