Sanity Pioneers: Get early access to betas, extra AI credits, and a direct line to the engineering team. Apply now

Can the results of a query in documentList be reused inside the function?

3 repliesLast updated: Nov 30, 2025

Can the results of a query in
documentList
be reused inside the function? For example, I’d like the title here to say like “13 Properties with Inspection Times” or whatever the count of the query is…

S.documentList()
  .schemaType('property')
  .title('Properties with Inspection Times')
  .filter(
      '_type == "property" && count(inspectionTimes) > 0'
  )
AI Update

Looking at the Structure Builder API, yes - you can query for the count and use it in your document list title! The context parameter passed to your structure function includes a getClient method that returns a configured Sanity client.

Here's how to make your title dynamic with the query count:

// sanity.config.js
import {structureTool} from 'sanity/structure'

export default defineConfig({
  // ... other config
  plugins: [
    structureTool({
      structure: async (S, context) => {
        // Get a configured Sanity client from context
        const client = context.getClient({apiVersion: '2023-01-01'})
        
        // Fetch the count
        const count = await client.fetch(
          'count(*[_type == "property" && count(inspectionTimes) > 0])'
        )
        
        return S.list()
          .title('Content')
          .items([
            S.documentList()
              .schemaType('property')
              .title(`${count} Properties with Inspection Times`)
              .filter('_type == "property" && count(inspectionTimes) > 0')
          ])
      }
    })
  ]
})

The key points from the Structure Builder API Reference:

You can also make this more reusable with a list item:

structure: async (S, context) => {
  const client = context.getClient({apiVersion: '2023-01-01'})
  
  const propertiesWithInspections = await client.fetch(
    'count(*[_type == "property" && count(inspectionTimes) > 0])'
  )
  
  return S.list()
    .title('Content')
    .items([
      S.listItem()
        .title(`${propertiesWithInspections} Properties with Inspection Times`)
        .child(
          S.documentList()
            .title(`${propertiesWithInspections} Properties with Inspection Times`)
            .schemaType('property')
            .filter('_type == "property" && count(inspectionTimes) > 0')
        ),
      // ... other items
    ])
}

Important notes:

Show original thread
3 replies

Was this answer helpful?

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.

Related contributions