ricokahler / sanity-codegen

Generate TypeScript types from your Sanity.io schemas
sanity-codegen-dev.vercel.app
MIT License
270 stars 19 forks source link

Incorrect groq codegened types when using array projection #261

Open itsHabib opened 2 years ago

itsHabib commented 2 years ago

Version: @sanity-codegen/groq-codegen": "1.0.0-alpha.26".

👋 Hello I've been noticing some behavior from the groq-codegen tool that doesn't look quite right. I've noticed when I have a query that uses an array projection, the type of the first field determines the rest of the types of the fields. For example, if I have a query like so:

 *[_type == 'my-type' && _id == $typeId]{
          "outer": outer[]-> {
            field-1
           field-2
        }

Assume that field-1 is type string and field-2 is a number. When the groq-codegen completes the query type will say that bothfield-1andfield-2` are strings. If i switched the query around to look like this:

 *[_type == 'my-type' && _id == $typeId]{
          "outer": outer[]-> {
            field-2 <------ field-2 is first now
           field-1
        }

the generated query type will say that the types of both field-1 and field-2 are numbers. This also seems to be true if I put any other type first. Am I missing something in my query, or is this an actual issue?