👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Understanding how to patch drafts in Sanity.io

13 replies
Last updated: Aug 20, 2021
Hi guys, I something that I'm struggling to understand, I think it's more of a conceptual thing with how Sanity treats drafts. I'm trying to sync documents from an external API, and it's working great using a Netlify function. In this case it's categories that come from a custom portal that the business uses, and will each sync over to sanity as documents whenever they are created / edited / deleted. I've got this working and it's super quick and great. However, I'm adding a few extra fields to each document within Sanity, for example a category description. I've noticed that if I have unpublished changes from within Sanity on a category, and I make changes in the external portal, it doesn't sync. I believe this is to do with the
_id
changing to
drafts.xxxxx
when there's a draft, so the patch I'm trying to run on the document doesn't do anything, as it's being patched to the regular
_id
, not the temporary draft
_id
. My question is, what's the best way to patch to a draft? Is it possible? I've looked high and low through the docs but I just can't see it mentioned. Below is the sync function for reference. Thanks very much for taking the time to read this.
Aug 19, 2021, 8:34 AM
Hey User! I have an idea of how to go about solving this. Just need to confirm and then I’ll update here.
Aug 19, 2021, 6:15 PM
user M
Thanks User!
Aug 19, 2021, 9:05 PM
user M
Thanks User!
Aug 19, 2021, 9:05 PM
OK, the important thing to understand about drafts is that they are saved in a separate document prefixed with
drafts.
and can be filtered using
*[(_id in path('drafts.**'))]
.
I think the way forward is to add logic to check if a draft exists, then patch that document if it
does or the published document if it doesn’t.
There are a few things you’ll need for this to work:

First, drafts cannot appear on APIs if the user accessing them is unauthenticated. Your client config already has a
token
, so you should already be able to do this without any changes.
Second, you’ll need to implement the logic. A coworker pointed out that this
isUniqueAcrossAllDocuments function might be a good starting point to get a draft if there is one, but to otherwise get the published document (just remove the unnecessary slug stuff).
Aug 19, 2021, 10:51 PM
Ah thanks
user M
this makes sense. I actually tried this approach, maybe my logic was off, but it just cleared every field when I added
drafts.
to the id. I was wondering if this is because I wasn't specifying a
_rev
? I noticed when Sanity makes a draft automatically it always has a
_rev
attached to it?
Aug 19, 2021, 10:54 PM
Ah, interesting. Do you mind sharing the code you used for this approach?
Aug 19, 2021, 11:09 PM
Yep, all I did really, as a bit of a hacky test, was to have some changes sitting on a document within sanity (so I knew there was a
<http://drafts.xxx|drafts.xxx>
version that existed) and tried patching to it with the code attached. Same as my previous code, just with
drafts.
added to the id. So this code doesn't actually check whether a draft exists, that was going to be my next move once I checked that this worked, but for some reason this just clears all fields.
Aug 19, 2021, 11:13 PM
user M
Sorry, I described this slightly incorrectly, running the sync function when there is a
<http://drafts.xxx|drafts.xxx>
version that exists doesn't clear the document, it just doesn't update anything. This code clears the document if there is no
<http://drafts.xxx|drafts.xxx>
present, but I believe that should be fixable once I add some logic in to check for a draft version.
Aug 19, 2021, 11:29 PM
Got it! I think I’ll be able to solve this one, but it will take me some time to replicate it/debug. I’ll update here once I’ve gotten it!
Aug 19, 2021, 11:36 PM
No worries, sorry it's a bit of a funny one. Will make a big difference to how I handle all my syncs and publish statuses in future projects though I think! Thanks for your help !!! :)
Aug 19, 2021, 11:37 PM
Ah! I think I might have found the issue! Hold on let me test, might save you some time!
Aug 19, 2021, 11:45 PM
user M
Ok I've got it, there was an error in my hacky trial. I was still trying to patch the 'draft' category content to the published category id. I think I can figure out the rest from here. Thanks very much for your help, it was super helpful to be able to check my understanding of how drafts are managed.
Aug 19, 2021, 11:54 PM
Awesome! I’m so glad you got it sorted out. Come back if you run into any more issues.
Aug 20, 2021, 12:08 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?