Using sanity-codegen with @sanity/client to query data and generate types in TypeScript.
17 replies
Last updated: Feb 11, 2021
G
Does anybody have experience with using sanity-codegen ? I feel like the query below should work. It works if I just use
*[_type == "country"]but then it selects all of the values.
const allCountries = await client.query( '*[_type == "country"] { \'slug\': slug.current }' );
Feb 11, 2021, 9:26 PM
G
When you query without the projection (i.e., just
*[_type == "country"]) is
slugat the top-level?
Feb 11, 2021, 9:30 PM
G
Yes
Feb 11, 2021, 9:31 PM
G
i actually think this is a bug in my code 😅
Feb 11, 2021, 9:32 PM
G
Aha, that explains it! I have been confused with this for a while now 😂
Feb 11, 2021, 9:33 PM
G
yeah apologies. to be frank, the sanity-codegen client is somewhat less stable than the codegen itself.
Feb 11, 2021, 9:37 PM
G
the codegen client is just a decent way to get automatic types with a relatively easy implementation but a real implementation of a codegen client would involve parsing groq queries and mapping projections from the codegen’ed schema types
Feb 11, 2021, 9:38 PM
G
so besides bundle size, you’ll really so no improvements over
@sanity/clientfor
cilent.query
Feb 11, 2021, 9:39 PM
G
Ok thanks. My application is fairly simple so i’m not sure if I need anything particularly fancy. Would
@sanity/clientwork with the generated types? I’ve not looked into it much yet
Feb 11, 2021, 9:42 PM
G
it does but you’ll have to import the codegen types yourself and add them as a type parameter
e.g.
e.g.
import * as Schema from '../codegen-output'; // e.g. client.fetch<Schema.BlogPost[]>('*[ _type === 'blogPost' ]');
Feb 11, 2021, 9:45 PM
G
this would pair well with typescript’s utility types: https://www.typescriptlang.org/docs/handbook/utility-types.html
Feb 11, 2021, 9:46 PM
G
e.g.
client.fetch<Pick<Schema.BlogPost, 'title'>[]>('*[ _type === 'blogPost' ] { title }');
Feb 11, 2021, 9:47 PM
G
Ok so i’ve tried this using
Would
@sanity/clientand it seems to work…
const allCountries = await client.fetch<Country[]>( '*[_type == "country"] {_id, slug, name}' );
Country[]or just
Countrymake sense there, seems like both work for me?
Feb 11, 2021, 9:52 PM
G
it would be
Pick<Country, '_id' | 'slug' | 'name'>[]
Feb 11, 2021, 10:02 PM
G
Thanks for your help man, really appreciate it. I’m relatively new to TS so it’s a bit of a challenge
Feb 11, 2021, 10:14 PM
G
Of course. I think i’ll update the docs with some of this feedback too. I think it’ll really help others who are in the same boat.
Feb 11, 2021, 10:17 PM
G
My eventual goal is to get it to where it just generates the rights types for you automatically but I’m not quite there yet
Feb 11, 2021, 10:18 PM
G
That would be nice and I would help get to that point if I had the ability 😂
Feb 11, 2021, 10:30 PM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.