groqd: Dereferencing Sanity `image.asset` field typing problem
Is there an existing issue for this?
- I have searched the existing issues
Code of Conduct
- I agree to follow this project’s Code of Conduct
Code Sandbox link
No response
Bug report
Hi, thanks for your work, I am in the middle of migrating from 0.x to 1.x
I have some problems with the deref of the Sanity Image field.
A normal Sanity Image field has the following structure
image?: {
asset?: {
_ref: string
_type: 'reference'
_weak?: boolean
[internalGroqTypeReferenceTo]?: 'sanity.imageAsset'
}
hotspot?: SanityImageHotspot
crop?: SanityImageCrop
_type: 'image'
}
My understanding now is that I can do
.project((sub) => ({
imageUrl: sub.field('image.asset').deref().field('url'),
...
but then typescript complains Argument of type 'url' is not assignable to parameter of type never because the deref seems to not knowing the result type of .field('image.asset').deref()
I tried to use some kind of fragment like
const sanityImage = q.fragmentForType<'sanity.imageAsset'>().project({
url: q.string().optional().nullable(),
})
.project((sub) => ({
imageFragment: sub.field('image.asset').deref().project(sanityImage),
but than I got the error message from groqd ['⛔️ Error: this projection has an invalid property ⛔️'] | ['⛔️ A parser can only be used for known properties; \'url\' is not known ⛔️']. The same problem the defref does not know the type from 'image.asset'
I found a work around for that, so that typescript is happy and the defref knows that 'asset' is a SanityImageAsset type.
.project((sub) => ({
image: sub.field('image').project((subsub) => ({
url: subsub.field('asset').deref().field('url'),
})),
but it seems not intended. Is there some mistake on my side or is that a bug of the defref method?
In your docs you often wrote for example
q.star.filterByType("product").project(sub => ({
imageUrl: sub.field("image").deref().field("url", q.string()),
but that results in {"imageUrl": image->.url} groq query string and that does not work, because the _ref is in the asset field
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 16 (13 by maintainers)
This library has rewired my brain. I have dreams in TS now. It’s weird 😃
It does seem like this is a bug in
deref. I will investigate! Also, you are correct that our doc samples are wrong for images, I’ll get that fixed too!