Skip to content

Add the benefit of dataloaders for mutation argumentsΒ #5462

@stevecrozz

Description

@stevecrozz

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions