sanity-io / sanity

Sanity Studio – Rapidly configure content workspaces powered by structured content
https://www.sanity.io
MIT License
5.2k stars 420 forks source link

TypeGen generates Array<never> for query using "in" #6628

Closed oyvind-stenhaug closed 3 weeks ago

oyvind-stenhaug commented 4 months ago

If you find a security vulnerability, do NOT open an issue. Email security@sanity.io instead.

Describe the bug

When a GROQ query uses the in operator in a filter component, the TypeGen-generated type of the query result ends up as Array.

To Reproduce

  1. Create a project (opting in to TypeScript) or edit an existing one, and make a sanity.config.ts like the following:
import {defineConfig, defineType, defineField} from 'sanity'

export default defineConfig({
  name: 'default',
  title: '<your_project_title>',
  projectId: '<your_project_id>',
  schema: {
    types: [
      defineType({
        type: 'document',
        title: 'Test',
        name: 'test',
        fields: [
          defineField({
            name: 'foo',
            type: 'string',
          }),
        ],
      }),
    ],
  },
})
  1. Create a src/queries.ts file with these contents:
const queryWithIn = groq`
  *[_type == "test" && foo in ["bar", "baz"]]{foo}
`
  1. Run typegen, e.g. npx sanity schema extract && npx sanity typegen generate && cat sanity.types.ts, and observe the QueryWithInResult type

Expected behavior

export type QueryWithInResult = Array<{
  foo: string | null
}>

(which is the result generated if you use a filter that involves == instead of in)

Actual behavior

export type QueryWithInResult = Array<never>

Which versions of Sanity are you using?

@sanity/cli (global)          3.41.1 (up to date)
@sanity/eslint-config-studio   4.0.0 (up to date)
@sanity/vision                3.41.1 (up to date)
sanity                        3.41.1 (up to date)

What operating system are you using?

GNU/Linux (Ubuntu 22.04.4 LTS)

Which versions of Node.js / npm are you running?

npm -v && node -v:

10.2.4 v20.11.1

oyvind-stenhaug commented 4 months ago

For what it's worth, this is a simplified case. My actual query is more like

*[_type == "mainDocumentType" && slug.current == $slug]{
  ...,
  portableTextField[]{
    ...,
    _type == "someCustomBlockType" => {
      _type,
      "nested": *[
        _type == "anotherDocumentType" && type in ^.types && (...more stuff)
      ]{
        ${NESTED_QUERY_PROJECTION}
      }
    },
    ${PORTABLE_TEXT_PROJECTION}
  }
}[0]
sgulseth commented 1 month ago

This should now be fixed in the latest release https://github.com/sanity-io/sanity/releases/tag/v3.56.0

sgulseth commented 3 weeks ago

Closing for now, feel free to reopen if this is still an issue