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

fix(datapath) must be equivalent for pull and push #6019

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 10 additions & 1 deletion src/plugins/replication-graphql/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { RxGraphQLReplicationClientState, RxGraphQLReplicationQueryBuilderResponseObject } from '../../types/index.d.ts';
import { ensureNotFalsy } from '../../plugins/utils/index.ts';
import { ensureNotFalsy, getProperty } from '../../plugins/utils/index.ts';

export const GRAPHQL_REPLICATION_PLUGIN_IDENTITY_PREFIX = 'graphql';

Expand Down Expand Up @@ -41,3 +41,12 @@ export function graphQLRequest(
return body;
});
}

export function getDataFromResult(
result: { data: object },
userDefinedDataPath: string | string[] | undefined
): any {
const dataPath = userDefinedDataPath || ['data', Object.keys(result.data)[0]];
const data: any = getProperty(result, dataPath);
return data;
}
10 changes: 4 additions & 6 deletions src/plugins/replication-graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '../../plugins/utils/index.ts';

import {
getDataFromResult,
graphQLRequest
} from './helper.ts';

Expand Down Expand Up @@ -131,8 +132,7 @@ export function replicateGraphQL<RxDocType, CheckpointType>(
if (result.errors) {
throw result.errors;
}
const dataPath = pull.dataPath || ['data', Object.keys(result.data)[0]];
let data: any = getProperty(result, dataPath);
let data: any = getDataFromResult(result, pull.dataPath);
if (pull.responseModifier) {
data = await pull.responseModifier(
data,
Expand Down Expand Up @@ -166,17 +166,15 @@ export function replicateGraphQL<RxDocType, CheckpointType>(
if (result.errors) {
throw result.errors;
}
const dataPath = push.dataPath || Object.keys(result.data)[0];
let data: any = getProperty(result.data, dataPath);

let data: any = getDataFromResult(result, push.dataPath);
if (push.responseModifier) {
data = await push.responseModifier(
data,
);
}

return data;
},
}, 
batchSize: push.batchSize,
modifier: push.modifier
};
Expand Down
14 changes: 12 additions & 2 deletions src/types/plugins/replication-graphql.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ export type GraphQLSyncPullOptions<RxDocType, CheckpointType> = Omit<
> & {
queryBuilder: RxGraphQLReplicationPullQueryBuilder<CheckpointType>;
streamQueryBuilder?: RxGraphQLReplicationPullStreamQueryBuilder;
dataPath?: string;
/**
* The path to the data in the GraphQL response.
* If set, the data will be taken from the response at this path.
* @example ['data', 'foo', 'bar'] or 'data.foo.bar'
*/
dataPath?: string | string[];
responseModifier?: RxGraphQLPullResponseModifier<RxDocType, CheckpointType>;
includeWsHeaders?: boolean;
};
Expand All @@ -63,7 +68,12 @@ export type GraphQLSyncPushOptions<RxDocType> = Omit<
'handler'
> & {
queryBuilder: RxGraphQLReplicationPushQueryBuilder;
dataPath?: string;
/**
* The path to the data in the GraphQL response.
* If set, the data will be taken from the response at this path.
* @example ['data', 'foo', 'bar'] or 'data.foo.bar'
*/
dataPath?: string | string[];
responseModifier?: RxGraphQLPushResponseModifier<RxDocType>;
};

Expand Down
30 changes: 30 additions & 0 deletions test/unit/replication-graphql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,36 @@ describe('replication-graphql.test.ts', () => {
1
);

server.close();
c.database.destroy();
});
it('should push documents from a custom dataPath if one is specified', async () => {
const [c, server] = await Promise.all([
humansCollection.createHumanWithTimestamp(batchSize),
SpawnServer.spawn()
]);

const replicationState = replicateGraphQL({
replicationIdentifier: randomCouchString(10),
collection: c,
url: server.url,
push: {
batchSize,
dataPath: 'data.writeHumans',
queryBuilder: pushQueryBuilder,
},
live: false,
retryTime: 1000,
deletedField: 'deleted'
});
ensureReplicationHasNoErrors(replicationState);
assert.strictEqual(replicationState.isStopped(), false);

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

server.close();
c.database.destroy();
});
Expand Down
Loading