Class: GraphQL::Execution::Multiplex Private

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(schema:, queries:, context:) ⇒ 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.



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

def initialize(schema:, queries:, context:)
  @schema = schema
  @queries = queries
  @context = context
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.



30
31
32
# File 'lib/graphql/execution/multiplex.rb', line 30

def context
  @context
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.



30
31
32
# File 'lib/graphql/execution/multiplex.rb', line 30

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.



30
31
32
# File 'lib/graphql/execution/multiplex.rb', line 30

def schema
  @schema
end

Class Method Details

.run_all(schema, query_options, *rest) ⇒ 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.



38
39
40
41
# File 'lib/graphql/execution/multiplex.rb', line 38

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

.run_queries(schema, queries, context: {}, max_complexity: nil) ⇒ 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.



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
75
76
77
78
79
80
81
# File 'lib/graphql/execution/multiplex.rb', line 43

def run_queries(schema, queries, context: {}, max_complexity: nil)
  query_instrumenters = schema.instrumenters[:query]
  multiplex_instrumenters = schema.instrumenters[:multiplex]
  multiplex = self.new(schema: schema, queries: queries, context: context)

  # First, run multiplex instrumentation, then query instrumentation for each query
  multiplex_instrumenters.each { |i| i.before_multiplex(multiplex) }
  queries.each do |query|
    query_instrumenters.each { |i| i.before_query(query) }
  end

  multiplex_analyzers = schema.multiplex_analyzers
  if max_complexity ||= schema.max_complexity
    multiplex_analyzers += [GraphQL::Analysis::MaxQueryComplexity.new(max_complexity)]
  end

  GraphQL::Analysis.analyze_multiplex(multiplex, multiplex_analyzers)

  # Then, do as much eager evaluation of the query as possible
  results = queries.map do |query|
    begin_query(query)
  end

  # Then, work through lazy results in a breadth-first way
  GraphQL::Execution::Lazy.resolve(results)

  # Then, find all errors and assign the result to the query object
  results.each_with_index.map do |data_result, idx|
    query = queries[idx]
    finish_query(data_result, query)
  end
ensure
  # Finally, run teardown instrumentation for each query + the multiplex
  # Use `reverse_each` so instrumenters are treated like a stack
  queries.each do |query|
    query_instrumenters.reverse_each { |i| i.after_query(query) }
  end
  multiplex_instrumenters.reverse_each { |i| i.after_multiplex(multiplex) }
end