Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeGen generates Array<never> for query using chained parent operator #6635

Closed
oyvind-stenhaug opened this issue May 13, 2024 · 2 comments · Fixed by sanity-io/groq-js#242
Closed
Assignees
Labels
bug typegen Issues related to TypeScript types generation

Comments

@oyvind-stenhaug
Copy link

Describe the bug

When a GROQ query uses multiple "^" parent operators chained together in a filter componen, the TypeGen-generated type of the query result ends up as Array<never>.

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, defineArrayMember} from 'sanity'

export default defineConfig({
  name: 'default',
  title: 'Sanity Home',
  projectId: 'c16r74n4',
  schema: {
    types: [
      defineType({
        name: 'mainDocument',
        type: 'document',
        fields: [
          defineField({
            name: 'description',
            type: 'array',
            of: [
              defineArrayMember({type: 'block'}),
              defineArrayMember({type: 'listRelevantThings'}),
            ],
          }),
        ],
      }),
      defineType({
        name: 'listRelevantThings',
        type: 'object',
        fields: [
          defineField({
            name: 'foo',
            type: 'string',
          }),
        ],
      }),
      defineType({
        name: 'referencingDocument',
        type: 'document',
        fields: [
          defineField({
            name: 'relevantFor',
            type: 'reference',
            to: {type: 'mainDocument'},
          }),
        ],
      }),
    ],
  },
})
  1. Create a src/queries.ts file with these contents:
const queryWithGrandparent = groq`
  *[_type == "mainDocument" && _id == $id]{
    description[]{
      _type == "listRelevantThings" => {
        "list": *[
          _type == "referencingDocument" && relevantFor._ref == ^.^._id
        ]{
          _id
        }
      }
    }
  }[0]
`
  1. Run typegen, e.g. npx sanity schema extract && npx sanity typegen generate && cat sanity.types.ts, and observe the QueryWithGrandparentResult type

Expected behavior

export type QueryWithGrandparentResult = {
  description: Array<
    | {}
    | {
        list: Array<{
          _id: string
        }>
      }
  > | null
} | null

Actual behavior

export type QueryWithGrandparentResult = {
  description: Array<
    | {}
    | {
        list: Array<never>
      }
  > | null
} | null

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

@rexxars rexxars added the typegen Issues related to TypeScript types generation label May 14, 2024
@bjoerge bjoerge removed the typegen Issues related to TypeScript types generation label May 22, 2024
@sgulseth sgulseth added bug typegen Issues related to TypeScript types generation labels May 22, 2024 — with Linear
@sgulseth sgulseth self-assigned this Jun 20, 2024
@sgulseth sgulseth reopened this Jun 20, 2024
@sgulseth
Copy link
Member

sgulseth commented Jul 2, 2024

Fixed in v3.49.0

@sgulseth sgulseth closed this as completed Jul 2, 2024
Copy link
Contributor

github-actions bot commented Oct 1, 2024

This thread has been automatically locked because it has not had recent activity. Please open a new issue for related bugs and link to relevant comments in this thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug typegen Issues related to TypeScript types generation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants