This isn't a bug, and I've been conscious of it during the last year of development. Now that I'm closing in on production and launching, I'm appreciating the scale of the problem it causes.
The issues comes from the liberal use of depth: 2-3 on collections and queries, which is especially pronounced due to the use of a ReusableContent collection pattern. The layout blocks on these ReusableCollection records add an extra level to the required depth (so that all the data required by the layout block components is available).
The size of the json data returned from the api is huge because of the side effect of the above. Some of my collections have records which contain 70 pages on their relationship field. You can see how these depth queries produce exponentially large responses.
It's not a big issue if I keep the mongo database running on the same machine as the server, and the lag here is tolerable thanks to the payload api (with a single user interacting with the site), however I was hoping to try Payload Cloud, and take advantage of the MongoDB Atlas serverless db.
As a test, I added my DB to the a new MongoDB Atlas serverless instance and connected it to my production build. The results were awful. 4-5 sec response lags on every route change, and initial load even worse 🤦♂️.
Optimisation advice
So with the above in mind — I believe I need to migrate all of my api calls to GraphQL, right?
I expect this to be a huge change, and I'm curious to know of others' experience in using GraphQL inside a NextJS application.
Can I incrementally adopt GraphQL? How would this work in practice?
I currently use the pages folder for the whole frontend, and wondering whether this would be a good point to embrace the app folder — If this were so, can I separate out the REST/GraphQL approach? Or does this need to be a wholistic change (all components/routes/pages need to use GraphQL)?
Any advice would be very welcome!
Thanks 🙏