Solvedgraphql batch Document how one could preload has_many associations

There was some discussion about how one might preload has_many associations in #graphql.

It might be interesting to share some of our AssociationLoader logic or writing a quick tutorial to achieve this in the README.

21 Answers

✔️Accepted Answer

If it helps, I wrote up a gist that shows how you could combine a GraphQL::Batch::Loader together with some instrumentation on your fields, in order to give you this nice syntax:

field :comments, !types[!CommentType] do
  preload comments: :author
  resolve -> (post, args, ctx) { post.comments }
end

It looks a little bit nicer than stacking a bunch of AssociationLoader's together in your resolve procs, and is pretty similar to the includes(comments: :author) syntax that Rails gives you.

https://gist.github.com/theorygeek/a1a59a2bf9c59e4b3706ac68d12c8434

Other Answers:

Hey everyone - I took @theorygeek's excellent work and turned it into a gem. Hopefully you find it useful!

something along these lines would be a wonderful addition to either the documentation or stdlib of graphql-batch.

More Issues: