🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

How to apply different schema based on type in a GROQ query for nested references

3 replies
Last updated: Jun 9, 2023
Hi. I am having trouble expanding nested references.I have a sections array that can take different types of objects, the objects can have nested fields that are references.
I am trying to write a GROQ query where each section type will have different fields. However, this changes the order of the sections.
Ideally I want to map over each section in the array, and apply a different schema based on the type.

Does anyone know how to achieve this?


        "sections": [
            ...sections[@._type == 'productList']{
                ...
            },
            ...sections[@._type == 'hero']{
                ...
            }
        ]

Jun 8, 2023, 11:45 AM
If I’m understanding what you’re after correctly, this is what you want:
sections": [
            _type == 'productList' => {
                ...
            },
            _type == 'hero' => {
                ...
            }
        ]
Jun 8, 2023, 5:13 PM
That's exactly what I was looking for, thank you very much!
Jun 9, 2023, 12:29 AM
You’re welcome!
Jun 9, 2023, 3:55 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?