Skip to content

Instantly share code, notes, and snippets.

@usulpro
Last active January 9, 2022 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save usulpro/f1812b436fdec496adf713f220d52d86 to your computer and use it in GitHub Desktop.
Save usulpro/f1812b436fdec496adf713f220d52d86 to your computer and use it in GitHub Desktop.
Can’t you still have references to your draft documents in Sanity CMS? Setting up a full-fledged preview environment for your content
* [_type == "post"] {
...,
author->
}
* [_type == "post"] {
...,
"author": * [_id == ("drafts." + ^.author._ref)][0],
}
* [_type == "post"] {
...,
"author": coalesce( * [_id == ("drafts." + ^.author._ref)][0], author->)
}
{
"type": "post"
}
{
"docs": * [_type == $type]
}
{
"drafts": @.docs [_id in path("drafts.**")],
"published": @.docs [!(_id in path("drafts.**"))],
"coupled": @.docs [!(_id in path("drafts.**"))]{"published": {...}, "draft": ^.docs [_id == "drafts." + ^._id][0]},
}
{
"allCoupled": [...@.coupled, ...@.drafts [!(@._id in ^.published[]{"_id": ("drafts." + _id)}._id)]{"draft": {...}}],
}
{
"selected": @.allCoupled[]{
...coalesce(@.draft, @.published)
},
}.selected[] {
...,
"author": coalesce( * [_id == ("drafts." + ^.author._ref)][0], author->)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment