Fetching multiple collections at once

default discord avatar
geersinglast year
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
    paulpopuslast year

    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
    derosullast year

    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
    paulpopuslast year

    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
    derosullast year

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

  • default discord avatar
    geersinglast year

    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.

  • 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 months ago

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

  • default discord avatar
    notchr2 months 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 months 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.