rmosolgo / graphql-ruby

Ruby implementation of GraphQL
http://graphql-ruby.org
MIT License
5.38k stars 1.39k forks source link

Dataloader `prime` #5160

Closed ravangen closed 1 week ago

ravangen commented 1 week ago

Is your feature request related to a problem? Please describe.

Provide an option to prime the dataloader's result cache with the provided key and value to avoid data being fetched needlessly.

Examples:

Describe the solution you'd like

Mechanism to add a result into a GraphQL::Dataloader::Source instance.

EDIT: Looking more closely, is that the purpose of merge? Perhaps an alias could be of benefit?

def prime(key, value)
  merge(key => value)
end

Additional context

Exploring feature parity between graphql-batch and dataloader, testing the wheels 😄

rmosolgo commented 1 week ago

Yes, that's exactly what merge is for :+1: I'm open to supporting def prime if you'd like to implement it. It could probably be implemented without creating the Hash, too:

  def prime(new_k, new_v)
    key = result_key_for(new_k)
    @results[key] = new_v
  end 
ravangen commented 1 week ago

Thanks, makes sense. Should have looked more closely.

I'm going to close the issue for now, since I have something working. But I'll keep this in mind for the future.