Open eivindml opened 2 years ago
Note: I believe this is planned for a future version of Groq. And I’ve heard this is the behavior from Groq v1.
Is there now an option for this? Want to migrate away from api v1.
Is there any update on this? This would be great just for default function parameters alone 😅
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: