Skip to content

Commit

Permalink
pubkey#2606: Allow 'dataPath' to be specified on the GraphQL replicat…
Browse files Browse the repository at this point in the history
…ion pull object to specify where to fetch result documents from
  • Loading branch information
joshmcarthur committed May 28, 2021
1 parent 7363aaf commit 3788a36
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs-src/replication-graphql.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ const replicationState = myCollection.syncGraphQL({
pull: {
queryBuilder: pullQueryBuilder, // the queryBuilder from above
modifier: doc => doc // (optional) modifies all pulled documents before they are handeled by RxDB. Returning null will skip the document.
dataPath: null // (optional) specifies the object path to access the document(s). Otherwise, the first result of the response data is used.
},
deletedFlag: 'deleted', // the flag which indicates if a pulled document is deleted
live: true // if this is true, rxdb will watch for ongoing changes and sync them, when false, a one-time-replication will be done
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/replication-graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
filter
} from 'rxjs/operators';
import GraphQLClient from 'graphql-client';
import objectPath from 'object-path';
import {
promiseWait,
flatClone,
Expand Down Expand Up @@ -234,9 +235,8 @@ export class RxGraphQLReplicationState {
return false;
}

// this assumes that there will be always only one property in the response
// is this correct?
const data: any[] = result.data[Object.keys(result.data)[0]];
const dataPath = (this.pull as any).dataPath || ['data', Object.keys(result.data)[0]];
const data: any[] = objectPath.get(result, dataPath);
const modified: any[] = (await Promise.all(data
.map(async (doc: any) => await (this.pull as any).modifier(doc))
)).filter(doc => !!doc);
Expand Down
1 change: 1 addition & 0 deletions src/types/plugins/replication-graphql.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type RxGraphQLReplicationQueryBuilder = (doc: any) =>
export interface GraphQLSyncPullOptions {
queryBuilder: RxGraphQLReplicationQueryBuilder;
modifier?: (doc: any) => Promise<any> | any;
dataPath?: string;
}
export interface GraphQLSyncPushOptions {
queryBuilder: RxGraphQLReplicationQueryBuilder;
Expand Down
23 changes: 23 additions & 0 deletions test/unit/replication-graphql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,29 @@ describe('replication-graphql.test.js', () => {
server.close();
c.database.destroy();
});
it('should pull documents from a custom dataPath if one is specified', async () => {
const [c, server] = await Promise.all([
humansCollection.createHumanWithTimestamp(0),
SpawnServer.spawn(getTestData(batchSize))
]);
const replicationState = c.syncGraphQL({
url: server.url,
pull: {
queryBuilder,
dataPath: 'data.feedForRxDBReplication'
},
deletedFlag: 'deleted'
});
assert.strictEqual(replicationState.isStopped(), false);

await AsyncTestUtil.waitUntil(async () => {
const docs = await c.find().exec();
return docs.length === batchSize;
});

server.close();
c.database.destroy();
});
it('pulled docs should be marked with a special revision if syncRevisions is false', async () => {
const [c, server] = await Promise.all([
humansCollection.createHumanWithTimestamp(0),
Expand Down

0 comments on commit 3788a36

Please sign in to comment.