Method: GraphQL::Schema.complexity_cost_calculation_mode_for

Defined in:
lib/graphql/schema.rb

.complexity_cost_calculation_mode_for(multiplex_context) ⇒ :future, ...

Implement this method to produce a per-query complexity cost calculation mode. (Technically, it's per-multiplex.)

This is a way to check the compatibility of queries coming to your API without adding overhead of running :compare for every query. You could sample traffic, turn it off/on with feature flags, or anything else.

Examples:

Sampling traffic

def self.complexity_cost_calculation_mode_for(_context)
  if rand < 0.1 # 10% of the time
    :compare
  else
    :legacy
  end
end

Using a feature flag to manage future mode

def complexity_cost_calculation_mode_for(context)
  current_user = context[:current_user]
  if Flipper.enabled?(:future_complexity_cost, current_user)
    :future
  elsif rand < 0.5 # 50%
    :compare
  else
    :legacy
  end
end

Parameters:

  • multiplex_context (Hash)

    The context for the currently-running Execution::Multiplex (which contains one or more queries)

Returns:

  • (:future)

    Use the new calculation algorithm -- may be higher than :legacy

  • (:legacy)

    Use the legacy calculation algorithm, warts and all

  • (:compare)

    Run both algorithms and call legacy_complexity_cost_calculation_mismatch if they don't match



1813
1814
1815
# File 'lib/graphql/schema.rb', line 1813

def complexity_cost_calculation_mode_for(multiplex_context)
  complexity_cost_calculation_mode
end