sanity-io / vscode-sanity

Visual Studio Code extension for developing applications powered by Sanity.io
MIT License
51 stars 8 forks source link

Query preview doesn't work with a concatenated template #10

Open paulpopus opened 2 years ago

paulpopus commented 2 years ago

When concatenating another string into the query, the preview isn't able to handle this and throws an error instead:

Example code:

`*[_type == 'blog'] {
  _id,
  _type,
  title,
  'published_at': publishedAt,
  'slug': slug.current,
  'hero_image': ${ImageQuery},
}`

This issue is consistent when using groq as a function:

groq`*[_type == 'blog'] {
  _id,
  _type,
  title,
  'published_at': publishedAt,
  'slug': slug.current,
  'hero_image': ${ImageQuery},
}`

And variations of comments so far in my testing.

The image query being another completely valid query template literal string, I assume this is to do with the syntax ${} conflicting with the query object syntax but not entirely sure.

The error is a syntax error image

I tried escaping this as well but that didn't work.

Why this would be useful:

String concatenation is useful for re-using object queries where you have multiple references or objects that are going to be the same, in this example we always want to project the image asset in the same way across every document.

What should happen:

String is concatenated into the query without any issues.

Note that you can run these queries just fine with the fetch client, it just affects this extension's preview.

mthomes commented 5 months ago

This would be huge. Currently I can only execute my simple queries, but many of them are built with partial imports for reuse.

runeb commented 2 weeks ago

Thanks for reporting. We recently released a new version of this extension. Could you verify if your issue is fixed by upgrading?

mthomes commented 2 weeks ago

@runeb I'm still seeing this error on 0.2.0.

Example code:

const image = groq`
"image": image{
  ...,
  "asset": asset->,
}
`;

const CONTACT_US_PAGE = groq`
*[_id == 'contactUsPage'][0]{
  ...,
  ${image}
}`;