
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeI can see the issue with your query. The syntax you're using (_id == "frontpage" => { }) is conditional projection syntax, not a way to fetch multiple documents with different shapes. Let me explain the correct approach.
Your query syntax is invalid because you're trying to use conditional projections incorrectly. The => operator in projections is meant to conditionally add fields to an object, not to create entirely different object structures based on filters.
To fetch multiple documents with different structures in one query, you have a few options:
The cleanest approach is to make two separate queries. In Next.js, you can fetch them in parallel:
// In your Next.js component or page
const [frontpage, siteSettings] = await Promise.all([
client.fetch(`*[_id == "frontpage"][0]{ _id, pageDescription, pageTitle, pageBuilder, mainImage }`),
client.fetch(`*[_id == "siteSettings"][0]{ title, description, primaryColor, secondaryColor, keywords }`)
])Fetch both documents and project all fields, then handle the different structures in your application:
export const frontPageSettings = `
*[_id in ["frontpage", "siteSettings"]]{
_id,
// frontpage fields
pageDescription,
pageTitle,
pageBuilder,
mainImage,
// siteSettings fields
title,
description,
primaryColor,
secondaryColor,
keywords
}
`Then in your code, separate them:
const data = await client.fetch(frontPageSettings)
const frontpage = data.find(d => d._id === 'frontpage')
const siteSettings = data.find(d => d._id === 'siteSettings')If you want to use conditional projections, the syntax needs to be different. The condition goes inside the projection to conditionally add fields:
export const frontPageSettings = `
*[_id in ["frontpage", "siteSettings"]]{
_id,
_id == "frontpage" => {
pageDescription,
pageTitle,
pageBuilder,
mainImage
},
_id == "siteSettings" => {
title,
description,
primaryColor,
secondaryColor,
keywords
}
}
`This will give you objects where frontpage documents have their specific fields and siteSettings documents have theirs, but they'll all have _id at the root level.
According to the GROQ pipeline components documentation, the condition => {} syntax expands the object on the right-hand side into the projection if the condition is true. It doesn't replace the entire projection - it adds to it. Your syntax was trying to use it as a filter replacement, which isn't valid.
For your use case with Next.js, Option 1 (separate queries with Promise.all) is the cleanest and most maintainable approach. It's explicit about what you're fetching, easier to type with TypeScript, and the performance difference is negligible since the queries run in parallel.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store