groqd
groqd copied to clipboard
fix: fixed conditional selection bug (empty base schema)
fixes #25
also touches #68
Please review, @gksander.
Sorry for ping, but couldn't request a review, since I am no maintainer.
So we're working on an idea with a new select() method that was part of the 0.10.0 release. You can check out the details in this PR here, but it's basically for the case where you have an empty base selection, and your sanity data falls into one of a few shapes.
To omit the empty object (or null depending on which context you use it), you pass it a default case. So here it would look like:
const { data, query } = await runPokemonQuery(
q("*")
.filter("_type == 'pokemon'")
.slice(0, 3)
.select(
{
"name == 'Charmander'": {
name: q.literal("Charmander"),
hp: ["base.HP", q.number()],
},
default: {
name: q.string(),
attack: ["base.Attack", q.number()],
},
}
)
);
or if you wanted specifically Bulbasaur & Charmander, you could add it to the filter, ie:
const { data, query } = await runPokemonQuery(
q("*")
.filter("_type == 'pokemon' && (name == 'Charmander' || name == 'Bulbasaur')")
.select(
{
"name == 'Charmander'": {
name: q.literal("Charmander"),
hp: ["base.HP", q.number()],
},
default: {
name: q.literal("Bulbasaur"),
attack: ["base.Attack", q.number()],
},
}
)
);
In the groq-builder branch, we've addressed this issue by automatically including the empty object in the result type, but there's an option called isExhaustive that won't include the empty object type.