Skip to content

Commit

Permalink
feat: TypeScript generic for connection init payload (`connectionPara…
Browse files Browse the repository at this point in the history
…ms`) (#311)
  • Loading branch information
enisdenjo committed Feb 19, 2022
1 parent afa768a commit e67cf80
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 58 deletions.
10 changes: 8 additions & 2 deletions docs/interfaces/client.ClientOptions.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
[graphql-ws](../README.md) / [client](../modules/client.md) / ClientOptions

# Interface: ClientOptions
# Interface: ClientOptions<P\>

[client](../modules/client.md).ClientOptions

Configuration used for the GraphQL over WebSocket client.

## Type parameters

| Name | Type |
| :------ | :------ |
| `P` | extends [`ConnectionInitMessage`](common.ConnectionInitMessage.md)[``"payload"``][`ConnectionInitMessage`](common.ConnectionInitMessage.md)[``"payload"``] |

## Table of contents

### Properties
Expand Down Expand Up @@ -52,7 +58,7 @@ ___

### connectionParams

`Optional` **connectionParams**: `Record`<`string`, `unknown`\> \| () => `undefined` \| `Record`<`string`, `unknown`\> \| `Promise`<`undefined` \| `Record`<`string`, `unknown`\>\>
`Optional` **connectionParams**: `P` \| () => `P` \| `Promise`<`P`\>

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
5 changes: 3 additions & 2 deletions docs/interfaces/server.Context.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
[graphql-ws](../README.md) / [server](../modules/server.md) / Context

# Interface: Context<E\>
# Interface: Context<P, E\>

[server](../modules/server.md).Context

## Type parameters

| Name | Type |
| :------ | :------ |
| `P` | extends [`ConnectionInitMessage`](common.ConnectionInitMessage.md)[``"payload"``][`ConnectionInitMessage`](common.ConnectionInitMessage.md)[``"payload"``] |
| `E` | `unknown` |

## Table of contents
Expand Down Expand Up @@ -45,7 +46,7 @@ ___

### connectionParams

`Optional` `Readonly` **connectionParams**: `Readonly`<`Record`<`string`, `unknown`\>\>
`Optional` `Readonly` **connectionParams**: `Readonly`<`P`\>

The parameters passed during the connection initialisation.

Expand Down
23 changes: 12 additions & 11 deletions docs/interfaces/server.ServerOptions.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
[graphql-ws](../README.md) / [server](../modules/server.md) / ServerOptions

# Interface: ServerOptions<E\>
# Interface: ServerOptions<P, E\>

[server](../modules/server.md).ServerOptions

## Type parameters

| Name | Type |
| :------ | :------ |
| `P` | extends [`ConnectionInitMessage`](common.ConnectionInitMessage.md)[``"payload"``][`ConnectionInitMessage`](common.ConnectionInitMessage.md)[``"payload"``] |
| `E` | `unknown` |

## Table of contents
Expand Down Expand Up @@ -57,7 +58,7 @@ ___

### context

`Optional` **context**: [`GraphQLExecutionContextValue`](../modules/server.md#graphqlexecutioncontextvalue) \| (`ctx`: [`Context`](server.Context.md)<`E`\>, `message`: [`SubscribeMessage`](common.SubscribeMessage.md), `args`: `ExecutionArgs`) => [`GraphQLExecutionContextValue`](../modules/server.md#graphqlexecutioncontextvalue) \| `Promise`<[`GraphQLExecutionContextValue`](../modules/server.md#graphqlexecutioncontextvalue)\>
`Optional` **context**: [`GraphQLExecutionContextValue`](../modules/server.md#graphqlexecutioncontextvalue) \| (`ctx`: [`Context`](server.Context.md)<`P`, `E`\>, `message`: [`SubscribeMessage`](common.SubscribeMessage.md), `args`: `ExecutionArgs`) => [`GraphQLExecutionContextValue`](../modules/server.md#graphqlexecutioncontextvalue) \| `Promise`<[`GraphQLExecutionContextValue`](../modules/server.md#graphqlexecutioncontextvalue)\>

A value which is provided to every resolver and holds
important contextual information like the currently
Expand Down Expand Up @@ -123,7 +124,7 @@ ___

### schema

`Optional` **schema**: `GraphQLSchema` \| (`ctx`: [`Context`](server.Context.md)<`E`\>, `message`: [`SubscribeMessage`](common.SubscribeMessage.md), `args`: `Omit`<`ExecutionArgs`, ``"schema"``\>) => `GraphQLSchema` \| `Promise`<`GraphQLSchema`\>
`Optional` **schema**: `GraphQLSchema` \| (`ctx`: [`Context`](server.Context.md)<`P`, `E`\>, `message`: [`SubscribeMessage`](common.SubscribeMessage.md), `args`: `Omit`<`ExecutionArgs`, ``"schema"``\>) => `GraphQLSchema` \| `Promise`<`GraphQLSchema`\>

The GraphQL schema on which the operations
will be executed and validated against.
Expand Down Expand Up @@ -229,7 +230,7 @@ called before the `onClose`.

| Name | Type |
| :------ | :------ |
| `ctx` | [`Context`](server.Context.md)<`E`\> |
| `ctx` | [`Context`](server.Context.md)<`P`, `E`\> |
| `code` | `number` |
| `reason` | `string` |

Expand Down Expand Up @@ -259,7 +260,7 @@ will still be called.

| Name | Type |
| :------ | :------ |
| `ctx` | [`Context`](server.Context.md)<`E`\> |
| `ctx` | [`Context`](server.Context.md)<`P`, `E`\> |
| `message` | [`CompleteMessage`](common.CompleteMessage.md) |

#### Returns
Expand Down Expand Up @@ -299,7 +300,7 @@ in the close event reason.

| Name | Type |
| :------ | :------ |
| `ctx` | [`Context`](server.Context.md)<`E`\> |
| `ctx` | [`Context`](server.Context.md)<`P`, `E`\> |

#### Returns

Expand Down Expand Up @@ -330,7 +331,7 @@ of the connection state - consider using the `onClose` callback.

| Name | Type |
| :------ | :------ |
| `ctx` | [`Context`](server.Context.md)<`E`\> |
| `ctx` | [`Context`](server.Context.md)<`P`, `E`\> |
| `code` | `number` |
| `reason` | `string` |

Expand Down Expand Up @@ -360,7 +361,7 @@ in the close event reason.

| Name | Type |
| :------ | :------ |
| `ctx` | [`Context`](server.Context.md)<`E`\> |
| `ctx` | [`Context`](server.Context.md)<`P`, `E`\> |
| `message` | [`ErrorMessage`](common.ErrorMessage.md) |
| `errors` | readonly `GraphQLError`[] |

Expand Down Expand Up @@ -391,7 +392,7 @@ in the close event reason.

| Name | Type |
| :------ | :------ |
| `ctx` | [`Context`](server.Context.md)<`E`\> |
| `ctx` | [`Context`](server.Context.md)<`P`, `E`\> |
| `message` | [`NextMessage`](common.NextMessage.md) |
| `args` | `ExecutionArgs` |
| `result` | [`ExecutionResult`](common.ExecutionResult.md)<`Record`<`string`, `unknown`\>, `Record`<`string`, `unknown`\>\> \| [`ExecutionPatchResult`](common.ExecutionPatchResult.md)<`unknown`, `Record`<`string`, `unknown`\>\> |
Expand Down Expand Up @@ -429,7 +430,7 @@ in the close event reason.

| Name | Type |
| :------ | :------ |
| `ctx` | [`Context`](server.Context.md)<`E`\> |
| `ctx` | [`Context`](server.Context.md)<`P`, `E`\> |
| `message` | [`SubscribeMessage`](common.SubscribeMessage.md) |
| `args` | `ExecutionArgs` |
| `result` | [`OperationResult`](../modules/server.md#operationresult) |
Expand Down Expand Up @@ -476,7 +477,7 @@ in the close event reason.

| Name | Type |
| :------ | :------ |
| `ctx` | [`Context`](server.Context.md)<`E`\> |
| `ctx` | [`Context`](server.Context.md)<`P`, `E`\> |
| `message` | [`SubscribeMessage`](common.SubscribeMessage.md) |

#### Returns
Expand Down
10 changes: 8 additions & 2 deletions docs/modules/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,15 +348,21 @@ ___

### createClient

**createClient**(`options`): [`Client`](../interfaces/client.Client.md)
**createClient**<`P`\>(`options`): [`Client`](../interfaces/client.Client.md)

Creates a disposable GraphQL over WebSocket client.

#### Type parameters

| Name | Type |
| :------ | :------ |
| `P` | extends `undefined` \| `Record`<`string`, `unknown`\>`undefined` \| `Record`<`string`, `unknown`\> |

#### Parameters

| Name | Type |
| :------ | :------ |
| `options` | [`ClientOptions`](../interfaces/client.ClientOptions.md) |
| `options` | [`ClientOptions`](../interfaces/client.ClientOptions.md)<`P`\> |

#### Returns

Expand Down
4 changes: 2 additions & 2 deletions docs/modules/common.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ ___

### isMessage

**isMessage**(`val`): val is ConnectionAckMessage \| PingMessage \| PongMessage \| ConnectionInitMessage \| SubscribeMessage \| NextMessage \| ErrorMessage \| CompleteMessage
**isMessage**(`val`): val is ConnectionInitMessage \| ConnectionAckMessage \| PingMessage \| PongMessage \| SubscribeMessage \| NextMessage \| ErrorMessage \| CompleteMessage

Checks if the provided value is a message.

Expand All @@ -148,7 +148,7 @@ Checks if the provided value is a message.

#### Returns

val is ConnectionAckMessage \| PingMessage \| PongMessage \| ConnectionInitMessage \| SubscribeMessage \| NextMessage \| ErrorMessage \| CompleteMessage
val is ConnectionInitMessage \| ConnectionAckMessage \| PingMessage \| PongMessage \| SubscribeMessage \| NextMessage \| ErrorMessage \| CompleteMessage

___

Expand Down
5 changes: 3 additions & 2 deletions docs/modules/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ ___

### makeServer

**makeServer**<`E`\>(`options`): [`Server`](../interfaces/server.Server.md)<`E`\>
**makeServer**<`P`, `E`\>(`options`): [`Server`](../interfaces/server.Server.md)<`E`\>

Makes a Protocol complient WebSocket GraphQL server. The server
is actually an API which is to be used with your favourite WebSocket
Expand All @@ -55,13 +55,14 @@ Read more about the Protocol in the PROTOCOL.md documentation file.

| Name | Type |
| :------ | :------ |
| `P` | extends `undefined` \| `Record`<`string`, `unknown`\>`undefined` \| `Record`<`string`, `unknown`\> |
| `E` | `unknown` |

#### Parameters

| Name | Type |
| :------ | :------ |
| `options` | [`ServerOptions`](../interfaces/server.ServerOptions.md)<`E`\> |
| `options` | [`ServerOptions`](../interfaces/server.ServerOptions.md)<`P`, `E`\> |

#### Returns

Expand Down
5 changes: 3 additions & 2 deletions docs/modules/use_fastify_websocket.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

### makeHandler

**makeHandler**<`E`\>(`options`, `keepAlive?`): `fastifyWebsocket.WebsocketHandler`
**makeHandler**<`P`, `E`\>(`options`, `keepAlive?`): `fastifyWebsocket.WebsocketHandler`

Make a handler to use on a [fastify-websocket](https://github.com/fastify/fastify-websocket) route.
This is a basic starter, feel free to copy the code over and adjust it to your needs
Expand All @@ -25,13 +25,14 @@ This is a basic starter, feel free to copy the code over and adjust it to your n

| Name | Type |
| :------ | :------ |
| `P` | extends `undefined` \| `Record`<`string`, `unknown`\>`undefined` \| `Record`<`string`, `unknown`\> |
| `E` | extends `Record`<`PropertyKey`, `unknown`\>`Record`<`PropertyKey`, `never`\> |

#### Parameters

| Name | Type | Default value |
| :------ | :------ | :------ |
| `options` | [`ServerOptions`](../interfaces/server.ServerOptions.md)<[`Extra`](../interfaces/use_fastify_websocket.Extra.md) & `Partial`<`E`\>\> | `undefined` |
| `options` | [`ServerOptions`](../interfaces/server.ServerOptions.md)<`P`, [`Extra`](../interfaces/use_fastify_websocket.Extra.md) & `Partial`<`E`\>\> | `undefined` |
| `keepAlive` | `number` | `12_000` |

#### Returns
Expand Down
5 changes: 3 additions & 2 deletions docs/modules/use_uWebSockets.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

### makeBehavior

**makeBehavior**<`E`\>(`options`, `behavior?`, `keepAlive?`): `uWS.WebSocketBehavior`
**makeBehavior**<`P`, `E`\>(`options`, `behavior?`, `keepAlive?`): `uWS.WebSocketBehavior`

Make the behaviour for using a [uWebSockets.js](https://github.com/uNetworking/uWebSockets.js) WebSocket server.
This is a basic starter, feel free to copy the code over and adjust it to your needs
Expand All @@ -27,13 +27,14 @@ This is a basic starter, feel free to copy the code over and adjust it to your n

| Name | Type |
| :------ | :------ |
| `P` | extends `undefined` \| `Record`<`string`, `unknown`\>`undefined` \| `Record`<`string`, `unknown`\> |
| `E` | extends `Record`<`PropertyKey`, `unknown`\>`Record`<`PropertyKey`, `never`\> |

#### Parameters

| Name | Type | Default value |
| :------ | :------ | :------ |
| `options` | [`ServerOptions`](../interfaces/server.ServerOptions.md)<[`Extra`](../interfaces/use_uWebSockets.Extra.md) & `Partial`<`E`\>\> | `undefined` |
| `options` | [`ServerOptions`](../interfaces/server.ServerOptions.md)<`P`, [`Extra`](../interfaces/use_uWebSockets.Extra.md) & `Partial`<`E`\>\> | `undefined` |
| `behavior` | `WebSocketBehavior` | `{}` |
| `keepAlive` | `number` | `12_000` |

Expand Down
5 changes: 3 additions & 2 deletions docs/modules/use_ws.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

### useServer

**useServer**<`E`\>(`options`, `ws`, `keepAlive?`): [`Disposable`](../interfaces/common.Disposable.md)
**useServer**<`P`, `E`\>(`options`, `ws`, `keepAlive?`): [`Disposable`](../interfaces/common.Disposable.md)

Use the server on a [ws](https://github.com/websockets/ws) ws server.
This is a basic starter, feel free to copy the code over and adjust it to your needs
Expand All @@ -25,13 +25,14 @@ This is a basic starter, feel free to copy the code over and adjust it to your n

| Name | Type |
| :------ | :------ |
| `P` | extends `undefined` \| `Record`<`string`, `unknown`\>`undefined` \| `Record`<`string`, `unknown`\> |
| `E` | extends `Record`<`PropertyKey`, `unknown`\>`Record`<`PropertyKey`, `never`\> |

#### Parameters

| Name | Type | Default value |
| :------ | :------ | :------ |
| `options` | [`ServerOptions`](../interfaces/server.ServerOptions.md)<[`Extra`](../interfaces/use_ws.Extra.md) & `Partial`<`E`\>\> | `undefined` |
| `options` | [`ServerOptions`](../interfaces/server.ServerOptions.md)<`P`, [`Extra`](../interfaces/use_ws.Extra.md) & `Partial`<`E`\>\> | `undefined` |
| `ws` | `Server` | `undefined` |
| `keepAlive` | `number` | `12_000` |

Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/utils/tservers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface TServer {
expire?: number,
) => Promise<void>;
waitForConnect: (
test?: (ctx: Context<WSExtra | UWSExtra | FastifyExtra>) => void,
test?: (ctx: Context<any, WSExtra | UWSExtra | FastifyExtra>) => void,
expire?: number,
) => Promise<void>;
waitForOperation: (test?: () => void, expire?: number) => Promise<void>;
Expand Down Expand Up @@ -152,7 +152,7 @@ export async function startWSTServer(
socket.once('close', () => sockets.delete(socket));
});

const pendingConnections: Context<WSExtra>[] = [];
const pendingConnections: Context<any, WSExtra>[] = [];
let pendingOperations = 0,
pendingCompletes = 0;
const server = useWSServer(
Expand Down Expand Up @@ -335,7 +335,7 @@ export async function startUWSTServer(
// sockets to kick off on teardown
const sockets = new Set<uWS.WebSocket>();

const pendingConnections: Context<UWSExtra>[] = [];
const pendingConnections: Context<any, UWSExtra>[] = [];
let pendingOperations = 0,
pendingCompletes = 0;
const listenSocket = await new Promise<uWS.us_listen_socket>(
Expand Down Expand Up @@ -473,7 +473,7 @@ export async function startFastifyWSTServer(
// sockets to kick off on teardown
const sockets = new Set<ws>();

const pendingConnections: Context<FastifyExtra>[] = [];
const pendingConnections: Context<any, FastifyExtra>[] = [];
const pendingClients: TServerClient[] = [];
let pendingOperations = 0,
pendingCompletes = 0,
Expand Down
14 changes: 7 additions & 7 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ export type EventListener<E extends Event> = E extends EventConnecting
*
* @category Client
*/
export interface ClientOptions {
export interface ClientOptions<
P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'],
> {
/**
* URL of the GraphQL over WebSocket Protocol compliant server to connect.
*
Expand All @@ -221,11 +223,7 @@ export interface ClientOptions {
* Throwing an error from within this function will close the socket with the `Error` message
* in the close event reason.
*/
connectionParams?:
| ConnectionInitMessage['payload']
| (() =>
| Promise<ConnectionInitMessage['payload']>
| ConnectionInitMessage['payload']);
connectionParams?: P | (() => Promise<P> | P);
/**
* Controls when should the connection be established.
*
Expand Down Expand Up @@ -428,7 +426,9 @@ export interface Client extends Disposable {
*
* @category Client
*/
export function createClient(options: ClientOptions): Client {
export function createClient<
P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'],
>(options: ClientOptions<P>): Client {
const {
url,
connectionParams,
Expand Down

0 comments on commit e67cf80

Please sign in to comment.