groqd icon indicating copy to clipboard operation
groqd copied to clipboard

fix: fixed conditional selection bug (empty base schema)

Open iduuck opened this issue 2 years ago • 3 comments

fixes #25

also touches #68

iduuck avatar Feb 24 '23 16:02 iduuck

Please review, @gksander.


Sorry for ping, but couldn't request a review, since I am no maintainer.

iduuck avatar Feb 24 '23 16:02 iduuck

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()],
            },
          }
        )
    );

maxyinger avatar Mar 09 '23 14:03 maxyinger

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.

scottrippey avatar Feb 08 '24 02:02 scottrippey