sanity-io / sanity

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

TypeGen does not generate correct type when querying an array containing multiple references types #6913

Open obrassard opened 3 weeks ago

obrassard commented 3 weeks ago

I have a document containing an array field, mixing different types of reference and objects :

defineField({
      name: 'modules',
      title: 'Modules',
      type: 'array',
      group: 'editorial',
      of: [
        { type: 'reference', name: 'ref.module.fullWidthTextBlock', to: [{ type: 'module.fullWidthTextBlock' }] },
        { type: 'reference', name: 'ref.module.textWithImageBlock', to: [{ type: 'module.textWithImageBlock' }] },
        { type: 'reference', name: 'ref.module.fullWidthImageBlock', to: [{ type: 'module.fullWidthImageBlock' }] },
        // ... Other reference types here
        { type: 'module.instagramBlock' }, // Object
      ],
    }),

We fetch this data using a query like this :

export const SanityProductModulesQuery = groq`
*[_type == 'product' && store.slug.current == $handle][0].modules[] {
    _type == 'module.instagramBlock' => {
        _key,
        _type,
        "title": title[$lang],
        category,
    },
    _type == 'ref.module.textWithImageBlock' => @ -> {
        _type,
        "_key": ^._key,
        "subTitle": subTitle[$lang],
        "title": title[$lang],
        "body": body[$lang],
        color,
        orientation
    },
    _type == 'ref.module.fullWidthTextBlock' => @ -> {
        _type,
        "_key": ^._key,
        "title": title[$lang],
        "body": body[$lang],
    }
}
`;

The Generated Types for this query is incorrect :

export type SanityProductModulesQueryResult = Array<
  | {
      _key: string;
      _type: 'module.instagramBlock';
      title: Array<{
        _type: 'localizedString';
        fr: string;
        en: string;
      }>;
      category: string;
    }
  | {} //We should have other types here, for referenced items
> | null;

The query itself work properly when tested in the vision plugin.


Which versions of Sanity are you using?

@sanity/cli (global)          3.45.0 (up to date)
@sanity/asset-utils            1.3.0 (up to date)
@sanity/color-input            3.1.1 (up to date)
@sanity/eslint-config-studio   3.0.1 (latest: 4.0.0)
@sanity/icons                  3.2.0 (up to date)
@sanity/language-filter        4.0.2 (up to date)
@sanity/ui                     2.3.1 (latest: 2.3.3)
@sanity/vision                3.45.0 (up to date)
sanity                        3.45.0 (up to date)

What operating system are you using? Mac OS 14.5 / Chrome

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

NPM : 9.8.1 NODE : v18.18.2

obrassard commented 3 weeks ago

I thought this issue might be related to https://github.com/sanity-io/sanity/issues/6555 so I removed the => @ to test like this :

export const SanityProductModulesQuery = groq`
*[_type == 'product' && store.slug.current == $handle][0].modules[] {
    _type == 'module.instagramBlock' => {
        _key,
        _type,
        "title": title[$lang],
        category,
    },
    _type == 'ref.module.textWithImageBlock' => {
        _type,
        _key,
        _ref,
    },
    _type == 'ref.module.fullWidthTextBlock' => {
        _type,
        _key,
        _ref,
    }
}
`;

However, the result stays the same, and the generated Array type only contains the correct type for module.instagramBlock (which is the only possible object in our schema).

I have the impression that the problem has more to do with the fact that we have several references with different names, and the parser doesn't resolve properly those custom _type (eg _type == 'ref.module.textWithImageBlock')

sgulseth commented 5 days ago

Hi! Can you try with the latest version we just released? v3.49.0

obrassard commented 5 days ago

Hi @sgulseth, unfortunately the issue persist event after upgrading to v3.49.0 :

⚡npx sanity versions
@sanity/cli (global)          3.49.0 (up to date)
@sanity/asset-utils            1.3.0 (up to date)
@sanity/color-input            3.1.1 (up to date)
@sanity/eslint-config-studio   4.0.0 (up to date)
@sanity/icons                  3.2.0 (up to date)
@sanity/language-filter        4.0.2 (up to date)
@sanity/ui                     2.6.1 (up to date)
@sanity/vision                3.49.0 (up to date)
sanity                        3.49.0 (up to date)