rmosolgo/graphql-ruby

dataloaders not working in autoloaded mutation arguments

stevecrozz opened this issue · 1 comments

Describe the bug

I have a mutation that takes an argument like:

argument :thing_ids, type: [ID], required: true, loads: Types::Thing

The objects are automatically loaded which is great. And they are also automatically authorized which is also great. But the authorization on the Thing type uses a dataloader (extending GraphQL::Dataloader::Source). It does run, but the dataloader fetch method is called once for every loaded object rather than once for the whole batch.

Versions

graphql version: 2.2.5
rails (or other framework): 6.1.7.7

GraphQL schema

If the above info is not enough to go on, I will put together a more complete report with an example scenario for you.

Hey, sorry for my slow response to this. I just wrote up a replication:

require "bundler/inline"

gemfile do
  gem "graphql", "2.3.5"
end

class MySchema < GraphQL::Schema
  class AuthSource < GraphQL::Dataloader::Source
    def fetch(objs)
      puts "#{self.class}: #{objs}"
      objs.map { |_o| true }
    end
  end

  class Thing < GraphQL::Schema::Object
    def self.authorized?(obj, ctx)
      ctx.dataloader.with(AuthSource).load(obj)
    end
    field :name, String
  end

  class Query < GraphQL::Schema::Object
    field :things, [Thing] do
      argument :thing_ids, [ID], loads: Thing
    end

    def things(things:)
      things
    end
  end

  query(Query)
  use GraphQL::Dataloader
  def self.object_from_id(id, ctx)
    OpenStruct.new(name: "Thing #{id}")
  end

  def self.resolve_type(abs_type, obj, ctx)
    Thing
  end
end


query_string = <<~GRAPHQL
{
  things(thingIds: ["1", "2", "3"]) { name }
}
GRAPHQL

pp MySchema.execute(query_string).to_h

I'll work up a fix soon!