-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
select$ operator for optional queries #183
Comments
@heggemsnes heres a bit of further context around this discussion for your example, the select method conditions accept queries as well as the projection shorthand, so does using the entity query equivalent of what you have work? q.select(
"_type == 'nice'": q('').grab$({
title: q.string().optional()
})
) |
just a heads up, using the entity level select const entityQuerySelect = q('').select({
"_type == 'nice'": q('').grab$({
title: q.string().optional()
})
})
console.log(entityQuerySelect.query)
/* output GROQ:
{
...select(
_type == 'nice' => { title }
)
}
*/
const propertyLevelSelect = q.select({
"_type == 'nice'": q('').grab$({
title: q.string().optional()
})
})
console.log(propertyLevelSelect.query)
/* output GROQ:
select(
_type == 'nice' => { title }
)
*/ |
Hei @littlemilkstudio thanks for your input here. I'm mostly doing the export const linksSelect = q.select({
"_type == 'externalLinkObject'": {
_key: q.string(),
_type: q.literal("externalLinkObject"),
...externalLinkQuery,
},
"_type == 'internalLinkObject'": {
_key: q.string(),
_type: q.literal("internalLinkObject"),
...internalLinkQuery,
},
"_type == 'downloadLinkObject'": {
_key: q.string(),
_type: q.literal("downloadLinkObject"),
...downloadLinkQuery,
},
default: {
linkType: q.literal("unknown"),
link: q("link").grab({
title: q.string().nullable(),
}),
_key: q.string(),
},
}) While I realise that this does what I want:
It just feels like a bit unnecessary boilerplate. I think a discussion around nullable/optionals and what should be the default would be nice since |
Yea absolutely. This can be kind of confusing. So As far as which you should default to using between Interacting with sanity and groq directly, if a field is queried and a value does not exist, it will return This becomes interesting if you want to supply a default value for a field on validation if it doesn't exist. In such a scenario, Lastly, if you are unsure, or want to pass the same selection between to both |
Yea totally hear you. Another 2 options you can use here are:
"_type == 'downloadLinkObject'": nullToUndefined({
_key: q.string(),
_type: q.literal("downloadLinkObject"),
...downloadLinkQuery,
}),
I think something like a Does the clarification on |
Hey @littlemilkstudio thanks for taking the time here! I was not aware I could do I was also not aware about nullish(). Sounds perfect our use case :) |
Awesome! If you're still running into scenarios where you think a |
Hi!
As the title suggest, I'm loving both grab$ and select, but the .optional() method doesn't work the way I expect it to in regards to validation when using it like this:
When using nullToUndefined it works as expected :)
The text was updated successfully, but these errors were encountered: