Add the benefit of dataloaders for mutation arguments
Is your feature request related to a problem? Please describe.
I have an input for a mutation which is an array of tree nodes and their children (adjacency list). When using the "loads" param for nested mutation arguments, they do load data, but don't batch the loads.
My scheme's object_from_id method is configured to use dataloader in the hopes that loading objects can be done in a small number of database queries.
Describe the solution you'd like
I would like it if the loads params I provide to TreeNode would execute my loader in batch with all the IDs at once. But instead, each invocation of Sources::ByGlobalID#fetch receives only one single node's IDs.
Describe alternatives you've considered
I have tried many approaches to get this to work. I thought perhaps I could add the 'loads' param to the top level argument in ApplyTreeMutation, but that one seems to be executed after the TreeNode loads methods. I've also looked into using prepare, which I think would work if used Types::TreeNode#coerce_input, but I'm guessing that won't include the authorization that comes when I use loads and I'm not sure this is a stable API.
Additional context
I have copied a simplified version of what I'm doing. Hopefully this is enough to understand.
module Mutations
class ApplyTreeMutation < BaseMutation
argument :tree, type: [Types::TreeNode], required: true
end
end
module Types
class TreeNode < Types::BaseInputObject
argument :child_ids, type: [ID], required: true, loads: Unions::TreeContent
argument :id, type: ID, required: true, as: :object, loads: Unions::TreeContent
end
end
class MySchema
def self.object_from_id(encoded_id, context)
context.dataloader.with(Sources::ByGlobalID).load(encoded_id)
end
end
module Sources
class ByGlobalID < GraphQL::Dataloader::Source
def fetch(gid_params)
objects_by_gid_param = GlobalID::Locator.locate_many(gid_params, ignore_missing: true).index_by(&:to_gid_param)
gid_params.map do |gid_param|
objects_by_gid_param[gid_param]
end
end
end
end
Hey, thanks for the detailed writeup. I agree it would be better to make this work the way you described. Implementation-wise, it'd be complicated just because of how it's currently implemented, where each "depth" of arguments must be completely resolved before proceeding to the next depth, and as you noticed, it doesn't batch across lists.
I don't have short-term plans to investigate this but I'm hoping to land a pretty big refactor to execution code before the end of the year (#5389). I have a hunch it will make this easier to fix because it reduces all the crazy recursion in the current code.
If you have a chance to add a failing test to the GraphQL-Ruby test suite, that be a great first step.