Skip to content
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

Feature request: Don't return null values from groq query #100

Open
eivindml opened this issue May 24, 2022 · 3 comments
Open

Feature request: Don't return null values from groq query #100

eivindml opened this issue May 24, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@eivindml
Copy link

eivindml commented May 24, 2022

For some applications, I like to use a runtime type system so I can decode the query responses from Sanity, and have a 100% runtime guarantee of correct types. One issue I'm often bumping into with regards to this, is that Sanity returns optional empty values as null.

If my decoder tries to decode the value:

type Link = {
  title: string;
  label?: string;
}

It can fail, since my label type is label?: string while Sanity basically returns label: string | null, but only for some queries.

Example:

If I do a query like this;

*[_type == "link"] {
  _id,
  title,
  label
}

the "optional" label will return a value if string | null. It would be better to not return it at all, or return it as undefined.

Skjermbilde 2022-05-24 kl  11 05 39

If I use the ... syntax like this;

*[_type == "link"] {
  ...
}

I get the result I prefer, but now I'm querying unnecessary data.

I learnt that I can use the defined(), and it works, but it's a bit verbose, I have to apply that to all optional fields.

*[_type == "resource"] {
  _id,
    "slug": slug.current,
    title,
    defined(subtitle) => {subtitle},
    defined(content) => {content},
    description,
    defined(keywords) => {keywords},
    defined(video.url) => {"video": video.url},
    "asset": {
      defined(external) => {"external": asset.external},
      defined(internal) => {"internal": asset.internal.asset->url}
    },
    categories[]->{_id, title},
   "poster": poster.asset-> {
     ...,
     "lqip": metadata.lqip
   }
}

Describe the solution you'd like

It would be nice to be able to specify this behaviour at the top level of the groq query (or something similar).

*[_type == "resource"] | defined(*) {
  _id,
    "slug": slug.current,
    title,
}

Describe alternatives you've considered

  • Currently I'm sprinkling in defined() everywhere.
  • I've also considered crating a transform function, that can be applied to all responses, that will strip out any value that is null

Additional context

Generic function that will run a query, optionally transform the data and then try to decode it:
Skjermbilde 2022-05-24 kl  13 01 58

And here is a usage example:
Skjermbilde 2022-05-24 kl  13 04 30

@KittyGiraudel
Copy link

Note: I believe this is planned for a future version of Groq. And I’ve heard this is the behavior from Groq v1.

@kmelve kmelve transferred this issue from sanity-io/sanity Jan 12, 2023
@kmelve kmelve added the enhancement New feature or request label Jan 12, 2023
@eivindml
Copy link
Author

eivindml commented Feb 6, 2024

Is there now an option for this? Want to migrate away from api v1.

@rexxars rexxars changed the title Feature requeest: Don't return null values from groq query Feature request: Don't return null values from groq query Feb 22, 2024
@scyzemic
Copy link

Is there any update on this? This would be great just for default function parameters alone 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants