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

Allow connectionParams to be provided as part of wsOptions for replication-graphql #6741

Merged
merged 2 commits into from
Jan 7, 2025
Merged
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
4 changes: 3 additions & 1 deletion docs-src/docs/replication-graphql.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,9 @@ const replicationState = replicateGraphQL(

// Websocket options that can be passed as a parameter to initialize the subscription
// Can be applied anything from the graphql-ws ClientOptions - https://the-guild.dev/graphql/ws/docs/interfaces/client.ClientOptions
// Excepting parameters: 'url', 'shouldRetry', 'webSocketImpl', 'connectionParams' - locked for the internal usage
// Except these parameters: 'url', 'shouldRetry', 'webSocketImpl' - locked for internal usage
// Note: if you provide connectionParams as a wsOption, make sure it returns any necessary headers (e.g. authorization)
// because providing your own connectionParams prevents headers from being included automatically
wsOptions: {
retryAttempts: 10,
}
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/replication-graphql/graphql-websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ export function getGraphQLWebSocket(
GRAPHQL_WEBSOCKET_BY_URL,
url,
() => {
const connectionParamsHeaders = headers ? { headers } : undefined;
const wsClient = createClient({
...options,
url,
shouldRetry: () => true,
webSocketImpl: IsomorphicWebSocket,
connectionParams: headers ? { headers } : undefined,
connectionParams: options.connectionParams || connectionParamsHeaders,
});
return {
url,
Expand Down
2 changes: 1 addition & 1 deletion src/types/plugins/replication-graphql.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type RxGraphQLReplicationPushQueryBuilder = (
rows: RxReplicationWriteToMasterRow<any>[]
) => RxGraphQLReplicationQueryBuilderResponse;

export type RxGraphQLPullWSOptions = Omit<ClientOptions, 'url' | 'shouldRetry' | 'webSocketImpl' | 'connectionParams'>;
export type RxGraphQLPullWSOptions = Omit<ClientOptions, 'url' | 'shouldRetry' | 'webSocketImpl'>;

export type RxGraphQLReplicationPullQueryBuilder<CheckpointType> = (
latestPulledCheckpoint: CheckpointType | undefined,
Expand Down
15 changes: 11 additions & 4 deletions test/unit/replication-graphql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1693,8 +1693,14 @@ describe('replication-graphql.test.ts', () => {
},
'error': () => {
capturedWSStates.push('error');
}
}
},
},
connectionParams: () => {
capturedWSStates.push('connectionParams');
return {
token: 'Bearer token'
};
},
},
},
headers: {
Expand All @@ -1709,18 +1715,19 @@ describe('replication-graphql.test.ts', () => {
await replicationState.awaitInitialReplication();

await waitUntil(() => {
return capturedWSStates.length === 2;
return capturedWSStates.length === 3;
});

assert.equal(capturedWSStates.includes('connected'), true);
assert.equal(capturedWSStates.includes('connecting'), true);
assert.equal(capturedWSStates.includes('connectionParams'), true);
assert.equal(capturedWSStates.includes('closed'), false);
assert.equal(capturedWSStates.includes('error'), false);

replicationState.cancel();

await waitUntil(() => {
return capturedWSStates.length === 3;
return capturedWSStates.length === 4;
});

assert.equal(capturedWSStates.includes('closed'), true);
Expand Down