Join our next Developer Virtual Meetup – Live Q&A, Project Showcase, Swag raffle, and more 🔥

Select draft OR published document

By Formidable

Setting up a live preview and want to fetch a document's draft, with a fallback for the published document when there are no drafts? Check this out.

Draft with published document fallback

*[_type == 'pokemon' && slug.current == "bulbasaur"]
  | score(_id in path("drafts.**"))
  | order(_score desc)[0]

When building out live previews, you generally want to fetch draft documents. However, if there are no drafts available for the document you're editing – you want to fall back to the currently-published version of the document.

This query is an example of how to do this using GROQ's `score` and `order` functions.

Contributor