Resolving query recognition issues in Sanity project setup

3 replies
Last updated: Jan 13, 2025
I've also now tried moving the query into the sanity folder itself (
src/sanity/queries.ts
) and it's still not recognizing it
AI Update

Based on your issue, it sounds like you're having trouble with TypeScript recognizing your GROQ queries even after moving them to src/sanity/queries.ts. Here are the most common reasons this happens and how to fix them:

Make Sure You're Using defineQuery

Your queries need to be wrapped with the defineQuery helper function for TypeGen to recognize them:

// src/sanity/queries.ts
import { defineQuery } from 'next-sanity' // or from 'groq' if not using Next.js

export const EVENTS_QUERY = defineQuery(`
  *[_type == "event"]{
    _id, 
    name, 
    slug
  }
`)

Update Your TypeGen Configuration

If your queries are in a subfolder, you need to configure TypeGen to look there. Create or update sanity-typegen.json in your Studio folder:

{
  "path": "../src/**/*.{ts,tsx,js,jsx}",
  "schema": "./schema.json",
  "generates": "../src/sanity/types.ts"
}

The path field tells TypeGen where to search for queries using defineQuery.

Run the Generation Commands

After setting up your queries properly, you need to run both commands from your Studio folder:

# 1. Extract your schema
npx sanity@latest schema extract

# 2. Generate types
npx sanity@latest typegen generate

The typegen workflow requires both steps - first extracting the schema, then generating types from both the schema and your queries.

Common Issues

Query variable names must be unique: The variable name (like EVENTS_QUERY) becomes the type name, so make sure each query has a unique name across your entire project.

Check your import: Make sure you're importing from the right package - next-sanity if you're using Next.js, or groq for other frameworks.

File extensions: Ensure your path configuration includes the right file extensions (.ts, .tsx, etc.) where your queries are located.

Wrong directory: Make sure you're running the typegen commands from your Sanity Studio directory (where sanity.config.ts lives), not from your app's root.

After making these changes, run the typegen command again and you should see output like:

βœ… Generated TypeScript types for 14 schema types and 2 GROQ queries...

If you're still having issues, double-check that your queries are syntactically valid GROQ and that they're assigned to variables (not inline).

Do you mind to try moving the queries to the
/src
directory? I think we have a couple of default paths we search for. Let me see if I can find the code.
Yap that did itI've made a
sanity-typegen.json
to point it to the correct location of my files
Thanks for the the tip ^^!
awesome! Sorry I didn't get the link to the code handy, but as you found you can customize the paths in the typegen json πŸ™‚

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?