Generate query types automatically
Is your feature request related to a problem? Please describe.
As mentioned on Bluesky I would like for this module to have the types for my schema/queries available so I don't need to use generics (at best) or define types manually (at worst).
Getting the types is really easy, it takes just a couple commands and a config file. The minimum outcome of this issue would be a note or a page on the docs explaining how to set this up.
However, I would love to see if we could come up with a way to handle all of this on the module side.
Describe the solution you'd like to see
The ideal scenario is we pull some of the usual Nuxt tricks and make all of this happen behind the scenes without the user noticing, and queries are magically typed so no one has to use a generic (#404).
Not sure how we would do this though, hence this issue.
Describe alternatives you've considered
I'm currently working on a pnpm monorepo. I have two projects inside, one for sanity and a second one for nuxt. From sanity I can run a couple scripts (sanity schema extract and sanity typegen generate) and with that I get a file with all my type definitions.
Because having that file on my sanity project is kinda useless, I also have a sanity-typegen.json file with the following contents:
{
"path": "../frontend/app/**/*.{ts,tsx,js,jsx,mjs,cjs,astro,vue}",
"schema": "schema.json",
"generates": "../frontend/shared/types/sanity.d.ts"
}
This file is used by sanity typegen generate to place the resulting file with the types into my nuxt project, under /shared/types/sanity.d.ts, making these types ready to use on my queries via generics.
This is good enough but I really hope someone else (cough, cough, @danielroe 👀) knows how we can make it better.
Happy to help and look into it after the discussion.
Additional context
I've just opened a PR on Sanity's repo to add support for .vue files during typegen: https://github.com/sanity-io/sanity/pull/9979
That is the first step required to make any of this work. I have it pnpm patched on my project but hopefully they'll merge this soon (not that they have been in the past lol).
https://github.com/sanity-io/sanity/pull/9979 has been merged 🙌
I can't find internal types like Serializers being exported from the root or added to Nuxt's types. Would be nice to be able to import from a single place instead of hunting types around components (eg. import type { Serializers } from '@nuxtjs/sanity/runtime/components/sanity-content.js'). Auto-import would be even better.
I can open a different issue for this if needed since they're related to different features. I just didn't want to pollute the repo.
Heads up, #1251 which recently merged into the main branch, removes a lot of internal types in order to adapt to @portabletext/vue, including Serializers. I'm not sure about the presence of other internal types in this module, but this should hopefully make your importing easier.
Do you have a hint about the approach to this @danielroe ? You mentioned on bluesky that you had an idea but I can't figure it out
Heads up, #1251 which recently merged into the main branch, removes a lot of internal types in order to adapt to
@portabletext/vue, includingSerializers. I'm not sure about the presence of other internal types in this module, but this should hopefully make your importing easier.
It still doesn't re-export the types from the module forcing you to install @portabletext/vue or use shamefullyHoist: true if you're using pnpm.
It also doesn't like the use of resolveComponent which makes me really, really sad 😢
Jokes aside, we can tackle this issue once all of the issues with visual and live editing are solved.