Find qraphql performance issues

default discord avatar
wiesson
3 months ago
17

I'm using the GraphQL API to only request what I need but it looks like the Rest API is ~8x faster, even if it returns everything.



{
  Hotels(locale: ${locale}, where: { _status: { equals: published }}, limit: 1000) { docs { id slug updatedAt } },
RoundTrips(locale: ${locale}, limit: 1000, where: { _status: { equals: published }}) { docs { id slug updatedAt } },
  Blogs(locale: ${locale}, limit: 1000) { docs { id slug title content meta { title description } imageSrc image { id cloudPublicId alt slug } } }
},


I use the query above to get the static paths and it takes ~8 seconds. The rest api returns the same thing in like 2 seconds and I'm wondering where and how I could improve sth.



(I'm using the richtext field a lot to maintain internal links etc)

  • default discord avatar
    grandnainconnu
    3 months ago

    Have you tried making 3 separated parallel requests? Or it's maybe not possible in your case?

  • default discord avatar
    wiesson
    3 months ago

    It is possible, I thought it might be faster in one go 🙂



    Let me try

  • default discord avatar
    grandnainconnu
    3 months ago

    I don't know how the resolvers handle multiple collections queries in one go, maybe it isn't async between each other that could cause a long delay

  • default discord avatar
    wiesson
    3 months ago
    getStaticSitePathsByLocale Hotels: 126.58ms
    getStaticSitePathsByLocale RoundTrips: 104.384ms
    getStaticSitePathsByLocale Blogs: 6.577s


    Do you know if I can do "raw" mongodb queries?



    I'd like to select only what I need. I assume that payload.find selects every field of the collection and I'd like to optimize here a bit



    /answered



    /done



    /fixed

  • default discord avatar
    grandnainconnu
    3 months ago

    Yes you can! You have to get the model through payload collections



    payload.collections['yourCollection'].Model


    6 secondes for 1000 documents seems a bit long though, maybe check what the actual resolver performs


    Or you can integrate your own:

    https://payloadcms.com/docs/graphql/extending


    Btw, if it's to generate static paths, you could try to see to make some kind of loop/pagination system, to avoid querying everything at once

  • default discord avatar
    wiesson
    3 months ago

    ah perfect - so like this:



    // executes, name LIKE john and only selecting the "name" and "friends" fields
    await MyModel.find({ name: /john/i }, 'name friends').exec();
  • default discord avatar
    grandnainconnu
    3 months ago

    yes you can execute any raw query or map it as a resolver and use it in GraphQL

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.