Skip to content

Commit

Permalink
fix(mutate): serialize clientless patches correctly (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored Apr 3, 2023
1 parent 158e6b8 commit b635dff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/data/dataMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,14 @@ export function _mutate<R extends Record<string, Any>>(
): Observable<
SanityDocument<R> | SanityDocument<R>[] | SingleMutationResult | MultipleMutationResult
> {
const mut =
mutations instanceof Patch ||
mutations instanceof ObservablePatch ||
mutations instanceof Transaction ||
mutations instanceof ObservableTransaction
? mutations.serialize()
: mutations
let mut: Mutation | Mutation[]
if (mutations instanceof Patch || mutations instanceof ObservablePatch) {
mut = {patch: mutations.serialize()}
} else if (mutations instanceof Transaction || mutations instanceof ObservableTransaction) {
mut = mutations.serialize()
} else {
mut = mutations
}

const muts = Array.isArray(mut) ? mut : [mut]
const transactionId = options && (options as Any).transactionId
Expand Down
13 changes: 13 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,19 @@ describe('client', async () => {
expect(() => patch.dec({bar: 2}).commit()).toThrow(/client.*mutate/i)
})

test.skipIf(isEdge)('patch can be created without client and passed to mutate()', async () => {
const patch = new Patch('foo').dec({count: 1})

const mutations = [{patch: {id: 'foo', dec: {count: 1}}}]
nock(projectHost())
.post('/v1/data/mutate/foo?returnIds=true&returnDocuments=true&visibility=sync', {
mutations,
})
.reply(200, {results: [{id: 'foo', operation: 'update'}]})

await expect(getClient().mutate(patch)).resolves.not.toThrow()
})

// eslint-disable-next-line no-warning-comments
// @TODO investigate why this fails on Edge Runtime
test.skipIf(isEdge)('can manually call clone on patch', () => {
Expand Down

0 comments on commit b635dff

Please sign in to comment.