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

groq-builder: Query-result mismatch #298

Open
2 tasks done
brazelja opened this issue Aug 6, 2024 · 0 comments
Open
2 tasks done

groq-builder: Query-result mismatch #298

brazelja opened this issue Aug 6, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@brazelja
Copy link

brazelja commented Aug 6, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Code of Conduct

  • I agree to follow this project's Code of Conduct

Code Sandbox link

No response

Bug report

Recently found this library after struggling with groq typing forever and so far I'm loving it!

During use I found a bug, I assume, that is related to what happens to image data between the sanity client returning and the groq-builder query returning.

Given the following schema:

const Grid = defineType({
  name: 'grid',
  title: 'Grid',
  type: 'object',
  fields: [
    defineField({
      name: 'items',
      title: 'Items',
      type: 'array',
      of: [
        defineArrayMember({ type: 'feature' }), // Custom object
        defineArrayMember({
          // image type in question
          name: 'image',
          title: 'Image',
          icon: ImageIcon,
          type: 'image',
          options: { hotspot: true },
          fields: [
            defineField({
              name: 'alt',
              type: 'string',
              title: 'Alternative text',
            }),
          ],
        }),
      ],
      validation: (rule) => rule.required(),
    }),
  ],
});

And the following query builder:

const GRID_FRAGMENT = q.fragment<Grid>().project((p) => ({
  _type: z.literal('grid'),
  items: p.field('items[]').project((p) => ({
    _key: z.string(),
    ...p.conditionalByType({
      feature: FEATURE_FRAGMENT, // Working fragment
      image: { asset: true, alt: true }, // Image projection
    }),
  })),
}));

const QUERY = q
  .parameters<{ slug: string }>()
  .star.filterByType('page')
  .filterBy('slug.current == $slug')
  .slice(0)
  .project((p) => ({
    content: p.field('content[]').project((p) => ({
      _key: z.string(),
      ...p.conditionalByType({
        grid: GRID_FRAGMENT, // Here is where the grid schema/fragment is referenced
      }),
    })),
  }));

const page = await runQuery(QUERY, { parameters: { slug: '...' } });

I receive the following back from the sanity client (raw):

{
  "content": [
    {
      "_key": "0959152d2389",
      "_type": "grid",
      "items": [
        {
          "_key": "cf3b123386aa",
          "_type": "image",
          "asset": {
            "_ref": "image-339bb1bb3088ece60f868b4b58a7aa63afde2425-1912x913-png",
            "_type": "reference"
          },
          "alt": "..."
        },
        {
          "_key": "f3ba7c857962",
          "_type": "heading",
          .
          .
          .
        }
      ]
    },
    .
    .
    .
  ]
}

And this back from runQuery (groq-builder)

{
  "content": [
    {
      "_key": "0959152d2389",
      "_type": "grid",
      "items": [
        {
          "_key": "cf3b123386aa",
          "_type": "image" // Where did asset and alt go?
        },
        {
          "_key": "f3ba7c857962",
          "_type": "heading",
          .
          .
          .
        }
      ]
    },
    .
    .
    .
  ]
}

As you can see, the reult from runQuery removed the asset and alt properties of the image type from the value originally returned from the sanity client.

Using InferResultType to get the return type of the query appears to show the correct types, including the asset and alt properties.

Interestingly though, if I replace

image: { asset: true, alt: true }

with

image: { asset: true, alt: z.string() }

then asset and alt are properly inlcuded in the query result, which leads me to believe there is something going on with the parsing logic in between the client return and the query return.

So far I have only run into this issue when projecting image properties inside of a p.conditionalByType({...}) call.

@brazelja brazelja added the bug Something isn't working label Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant