Fetching multiple collections at once

default discord avatar
geersing12 months ago
9

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
    paulpopus12 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
    derosul12 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
    paulpopus12 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
    derosul12 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
    geersing12 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.

  • discord user avatar
    jarrod_not_jared
    12 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
  • default discord avatar
    christopher.nowlan2 weeks ago

    Is it possible yet to query multiple collections at the same time using the local api?

  • default discord avatar
    notchr2 weeks ago

    I don't think you can query two at once? But I know that you can make two calls and merge the results



    https://payloadcms.com/docs/local-api/overview#local-options-available
  • default discord avatar
    christopher.nowlan2 weeks ago

    Thanks.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

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