Skip to content

Generate query types automatically #1252

@hacknug

Description

@hacknug

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: sanity-io/sanity#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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions