Absinthe parent id in resolver with dataloader


I have a schema that looks like this

object :post do
    field(:id, non_null(:id))
    field(:products, list_of(non_null(:post_product)), resolve: dataloader(:repo))
    field(:description, :string)

object :post_product do
    field(:product_id, non_null(:string)
    field(:url, :string) do

I need the post.id to build the product URL, it should be something like this .../posts/{post_id}/products/{product_id}

One product may be in multiple posts, so inside the Product schema I have a list of Post.
The client can only query products through posts, so they’re always inside a Post scope.

How can I pass the post_id to the post_product.url resolver?
Or I’m going with a bad solution for this?

Thanks in advance :slight_smile:

UPDATE: After the answer from @fuelen I realize that my example didn’t match what I need to do, so I change the question and the example :slight_smile:


Looks like post_comment struct have a foreign key to post. I guess, it is done via post_id field. post_comment is passed as a first argument to &Resolver.build_url/3, so simply use post_comment.post_id in it.

Hi, thanks for the answer and you’re right.

But actually I realized that my example didn’t match what I have in the project, so I updated the question and the example :slight_smile: