Joint session with Vercel: How to build intelligent storefronts (May 15th)

Fetch the draft or published version of a document

One query to fetch the draft it it exists, and fall back to the published version of a document.

By Daniel Favand


GROQ query

coalesce(
  *[_id == 'drafts.' + $documentId][0], 
  *[_id == $documentId][0]
)

This query uses the coalesce GROQ function to resolve the draft version of a document, and if it doesn't exist, fall back to the published version.

Be sure to supply a documentId as a parameter for the query.

Contributor

Daniel Favand

Solution Engineer at Sanity.io, helping clients build great content experiences.

Visit Daniel Favand's profile