-
Notifications
You must be signed in to change notification settings - Fork 426
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
Schema extract always assumes inline referenced documents are References #6913
Comments
I thought this issue might be related to #6555 so I removed the export const SanityProductModulesQuery = groq`
*[_type == 'product' && store.slug.current == $handle][0].modules[] {
_type == 'module.instagramBlock' => {
_key,
_type,
"title": title[$lang],
category,
},
_type == 'ref.module.textWithImageBlock' => {
_type,
_key,
_ref,
},
_type == 'ref.module.fullWidthTextBlock' => {
_type,
_key,
_ref,
}
}
`; However, the result stays the same, and the generated Array type only contains the correct type for I have the impression that the problem has more to do with the fact that we have several references with different names, and the parser doesn't resolve properly those custom |
Hi! Can you try with the latest version we just released? v3.49.0 |
Hi @sgulseth, unfortunately the issue persist event after upgrading to v3.49.0 :
|
@sgulseth We noticed another issue that might be related to this one. For context, we have different However, in one particular case we wanted to use some of those section schema without them being referenced. For this case, we've been using our
Where all "modules" are
It is noteworthy that once the type of |
Thanks for letting us know, @obrassard! I suspect this is related to sanity-io/groq-js#250 as we weren't handling splatting over optional fields very well. A workaround, and to confirm the hypothesis, would be to mark the |
@sgulseth Unfortunately adding a required validation on the modules field did not solve the issue. To help you diagnose the issue, I created a sample repository here : https://github.com/obrassard/sanity-typegen-issues containing an example of the two problems mentioned here. In the example you'll find 4 types of documents (two types of page
There is two different queries in the groq directory that were used to fetch an article or a product based on their slug. In both case the generated types are incorrect for the Note that you can use Let me know if you need more info ! 😄 |
On other branches of the example repo, you may also notice how
|
Ah, they are referencing another document. Then you need to dereference the referenced document. So instead of doing:
do:
|
I see. The problem is still similar: Inside of
So you can't compare _type to the referenced document type. What you want is something like:
here we dereference Running this locally gives me:
|
Thanks @sgulseth I hadn't thought of writing the product query this way, but indeed by modifying the query as you proposed, it works as expected. Note, however, that the initial query (with On the other hand, it doesn't solve the second case (BlogArticleQuery) where there is no reference. |
np!
That is weird! That might indicate that the schema model and your content model isn't 1-1 Is the reproduction repo you pushed up to date with what you have that doens't work? |
Sorry for the confusion, actually adding the It does not work, because unlike the other query the modules are not referenced here (even tho they are declared as |
@sgulseth While testing the new query with If you have an array mixing references and non-refeferenced objects, the result is not correct. I updated the example repo, and as you can see here the When generating the type, we got :
But we should have :
|
From what I can tell based on your vision result is that your schema has diverged from the content. In your schema it's references, but in the content lake it's stored inline.
yes, then you also need to compare the types inside the modules container. Ie something like:
generates: modules: Array<
| {
_key: string
_type: 'module.objectBlock'
title: string
}
| {
_key: string
_type: 'module.textBlock'
title: string
body: string
}
| {
_key: string
_type: 'module.textImageBlock'
subTitle: string | null
title: string
body: string
image: {
asset?: {
_ref: string
_type: 'reference'
_weak?: boolean
[internalGroqTypeReferenceTo]?: 'sanity.imageAsset'
}
hotspot?: SanityImageHotspot
crop?: SanityImageCrop
_type: 'image'
}
}
> |
In the definition of the defineField({
name: 'modules',
type: 'array',
validation: Rule => Rule.required(),
// No reference here
of: [
{ type: 'module.textBlock' },
{ type: 'module.textImageBlock' },
],
}) E.g. If I add a text block in an article, the text block data is saved directly in the article document itself, not in a It seems that the issue is with the // Not this
of: [
{ type: 'reference', to: [
{ type: 'module.textBlock' },
{ type: 'module.textImageBlock' },
]}
] This assumption is probably made because However, in our real case scenario, we would not want to define those modules as Of course, we could have created other |
Ah. I see, you are using a document as an inline reference. This looks to be a bug in the schema extract, yes :) |
This thread has been automatically locked because it has not had recent activity. Please open a new issue for related bugs and link to relevant comments in this thread. |
I have a document containing an array field, mixing different types of reference and objects :
We fetch this data using a query like this :
The Generated Types for this query is incorrect :
The query itself work properly when tested in the vision plugin.
Which versions of Sanity are you using?
What operating system are you using?
Mac OS 14.5 / Chrome
Which versions of Node.js / npm are you running?
NPM : 9.8.1
NODE : v18.18.2
The text was updated successfully, but these errors were encountered: