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

feat(client): url option accepts a function or a Promise #143

Merged
merged 4 commits into from
Mar 25, 2021
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
10 changes: 9 additions & 1 deletion docs/interfaces/client.clientoptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,18 @@ ___

### url

• **url**: *string*
• **url**: *string* \| () => *string* \| *Promise*<string\>

URL of the GraphQL over WebSocket Protocol compliant server to connect.

If the option is a function, it will be called on every WebSocket connection attempt.
Returning a promise is supported too and the connecting phase will stall until it
resolves with the URL.

A good use-case for having a function is when using the URL for authentication,
where subsequent reconnects (due to auth) may have a refreshed identity token in
the URL.

___

### webSocketImpl
Expand Down
19 changes: 16 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,18 @@ export type EventListener<E extends Event> = E extends EventConnecting

/** Configuration used for the GraphQL over WebSocket client. */
export interface ClientOptions {
/** URL of the GraphQL over WebSocket Protocol compliant server to connect. */
url: string;
/**
* URL of the GraphQL over WebSocket Protocol compliant server to connect.
*
* If the option is a function, it will be called on every WebSocket connection attempt.
* Returning a promise is supported too and the connecting phase will stall until it
* resolves with the URL.
*
* A good use-case for having a function is when using the URL for authentication,
* where subsequent reconnects (due to auth) may have a refreshed identity token in
* the URL.
*/
url: string | (() => Promise<string> | string);
/**
* Optional parameters, passed through the `payload` field with the `ConnectionInit` message,
* that the client specifies when establishing a connection with the server. You can use this
Expand Down Expand Up @@ -330,7 +340,10 @@ export function createClient(options: ClientOptions): Client {
}

emitter.emit('connecting');
const socket = new WebSocketImpl(url, GRAPHQL_TRANSPORT_WS_PROTOCOL);
const socket = new WebSocketImpl(
typeof url === 'function' ? await url() : url,
GRAPHQL_TRANSPORT_WS_PROTOCOL,
);

socket.onerror = (err) => {
// we let the onclose reject the promise for correct retry handling
Expand Down
13 changes: 13 additions & 0 deletions src/tests/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ it('should use the provided WebSocket implementation', async () => {
await server.waitForClient();
});

it('should accept a function for the url', async () => {
const { url, ...server } = await startTServer();

createClient({
url: () => Promise.resolve(url),
retryAttempts: 0,
onNonLazyError: noop,
lazy: false,
});

await server.waitForClient();
});

it('should not accept invalid WebSocket implementations', async () => {
const { url } = await startTServer();

Expand Down