Skip to content

Improve N+1 queries tooling for GraphQL types #129

Description

@Taucher2003

Our current tooling against N+1 queries uses lookahead_field defined on the BaseObject.

def self.lookahead_field(field, base_scope:, lookaheads: [], conditional_lookaheads: {})
define_method(field) do |*_args, lookahead:, **_kwargs|
field_selected = lambda do |f|
lookahead.selects?(f) ||
lookahead.selection(:nodes).selects?(f) ||
lookahead.selection(:edges).selection(:node).selects?(f)
end
scope = lookaheads.reduce(base_scope.call(object)) { |acc, f| acc.preload(f) }
conditional_lookaheads.reduce(scope) do |acc, (f, preload_field)|
field_selected.call(f) ? acc.preload(preload_field) : acc
end
end
end

With this setup, the configuration for preloads has to be done on every type that includes the type affected by N+1 queries.

This should either be moved to resolvers or some custom tooling that defines the preloads on the affected type directly.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requesttoolingRelated to project internal processes or tooling

Fields

System

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions