Skip to content

Commit

Permalink
fix(typegen): find queries imported with defineQuery from next-sanity (
Browse files Browse the repository at this point in the history
  • Loading branch information
sgulseth authored Aug 20, 2024
1 parent e3a02c4 commit b86e3d0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,29 @@ describe('findQueries with defineQuery', () => {
const queries = findQueriesInSource(source, __filename, undefined)
expect(queries.length).toBe(0)
})

test('can import from next-sanity', () => {
const source = `
import { defineQuery } from "next-sanity";
const postQuery = defineQuery("*[_type == 'author']");
const res = sanity.fetch(postQuery);
`

const queries = findQueriesInSource(source, 'test.ts')
expect(queries.length).toBe(1)
const queryResult = queries[0]

expect(queryResult?.result).toEqual("*[_type == 'author']")
})

test('wont import from other package names', () => {
const source = `
import { defineQuery } from "other";
const postQuery = defineQuery("*[_type == 'author']");
const res = sanity.fetch(postQuery);
`

const queries = findQueriesInSource(source, 'test.ts')
expect(queries.length).toBe(0)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const require = createRequire(__filename)
const groqTagName = 'groq'
const defineQueryFunctionName = 'defineQuery'
const groqModuleName = 'groq'
const nextSanityModuleName = 'next-sanity'

const ignoreValue = '@sanity-typegen-ignore'

Expand Down Expand Up @@ -52,7 +53,8 @@ export function findQueriesInSource(
// Look for strings wrapped in a defineQuery function call
const isDefineQueryCall =
babelTypes.isCallExpression(init) &&
isImportFrom(groqModuleName, defineQueryFunctionName, scope, init.callee)
(isImportFrom(groqModuleName, defineQueryFunctionName, scope, init.callee) ||
isImportFrom(nextSanityModuleName, defineQueryFunctionName, scope, init.callee))

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

0 comments on commit b86e3d0

Please sign in to comment.