Skip to content

Commit b86e3d0

Browse files
authored
fix(typegen): find queries imported with defineQuery from next-sanity (#7391)
1 parent e3a02c4 commit b86e3d0

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

packages/@sanity/codegen/src/typescript/__tests__/findQueriesInSource.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -363,4 +363,29 @@ describe('findQueries with defineQuery', () => {
363363
const queries = findQueriesInSource(source, __filename, undefined)
364364
expect(queries.length).toBe(0)
365365
})
366+
367+
test('can import from next-sanity', () => {
368+
const source = `
369+
import { defineQuery } from "next-sanity";
370+
const postQuery = defineQuery("*[_type == 'author']");
371+
const res = sanity.fetch(postQuery);
372+
`
373+
374+
const queries = findQueriesInSource(source, 'test.ts')
375+
expect(queries.length).toBe(1)
376+
const queryResult = queries[0]
377+
378+
expect(queryResult?.result).toEqual("*[_type == 'author']")
379+
})
380+
381+
test('wont import from other package names', () => {
382+
const source = `
383+
import { defineQuery } from "other";
384+
const postQuery = defineQuery("*[_type == 'author']");
385+
const res = sanity.fetch(postQuery);
386+
`
387+
388+
const queries = findQueriesInSource(source, 'test.ts')
389+
expect(queries.length).toBe(0)
390+
})
366391
})

packages/@sanity/codegen/src/typescript/findQueriesInSource.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const require = createRequire(__filename)
1313
const groqTagName = 'groq'
1414
const defineQueryFunctionName = 'defineQuery'
1515
const groqModuleName = 'groq'
16+
const nextSanityModuleName = 'next-sanity'
1617

1718
const ignoreValue = '@sanity-typegen-ignore'
1819

@@ -52,7 +53,8 @@ export function findQueriesInSource(
5253
// Look for strings wrapped in a defineQuery function call
5354
const isDefineQueryCall =
5455
babelTypes.isCallExpression(init) &&
55-
isImportFrom(groqModuleName, defineQueryFunctionName, scope, init.callee)
56+
(isImportFrom(groqModuleName, defineQueryFunctionName, scope, init.callee) ||
57+
isImportFrom(nextSanityModuleName, defineQueryFunctionName, scope, init.callee))
5658

5759
if (babelTypes.isIdentifier(node.id) && (isGroqTemplateTag || isDefineQueryCall)) {
5860
// If we find a comment leading the decleration which macthes with ignoreValue we don't add

0 commit comments

Comments
 (0)