Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions guides/queries/complexity_and_depth.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,26 @@ class Types::BaseField < GraphQL::Schema::Field
end
```

## Caching complexity

Calculating complexity can be slow in larger monolithic applications. To help with this, QueryComplexity provides a digest that can be used for caching:

```ruby
class CachedMaxQueryComplexity < GraphQL::Analyzer::MaxQueryComplexity
# You should use persistent cache store for production such as Rails.cache or Redis.new
NAIVE_CACHE = {}

def max_possible_complexity
# Dynamic complexities such as lambda complexity or complexity_for method are impossible to cache
if @complexity_digest.key?(query)
NAIVE_CACHE.fetch(Digest::SHA256.hexdigest(@complexity_digest[query].sort_by(&:first).to_json)) { super }
else
super
end
end
end
```

## How complexity scoring works

GraphQL Ruby's complexity scoring algorithm is biased towards selection fairness. While highly accurate, its results are not always intuitive. Here's an example query performed on the [Shopify Admin API](https://shopify.dev/docs/api/admin-graphql):
Expand Down
48 changes: 47 additions & 1 deletion lib/graphql/analysis/query_complexity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def initialize(query)
@complexities_on_type_by_query = {}
@intersect_cache = Hash.new { |h, k| h[k] = {}.compare_by_identity }.compare_by_identity
@possible_types_cache = {}.compare_by_identity
@uncacheable = []
@complexity_digest = Hash.new { |h, k| h[k] = Set.new }
end

# Override this method to use the complexity result
Expand Down Expand Up @@ -85,14 +87,58 @@ def on_enter_field(node, parent, visitor)
return if @skip_introspection_fields && visitor.field_definition.introspection?
parent_type = visitor.parent_type_definition
field_key = node.alias || node.name
field_definition = visitor.field_definition

# Find or create a complexity scope stack for this query.
scopes_stack = @complexities_on_type_by_query[visitor.query] ||= [ScopedTypeComplexity.new(nil, nil, query, visitor.response_path)]

# Find or create the complexity costing node for this field.
scope = scopes_stack.last[parent_type][field_key] ||= ScopedTypeComplexity.new(parent_type, visitor.field_definition, visitor.query, visitor.response_path)
scope = scopes_stack.last[parent_type][field_key] ||= ScopedTypeComplexity.new(parent_type, field_definition, visitor.query, visitor.response_path)
scope.nodes.push(node)
scopes_stack.push(scope)

# We already know we can't cache this query.
if @uncacheable.include?(visitor.query)
return
end

# If the field definition is dynamically computed, we can't cache the complexity.
if field_definition.complexity.is_a?(Proc) || field_definition.respond_to?(:complexity_for)
@uncacheable << visitor.query
@complexity_digest.delete(visitor.query)

return
end

# Add the field definition to the complexity fields hash.
complexity_digest = [field_definition.name, field_definition.complexity]

# The page size affects the complexity.
if field_definition.connection?
arguments = visitor.arguments_for(node, field_definition)
return unless arguments.is_a?(GraphQL::Execution::Interpreter::Arguments) # FIXME: Are errors by definition not cacheable?
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible that what we are looking for here is to flag as not cacheable since we can't access the actual arguments:

unless arguments.is_a?(GraphQL::Execution::Interpreter::Arguments)
  @uncacheable << visitor.query
  @complexity_digest.delete(visitor.query)

  return
end

max_possible_page_size = nil

if arguments[:first]
max_possible_page_size = arguments[:first]
end

if arguments[:last] && (max_possible_page_size.nil? || arguments[:last] > max_possible_page_size)
max_possible_page_size = arguments[:last]
end

if max_possible_page_size.nil?
max_possible_page_size = field_definition.default_page_size || visitor.query.schema.default_page_size || field_definition.max_page_size || visitor.query.schema.default_max_page_size
end

if max_possible_page_size.nil?
raise GraphQL::Error, "Can't calculate complexity for #{field_definition.path}, no `first:`, `last:`, `default_page_size`, `max_page_size` or `default_max_page_size`"
end
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entire algorithm was copied over from GraphQL::Schema::Field#calculate_complexity. It is very likely that we want to extract this max_possible_page_size to a method instead of duplicating it.


complexity_digest << max_possible_page_size
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a field (assuming complexity 1):

["Person.name", 1]

For a connection (assuming complexity 1 and first/last as 10):

["People", 1, 10]

end

@complexity_digest[visitor.query] << complexity_digest
end

def on_leave_field(node, parent, visitor)
Expand Down