Querying different fields for specific types within an array using GROQ in Slack thread

17 replies
Last updated: Apr 14, 2021
Is there a way with GROQ to query different fields for specific
_type
within an array of items with different types?PS: sorry if I feel floody, GROQ is getting my head a bit screwed
🤪
Apr 14, 2021, 6:51 PM
My types are platform or project, and here is my attempt:
*[_type == 'about'][0] {
    content {
        recognitions[] {
            "platforms": *[_type == 'platform'] {
                _type,
                name,
                amount,
                "logo": logo.asset-> {
                    url,
                    "width": metadata.dimensions.width,
                    "height": metadata.dimensions.height,
                },
            },
            "projects": *[_type == 'project'] {
                _type,
                name,
                recognition[] {
                    text,
                    year,
                    link,
                },
            }
        }
    }
}
Apr 14, 2021, 6:53 PM
For sure! You can use conditionals to achieve this. I like to think of this:

_type == 'platform' => {
  ...
}
as the GROQ version of:


if (_type === 'platform') {
  ...
}
Apr 14, 2021, 6:58 PM
My types are platform or project, and here is my attempt:
*[_type == 'about'][0] {
    content {
        recognitions[] {
            "platforms": *[_type == 'platform'] {
                _type,
                name,
                amount,
                "logo": logo.asset-> {
                    url,
                    "width": metadata.dimensions.width,
                    "height": metadata.dimensions.height,
                },
            },
            "projects": *[_type == 'project'] {
                _type,
                name,
                recognition[] {
                    text,
                    year,
                    link,
                },
            }
        }
    }
}
Apr 14, 2021, 6:53 PM
Dammit, I was close on this one haha! Thanks for your precious and quick help
user A
🙂
Apr 14, 2021, 7:00 PM
You were! 👍
Apr 14, 2021, 7:00 PM
Do you think it would be possible to "group" for example all the
platform
results together as
platforms
for instance? So I don't have all the items on the same level and have to filter later when rendering the content.Tried this but doesn't seem to be the way:

"platforms": _type == 'platform' => {
   // fields
}
Apr 14, 2021, 7:10 PM
Do you think it would be possible to "group" for example all the
platform
results together as
platforms
for instance? So I don't have all the items on the same level and have to filter later when rendering the content
Apr 14, 2021, 7:10 PM
user A
Do you think that would be possible to "group" for example all the
platform
results together as a
platforms
array for instance? So I don't have all the items on the same level and have to filter later when rendering data.Tried this but doesn't seem to be the way:

"platforms": _type == 'platform' => {
   // fields
}

Apr 14, 2021, 7:27 PM
user A
Do you think that would be possible to "group" for example all the
platform
results together as a
platforms
array for instance? So I don't have all the items on the same level and have to filter later when rendering data.Tried this but doesn't seem to be the way:

"platforms": _type == 'platform' => {
   // fields
}

Apr 14, 2021, 7:27 PM
I think you can, as long as you don’t need to elevate anything out of an array (I don’t think that’s possible). Just trying to visualize this—does your new query look something like this?

*[_type == 'about'][0] {
  content {
  	recognitions[] {
      _type == "platform" => {
        _type,
        name,
        amount,
        "logo": logo.asset-> {
          url,
          "width": metadata.dimensions.width,
          "height": metadata.dimensions.height,
        }
      },
      _type == 'project' => {
        _type,
        name,
        recognition[] {
          text,
          year,
          link,
        },
      }
    }
  }
}
Apr 14, 2021, 8:23 PM
It is that yes
recognitions[] {
  _type,
  name,
  _type == 'platform' => {
    amount,
    "logo": logo.asset-> {
      url,
      "width": metadata.dimensions.width,
      "height": metadata.dimensions.height,
    },
  },
  _type == 'project' => {
    recognition[] {
      text,
      year,
      link,
    },
  },
},
Apr 14, 2021, 8:23 PM
Okay, thank you. I’m re-reading your question and just want to make sure of what you want. How would you like your returned data to look?
Apr 14, 2021, 8:37 PM
Something like that I suppose:
recognitions: {
  platforms: [],
  projects: [],
}
So I don't have to filter my content that way:

data.recognitions.filter(item => item._type === 'platform')
and then can do

data.recognitions.platforms
Apr 14, 2021, 8:39 PM
I mean I could always change my schema seeing this written that way haha
Apr 14, 2021, 8:40 PM
Okay, that’s helpful. If the important part is grouping them, then it sounds like maybe you’re after something like this:

*[_type == 'about'][0].content {
  'platforms': recognitions[_type == 'platform'] {
    _type,
    name,
    amount,
    "logo": logo.asset-> {
      url,
      "width": metadata.dimensions.width,
      "height": metadata.dimensions.height,
    },
  },
  'projects': recognitions[_type == 'project'] {
    _type,
    name,
    recognition[] {
      text,
      year,
      link,
    },
  },
}
If that doesn’t work I may have misread your schema code.
Apr 14, 2021, 9:16 PM
Cool! Good tip for the
.content
. I was thinking that both the
"projects"
and
"platforms"
would be inside of
recognitions[]
but if it's not possible it's not a big deal, filters work 😉
Apr 14, 2021, 9:19 PM
Would you mind seeing how this works against your schema?

{
  "recognitions": {
    'platforms': *[_type == 'about'][0].content.recognitions[_type == 'platform'] {
      _type,
      name,
      amount,
      "logo": logo.asset-> {
        url,
        "width": metadata.dimensions.width,
        "height": metadata.dimensions.height,
      },
    },
    'projects': *[_type == 'about'][0].content.recognitions[_type == 'project'] {
      _type,
      name,
      recognition[] {
        text,
        year,
        link,
      },
    },
  }
}
Apr 14, 2021, 9:46 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the 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?

Categorized in