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

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

Closed
markomitranic opened this issue Apr 9, 2024 · 8 comments · Fixed by #6457
Closed

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

markomitranic opened this issue Apr 9, 2024 · 8 comments · Fixed by #6457
Assignees
Labels
bug typegen Issues related to TypeScript types generation

Comments

@markomitranic
Copy link

markomitranic commented Apr 9, 2024

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
@bjoerge bjoerge added the typegen Issues related to TypeScript types generation label Apr 10, 2024
@bobinska-dev bobinska-dev added the Feature label Apr 11, 2024 — with Linear
@hb20007
Copy link

hb20007 commented Apr 12, 2024

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
Copy link
Author

markomitranic commented Apr 12, 2024

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
Copy link

hb20007 commented Apr 12, 2024

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
Copy link

#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
Copy link
Member

sgulseth commented May 1, 2024

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

@georgeosutton
Copy link

georgeosutton commented May 1, 2024

@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
Copy link
Member

sgulseth commented May 1, 2024

Thanks a lot! I have a fix lined up here #6540

@sgulseth
Copy link
Member

sgulseth commented May 8, 2024

fix for this was released yesterday, v3.41.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug typegen Issues related to TypeScript types generation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants