sanity-io / sanity

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

CLI Typegen doesn't work with imported partial queries #6297

Closed markomitranic closed 4 months ago

markomitranic commented 5 months ago

Describe the bug

When using groq typegen sanity schema extract && sanity typegen generate it is perfectly able to work with queries that are composed out of concatenated string variables. It, however doesn't work, crashes, if any of the variables are imported from another file.

To Reproduce

  1. Create 2 files - repo.ts and util.ts
  2. Place the following query into the repo.ts:

    import { inner } from "./util";
    
    export function marko() {
       const myTestQuery = groq`*[_type == "sanity.imageAsset"] ${inner}`;
       return client.fetch(myTestQuery);
    }
  3. Place the following query segment into the util.ts file:

    export const inner = `{ metadata }`;
  4. Run the typegen command: sanity schema extract && sanity typegen generate
  5. It will crash with an error, saying that it ✖ Cannot find module './util'

Expected behavior

It should behave the same as it does when the inner variable is within the same file as the query - successfully compose the query and its type.

Which versions of Sanity are you using?

@sanity/cli (global) 3.37.1 (up to date)
@sanity/document-internationalization 3.0.0 (up to date)
@sanity/icons 2.11.8 (up to date)
@sanity/image-url 1.0.2 (up to date)
@sanity/presentation 1.12.3 (up to date)
@sanity/ui 2.1.1 (up to date)
@sanity/vision 3.37.1 (up to date)
@sanity/webhook 4.0.3 (up to date)
sanity 3.37.1 (up to date)

What operating system are you using?

macOS Sonoma

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

10.2.4
v20.11.1
hb20007 commented 5 months ago

I am facing a similar issue to this, but the error message I get is:

✖ Could not find binding for import "useLocale" in next-intl in "../src/app/[locale]/trails/page.tsx"

This is the relevant code from my ../src/app/[locale]/trails/page.tsx file:

import { groq } from 'next-sanity';
import { useLocale } from 'next-intl';

export default async function Trails() {
  const locale = useLocale();
  const TRAILS_QUERY = groq`*[_type == "trail" && language == "${locale}"]{
    _id,
    name
  }`;

I need the locale parameter to be dynamic based on the user's locale, so this issue is blocking me from using TypeGen in my project.

markomitranic commented 5 months ago

I am facing a similar issue to this, but the error message I get is:

✖ Could not find binding for import "useLocale" in next-intl in "../src/app/[locale]/trails/page.tsx"

This is the relevant code from my ../src/app/[locale]/trails/page.tsx file:

import { groq } from 'next-sanity';
import { useLocale } from 'next-intl';

export default async function Trails() {
  const locale = useLocale();
  const TRAILS_QUERY = groq`*[_type == "trail" && language == "${locale}"]{
    _id,
    name
  }`;

I need the locale parameter to be dynamic based on the user's locale, so this issue is blocking me from using TypeGen in my project.

Hey, in your case that is a different issue, you should be using prepared queries, so you would not be concatenating strings at all, you would send the locale param as a second argument

example


sanityClient.fetch(
  `*[_type == "orders" && orderId.current == $orderId][0]{...}`,
  {orderId: query.orderId}
)
hb20007 commented 5 months ago

I am facing a similar issue to this, but the error message I get is: ✖ Could not find binding for import "useLocale" in next-intl in "../src/app/[locale]/trails/page.tsx" This is the relevant code from my ../src/app/[locale]/trails/page.tsx file:

import { groq } from 'next-sanity';
import { useLocale } from 'next-intl';

export default async function Trails() {
  const locale = useLocale();
  const TRAILS_QUERY = groq`*[_type == "trail" && language == "${locale}"]{
    _id,
    name
  }`;

I need the locale parameter to be dynamic based on the user's locale, so this issue is blocking me from using TypeGen in my project.

Hey, in your case that is a different issue, you should be using prepared queries, so you would not be concatenating strings at all, you would send the locale param as a second argument

example

sanityClient.fetch(
  `*[_type == "orders" && orderId.current == $orderId][0]{...}`,
  {orderId: query.orderId}
)

Thanks, and it's working for me now. Concatenation was working for me to get the data so I assumed that the issue was with sanity typegen. Now only the original issue which you reported remains.

georgeosutton commented 4 months ago

6457 Does not seems to fix all cases still getting Cannot find module if I use relative imports. Seems to happen when I have a fragments composed of other fragments.

sgulseth commented 4 months ago

@georgeosutton Do you have a reproduction either in a repo or a gist somewhere?

georgeosutton commented 4 months ago

@sgulseth reproduction here

I've added a folder with relevant fragments / query.

I get the following when I run pnpm run typegen

Cannot find module 'C:\Users\George\Documents\GitHub\typegen-reproduction\fragments\image'
Require stack:
- C:\Users\George\Documents\GitHub\typegen-reproduction\node_modules\.pnpm\@sanity+codegen@3.40.0\node_modules\@sanity\codegen\lib\index.js   
- C:\Users\George\Documents\GitHub\typegen-reproduction\node_modules\.pnpm\@sanity+cli@3.40.0\node_modules\@sanity\cli\lib\workers\typegenGenerate.js in "./src/sanity/lib/queries.ts"
sgulseth commented 4 months ago

Thanks a lot! I have a fix lined up here https://github.com/sanity-io/sanity/pull/6540

sgulseth commented 4 months ago

fix for this was released yesterday, v3.41.0

github-actions[bot] commented 1 month ago

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.