You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
typeLink={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.
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.
Is there now an option for this? Want to migrate away from api v1.
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
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:
It can fail, since my label type is
label?: string
while Sanity basically returnslabel: string | null
, but only for some queries.Example:
If I do a query like this;
the "optional"
label
will return a value ifstring | null
. It would be better to not return it at all, or return it asundefined
.If I use the
...
syntax like this;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.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).
Describe alternatives you've considered
defined()
everywhere.null
Additional context
Generic function that will run a query, optionally transform the data and then try to decode it:
And here is a usage example:
The text was updated successfully, but these errors were encountered: