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

default discord avatar
wiesson
3 months ago
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
    jarrod69420
    3 months ago

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

  • default discord avatar
    wiesson
    3 months ago

    Sure!



    Any example I could have a look at?

  • default discord avatar
    jarrod69420
    3 months ago

    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
    wiesson
    3 months ago

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

  • default discord avatar
    jarrod69420
    3 months ago

    Yeah not a bad idea!

  • default discord avatar
    wiesson
    3 months ago

    /solved

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.