Skip to content

Commit 5ec8cba

Browse files
committed
FIX #1812 Updates to documents fail with GraphQL replication
1 parent 238392d commit 5ec8cba

File tree

5 files changed

+85
-11
lines changed

5 files changed

+85
-11
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
Other:
66
- Added a new example for electron with the remote API. Thanks [@SebastienWae](https://github.com/SebastienWae)
77

8+
Bugfixes:
9+
- Updates to documents fail with GraphQL replication. [#1812](https://github.com/pubkey/rxdb/issues/1812). Thanks [@gautambt](https://github.com/gautambt)
10+
811
### 8.7.4 (2 December 2019)
912

1013
Other:

examples/graphql/client/index.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ RxDB.plugin(RxDBErrorMessagesModule);
1919
import RxDBValidateModule from 'rxdb/plugins/validate';
2020
RxDB.plugin(RxDBValidateModule);
2121

22+
import UpdatePlugin from 'rxdb/plugins/update';
23+
RxDB.plugin(UpdatePlugin);
24+
2225
import {
2326
GRAPHQL_PORT,
2427
GRAPHQL_PATH,
@@ -188,13 +191,14 @@ async function run() {
188191
}`;
189192
const ret = wsClient.request({ query });
190193
ret.subscribe({
191-
next(data) {
192-
console.log('subscription emitted => trigger run');
194+
next: async (data) => {
195+
console.log('subscription emitted => trigger run()');
193196
console.dir(data);
194-
replicationState.run();
197+
await replicationState.run();
198+
console.log('run() done');
195199
},
196200
error(error) {
197-
console.log('got error:');
201+
console.log('run() got error:');
198202
console.dir(error);
199203
}
200204
});

src/plugins/replication-graphql/crawling-checkpoint.ts

-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import {
3030
// things for the push-checkpoint
3131
//
3232

33-
3433
const pushSequenceId = (endpointHash: string) => LOCAL_PREFIX + PLUGIN_IDENT + '-push-checkpoint-' + endpointHash;
3534

3635
/**
@@ -137,8 +136,6 @@ export async function getChangesSinceLastPushSequence(
137136
}
138137

139138

140-
141-
142139
//
143140
// things for pull-checkpoint
144141
//

src/plugins/replication-graphql/index.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,10 @@ export class RxGraphQLReplicationState {
354354
await this.collection.pouch.bulkDocs(
355355
[
356356
toPouch
357-
], {
358-
new_edits: false
359-
}
357+
],
358+
{
359+
new_edits: false
360+
}
360361
);
361362

362363
/**
@@ -366,9 +367,15 @@ export class RxGraphQLReplicationState {
366367
* so other instances get informed about it
367368
*/
368369
const originalDoc = flatClone(toPouch);
369-
originalDoc._deleted = deletedValue;
370+
if (deletedValue) {
371+
originalDoc._deleted = deletedValue;
372+
} else {
373+
delete originalDoc._deleted;
374+
}
370375
delete originalDoc[this.deletedFlag];
376+
delete originalDoc._revisions;
371377
originalDoc._rev = newRevision;
378+
372379
const cE = changeEventfromPouchChange(
373380
originalDoc,
374381
this.collection

test/unit/replication-graphql.test.ts

+63
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,69 @@ describe('replication-graphql.test.js', () => {
17631763
return docs.length === (amount + 1);
17641764
});
17651765

1766+
server.close();
1767+
db.destroy();
1768+
});
1769+
it('#1812 updates fail when graphql is enabled', async () => {
1770+
const db = await RxDB.create({
1771+
name: util.randomCouchString(10),
1772+
adapter: 'memory',
1773+
multiInstance: false,
1774+
queryChangeDetection: true,
1775+
password: util.randomCouchString(10)
1776+
});
1777+
const schema = clone(schemas.humanWithTimestampAllIndex);
1778+
schema.properties.name.encrypted = true;
1779+
const collection = await db.collection({
1780+
name: 'humans',
1781+
schema
1782+
});
1783+
const server = await SpawnServer.spawn();
1784+
assert.strictEqual(server.getDocuments().length, 0);
1785+
1786+
const replicationState = collection.syncGraphQL({
1787+
url: server.url,
1788+
push: {
1789+
batchSize,
1790+
queryBuilder: pushQueryBuilder
1791+
},
1792+
pull: {
1793+
queryBuilder
1794+
},
1795+
live: true,
1796+
deletedFlag: 'deleted'
1797+
});
1798+
await replicationState.awaitInitialReplication();
1799+
1800+
// add one doc
1801+
const testData = getTestData(1).pop();
1802+
delete testData.deleted;
1803+
await collection.insert(testData);
1804+
1805+
// sync
1806+
await replicationState.run();
1807+
assert.strictEqual(server.getDocuments().length, 1);
1808+
1809+
// update document
1810+
const newTime = Math.round(new Date().getTime() / 1000);
1811+
const doc = await collection.findOne().exec();
1812+
await doc.atomicSet('updatedAt', newTime);
1813+
1814+
// check server
1815+
await replicationState.run();
1816+
await AsyncTestUtil.waitUntil(() => {
1817+
const notUpdated = server.getDocuments().find((d: any) => d.updatedAt !== newTime);
1818+
return !notUpdated;
1819+
});
1820+
1821+
// also delete to ensure nothing broke
1822+
await doc.remove();
1823+
await replicationState.run();
1824+
await AsyncTestUtil.waitUntil(() => {
1825+
const d = server.getDocuments().pop();
1826+
return d['deleted'];
1827+
});
1828+
17661829
server.close();
17671830
db.destroy();
17681831
});

0 commit comments

Comments
 (0)