Query all pages / collections for all locales to get all slug fields via GraphQL / Rest API

default discord avatar
wiessonlast year
7

I find it very time-consuming and error-prone to maintain the SEO related fields such as alternate hreflang because I need the other locales / slug of each page / collection.



I can query

/api/pages?locales=all

but it returns everything which is a way too much, I only need the id, the slug field and title/description. Does anyone has a good approach to solve this? For now, I'm querying each collection and each page by id but it takes a while and I assume, it could be way faster.

  • default discord avatar
    jarrod69420last year

    Hey @wiesson, wondering if a custom endpoint would work for you?

  • default discord avatar
    wiessonlast year

    Sure!



    Any example I could have a look at?

  • default discord avatar
    jarrod69420last year

    Collections can have custom endpoints, check out the

    endpoints

    property here (

    https://payloadcms.com/docs/configuration/collections#options

    ).



    With it you can open a custom endpoint and use req.payload to use the local api. Fetch with payload.find({ collection: 'pages', locale: 'all' }) and then trim back the response for your endpoint. Alternatively you could even access the underlying mongoose model to make the query, i.e. payload.collections.['your-collection-slug-to-access'].Model().

  • default discord avatar
    wiessonlast year

    ah, wonderful! Should be built-in 🙂 (or available via the SEO plugin)

  • default discord avatar
    jarrod69420last year

    Yeah not a bad idea!

  • default discord avatar
    wiessonlast year

    /solved

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.