How to return updated values from a Sanity patch mutation
Based on the Sanity documentation, the issue you're experiencing is common when performing patch mutations. By default, patch mutations only return the document ID, not the updated document content. To get the updated values immediately after your patch, you need to use the returnDocuments parameter.
Solution: Use returnDocuments=true
When making your mutation request, add returnDocuments=true as a query parameter. This tells Sanity to return the full content of changed documents in the response.
With your current axios approach:
axios.post(
`https://${projectId}.api.sanity.io/${apiVersion}/data/mutate/${dataset}?returnDocuments=true`,
{
mutations: [
{
patch: {
id: 'your-blog-id',
set: { comments: updatedComments }
}
}
]
}
)
.then((response) => {
// The updated document is now directly in the response
const updatedDoc = response.data.results[0].document;
console.log(updatedDoc.comments);
return res.status(201).send({
message: 'CommentUpdated',
comments: updatedDoc.comments
});
})
.catch(function (error) {
console.log(error.message);
});Using the Sanity JS Client (recommended):
If you're using the official Sanity JavaScript client, you can use .commit() with options:
client
.patch(documentId)
.set({ /* your changes */ })
.commit({ returnDocuments: true })
.then((updatedDoc) => {
console.log(updatedDoc.comments);
return res.status(201).send({
message: 'CommentUpdated',
comments: updatedDoc.comments
});
});Why your current approach doesn't work
When you query the document immediately after patching (without returnDocuments), you're experiencing a race condition. Even though the mutation has been committed, the query might execute before Sanity's real-time index is fully updated, returning the old values instead of the new ones.
Additional helpful options
According to the Mutation API documentation:
returnDocuments=true- Returns the entire content of changed documentsreturnIds=true- Returns only the IDs (default behavior)visibility=sync- Ensures changes are visible to queries before returning (default)visibility=async- Returns immediately after commit (faster, but subsequent queries might not see changes immediately)
By using returnDocuments=true, you get the freshly updated document directly from the mutation response, completely avoiding timing issues with subsequent queries.
Show original thread5 replies
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.