Filtering child references in a Sanity query for a BMW make and model.
18 replies
Last updated: Jun 9, 2022
K
Is it possible to filter child references?
the above shows all
It doesn't seem to work
π€―
*[_type == "make" && name == 'BMW']{ name, "models": models[]->{ name, isHidden } }
modelsregardless if they are hidden or not. When I try to use:
*[_type == "make" && name == 'BMW']{ name, "models": models[isHidden != true]->{ name, isHidden } }
π€―
Jun 8, 2022, 10:55 PM
R
Hey
user J
! What version of the api are you using? Does it work if you try:*[_type == "make" && name == 'BMW']{ name, "models": models[!@.isHidden]->{ name, isHidden } }
Jun 8, 2022, 11:02 PM
K
2022-02-07
Jun 8, 2022, 11:03 PM
K
let me try
Jun 8, 2022, 11:03 PM
R
API should be ok, then!
Jun 8, 2022, 11:04 PM
K
hmm... not working π
Jun 8, 2022, 11:09 PM
R
Hmm, I'm able to filter/expand children on my end, so it looks like we've got something wrong with our query. What does the data for one of these models look like?
Jun 8, 2022, 11:12 PM
K
I've also tried:
but no luck with this one either
*[_type == "make" && name == 'BMW']{ name, "models": models[]->[isHidden == false] }
Jun 8, 2022, 11:14 PM
K
user M
, for now I've filtered out my results in javascript... But it would be useful to be able to filter in groq so it returns less data. Let me know if you get any other ideas. Thanks for your time.Jun 8, 2022, 11:17 PM
R
If you share what your data for
modelslooks like I can probably help.
Jun 9, 2022, 4:26 PM
K
Its pretty straight forward:
Make:
Model:
Make:
export default { name: 'make', title: 'Makes', type: 'document', fields: [ { name: 'name', title: 'Name', type: 'string', validation: (Rule) => Rule.required(), }, { name: 'models', title: 'Models', type: 'array', of: [ { type: 'reference', to: [{ type: 'model' }], }, ], }, ...
export default { name: 'model', title: 'Models', type: 'document', fields: [ { name: 'name', title: 'Model Name', type: 'string', validation: (Rule) => Rule.required(), }, { name: 'isHidden', title: 'Hidden?', type: 'boolean', initialValue: false, },
Jun 9, 2022, 4:31 PM
R
Have you checked that
isHiddenis actually set on the documents you're querying?
Jun 9, 2022, 4:33 PM
K
yes ofcourse:
Jun 9, 2022, 4:36 PM
R
π€· hmmmm. Is your dataset public? If so, can you share the url that vision outputs for that query (it's right above the pane that contains your results).
Jun 9, 2022, 4:38 PM
R
Oh wait, maybe:
*[_type == "make" && name == 'BMW']{ name, "models": models[!@->.isHidden]->{ name, isHidden } }
Jun 9, 2022, 4:39 PM
K
YAYAYAYAYAYAY!!!! WORKS :))))... definitely this example needs to be added to https://www.sanity.io/docs/query-cheat-sheet#3949cadc7524
Jun 9, 2022, 4:41 PM
R
Yes! So glad we got it figured out!
Jun 9, 2022, 4:43 PM
K
thanks so much for your help π
Jun 9, 2022, 4:43 PM
R
Very happy to help!
Jun 9, 2022, 4:45 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.