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.
Our current tooling against N+1 queries uses
lookahead_fielddefined on theBaseObject.sagittarius/app/graphql/types/base_object.rb
Lines 20 to 34 in 9e00268
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.