Fetching multiple collections at once

default discord avatar
geersing
4 months ago
8

I want to fetch multiple collections at once, right now my implementation is to fetch each collection in a different promise and then waiting on them all. Is there a way to make a single request that queries multiple collections at once?

  • default discord avatar
    paulpopus
    4 months ago

    There's no way to do that right now, awaiting all of your queries is the right way to do it right now, have you had any issues with that approach?

  • default discord avatar
    derosul
    4 months ago

    You can use the GraphQL api for that.



    In this example, i'm fetching bot "posts" and "categories" using 1 api call.

    Screenshot_2023-05-04_at_10.33.22.png
  • default discord avatar
    paulpopus
    4 months ago

    Ohh derp, I was thinking of the local API only...yes this is the way if you're using graphql



    if you're using REST you can create a custom endpoint for yourself

  • default discord avatar
    derosul
    4 months ago

    yes haha, with the rest api it's quite difficult to do, with the exception of creating your own endpoint ofcourse.

  • default discord avatar
    geersing
    4 months ago

    Right now I am using the local api to fetch every collection that has an url, to make sure the new one is unique. There is nothing wrong with it functionality wise, but it seems inefficient because i fetch every collection separate. Is graphql also intended to be used for internal use? I am making the request from inside payload.

  • default discord avatar
    jarrod69420
    4 months ago

    what are your collections? Could you not just prefix the document slug with the collection type?



    Also, have you seen the nested docs plugin:

    https://github.com/payloadcms/plugin-nested-docs
Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.