Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2606: Allow 'dataPath' to be specified on the GraphQL replication pull object to specify where to fetch result documents from #3195

Merged
merged 5 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

Features:
- Added `RxDatabase.migrationStates()` which returns an observable to observe the state of all ongoing migrations.
- Added `dataPath` property to GraphQL replication pull options to allow the document JSON lookup path to configured instead of assuming the document data is always the first child of the response [#2606](https://github.com/pubkey/rxdb/issues/2606)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add this to the top "comming soon" section, not an already released version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yep, my master was out of date. Just merged origin/master and resolved the conflict to place this in the correct version.


Bugfixes:
- Ensure every background task is done when `RxDatabase.destroy()` resolves. [#2938](https://github.com/pubkey/rxdb/issues/2938)
Expand Down
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.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is optional, the default value must be undefined not null

},
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