sanity-io / sanity

Sanity Studio – Rapidly configure content workspaces powered by structured content
https://www.sanity.io
MIT License
5.25k stars 427 forks source link

clientless patches fail #3141

Closed bagrounds closed 1 year ago

bagrounds commented 2 years ago

Describe the bug

From the documentation on the npm package page:

Clientless patches & transactions Transactions and patches can also be built outside the scope of a client:


const sanityClient = require('@sanity/client')
const client = sanityClient({
projectId: 'your-project-id',
dataset: 'bikeshop',
})

// Patches: const patch = new sanityClient.Patch('') client.mutate(patch.inc({count: 1}).unset(['visits']))



But this results in an HTTP 400, with a complaint about unknown keys.

After investigating a bit, it appears that when `patch.commit()` is called, the patch is wrapped in an object with a patch property before passing it to mutate, but the equivalent does not happen when passing a patch object to the mutate method externally.

Because the error message included the JSON body sent by the client, I was able to verify that it also fails when curling the REST API directly, but it succeeds if we wrap the patch operations in the `{ patch: ... }` object.

As a workaround, I'm able to pass the client into the `Patch` constructor and call commit after building up the appropriate operations.

Should be a very easy fix.

**To Reproduce**

Steps to reproduce the behavior:

Construct a patch object and pass it to `client.mutate`

**Expected behavior**

the patch succceeds
kmelve commented 1 year ago

Hi!

We are currently working on improving our workflows and follow-up on our open GitHub repository. In that work, we have decided to close most issues older than the release of Sanity Studio v3.

We value your feedback, so if this issue is still important to you and relevant for Sanity Studio v3, please search for relevant open issues. If you can’t find any, open a new one and link to relevant comments in this thread. For questions about how to do something, please post them in the slack community.