groqd
groqd copied to clipboard
`.raw` projection is not working correctly
Is there an existing issue for this?
- [x] I have searched the existing issues
Code of Conduct
- [x] I agree to follow this project's Code of Conduct
Code Sandbox link
No response
Bug report
Hello, thank you for this amazing lib. V1 is fab!
I've got a schema where I am trying to work out a distance so I am using raw(...) so that I can use the geo::distance function. My query is returning an error:
const itemsQuery = q.star
.filterByType("item")
.project(() => ({
location: true,
distance: q.raw(
"coalesce(geo::distance(location, geo::latLng(1.234, -1.234)), 0)",
q.number(),
),
}));
The error is:
result.distance: Required
I have wrapped it in a coalesce to see if that fixed it as not all location values are set, so want to default to 0 where that is the case, but that still has an error.
When I log out the actual query and paste it into the vision plugin it works perfectly, and all results have a numerical value.
*[_type == "item"] { location, "distance": coalesce(geo::distance(location, geo::latLng(1.234, -1.234)), 0) }
The generated type when viewed in my IDE is:
GroqBuilder<{ location: Geopoint | null; distance: number }[], SchemaConfig>
As a related issue. When I have a raw field I can't then chain an order of any field to the query and get the error:
Error: You cannot chain a new query once you've specified a parser, since this changes the result type.
Thank you for your help :)
Thanks for filing this issue!
I'm trying to reproduce it, but I'm not successful so far.
I added a few unit tests with similar queries here and it seems to be working correctly. So if you can help me find some steps to reproduce, I'd appreciate it!
A couple of things to try:
- Switch
q.number()toq.number().nullable()and see if it still errors? - The error message you posted,
"result.distance: Required", is a little unexpected. In my unit tests, I trigger validation errors, and my error message is:"result[0].imageCount: Expected number, received null". So I'm curious if you could identify where this error message is coming from? (or perhaps you simply shortened it for this bug report?)
Regarding your comment about chaining order ... I will need to think about that implementation, because it seems valid that you should be able to chain order or filter after a projection. I'll create a separate issue for tracking: #346
I tried q.number().nullable() and had the same error. I also tried adding .nullable() to the end of the projection with the same error
...
distance: q.raw(
"coalesce(geo::distance(location, geo::latLng(1.234, -1.234)), 0)",
q.number(),
).nullable(),
...