-
Notifications
You must be signed in to change notification settings - Fork 105
Open
Labels
Description
This library is fantastic and thanks for publishing it!
In my testing, IDs are not always aggregated across multiple queries when Schema#multiplex
is used. The promises do resolve correctly, but one DB query is made per multiplexed gql query.
I found a test in this repo, which runs queries with multiplex
and uses QueryNotifier.subscriber
to assert that only 1 query is made, but my loader does not behave.
Current gem versions:
gem "graphql", "=1.6.0"
gem "graphql-batch", "=0.3.9"
The loader in question is very simple:
module Loaders
class Single < ::GraphQL::Batch::Loader
attr_reader :model, :options, :key
def initialize(model_arg, options_arg = {})
@model = model_arg
@options = options_arg
@key = options.fetch(:key, :id)
end
def perform(ids)
model.where(key => ids).all.each(&method(:fulfill_record))
ids.reject(&method(:fulfilled?)).each(&method(:fulfill_nil))
end
private
def fulfill_record(record)
fulfill(record.send(key), record)
end
def fulfill_nil(id)
fulfill(id, nil)
end
end
end