Join us live Sept. 27 – How Sanity and Vercel powered Morning Brew's transformation –>

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

3 replies
Last updated: Sep 19, 2020

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'
  )

Sep 19, 2020, 7:34 AM

That’s not possible at the moment. But since you can run promises/observables, you can do the query in the structure builder… structure and use the data where you need it.
Here’s an example where we fetch counts in the icons:
https://sanity-io.slack.com/archives/CGKNRPC80/p1593460636120200?thread_ts=1593460050.120000&cid=CGKNRPC80
And another where we use the documentstore to list out tickets by their tags (that can be adapted to your use case):
https://github.com/sanity-io/community-studio/blob/master/deskStructure.js#L143

Sep 19, 2020, 9:11 AM

That’s not possible at the moment. But since you can run promises/observables, you can do the query in the structure builder… structure and use the data where you need it.
Here’s an example where we fetch counts in the icons:
https://sanity-io.slack.com/archives/CGKNRPC80/p1593460636120200?thread_ts=1593460050.120000&cid=CGKNRPC80
And another where we use the documentstore to list out tickets by their tags (that can be adapted to your use case):
https://github.com/sanity-io/community-studio/blob/master/deskStructure.js#L143

Sep 19, 2020, 9:11 AM

Okay cool. Yeah after posting I figured maybe I could do the query elsewhere and pass in the results that way. These code examples help, thanks!

Sep 19, 2020, 9:13 AM

Sanity.io: Get the most out of your content

Sanity.io is a platform to build websites and applications. It comes with great APIs that let you treat content like data. Give your team exactly what they need to edit and publish their content with the customizable Sanity Studio. Get real-time collaboration out of the box. Sanity.io comes with a hosted datastore for JSON documents, query languages like GROQ and GraphQL, CDNs, on-demand asset transformations, presentation agnostic rich text, plugins, and much more.

Don't compromise on developer experience. Join thousands of developers and trusted companies and power your content with Sanity.io. Free to get started, pay-as-you-go on all plans.