Closed markomitranic closed 4 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.
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}
)
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.
Cannot find module
if I use relative imports. Seems to happen when I have a fragments composed of other fragments.@georgeosutton Do you have a reproduction either in a repo or a gist somewhere?
@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"
Thanks a lot! I have a fix lined up here https://github.com/sanity-io/sanity/pull/6540
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.
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
repo.ts
andutil.ts
Place the following query into the
repo.ts
:Place the following query segment into the
util.ts
file:sanity schema extract && sanity typegen generate
✖ 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?
What operating system are you using?
macOS Sonoma
Which versions of Node.js / npm are you running?