Skip to content

Commit

Permalink
feat(server): Use fastify-websocket (enisdenjo#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo authored Jun 6, 2021
1 parent 784ea15 commit b62fc95
Show file tree
Hide file tree
Showing 9 changed files with 729 additions and 8 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,34 @@ uWS
});
```

##### With [fastify-websocket](https://github.com/fastify/fastify-websocket)

```ts
import Fastify from 'fastify'; // yarn add fastify
import fastifyWebsocket from 'fastify-websocket'; // yarn add fastify-websocket
import { makeHandler } from 'graphql-ws/lib/use/fastify-websocket';

const fastify = Fastify();
fastify.register(fastifyWebsocket);

fastify.get(
'/graphql',
{ websocket: true },
makeHandler(
// from the previous step
{ schema, roots },
),
);

fastify.listen(4000, (err) => {
if (err) {
fastify.log.error(err);
return process.exit(1);
}
console.log('Listening to port 4000');
});
```

#### Use the client

```ts
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ graphql-ws
- [client](modules/client.md)
- [common](modules/common.md)
- [server](modules/server.md)
- [use/fastify-websocket](modules/use_fastify_websocket.md)
- [use/uWebSockets](modules/use_uwebsockets.md)
- [use/ws](modules/use_ws.md)
32 changes: 32 additions & 0 deletions docs/interfaces/use_fastify_websocket.extra.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[graphql-ws](../README.md) / [use/fastify-websocket](../modules/use_fastify_websocket.md) / Extra

# Interface: Extra

[use/fastify-websocket](../modules/use_fastify_websocket.md).Extra

The extra that will be put in the `Context`.

## Table of contents

### Properties

- [connection](use_fastify_websocket.extra.md#connection)
- [request](use_fastify_websocket.extra.md#request)

## Properties

### connection

`Readonly` **connection**: `SocketStream`

The underlying socket connection between the server and the client.
The WebSocket socket is located under the `socket` parameter.

___

### request

`Readonly` **request**: `FastifyRequest`<RouteGenericInterface, Server, IncomingMessage\>

The initial HTTP upgrade request before the actual
socket and connection is established.
39 changes: 39 additions & 0 deletions docs/modules/use_fastify_websocket.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[graphql-ws](../README.md) / use/fastify-websocket

# Module: use/fastify-websocket

## Table of contents

### Interfaces

- [Extra](../interfaces/use_fastify_websocket.extra.md)

### Functions

- [makeHandler](use_fastify_websocket.md#makehandler)

## Server/fastify-websocket

### makeHandler

**makeHandler**<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

#### Type parameters

| Name | Type |
| :------ | :------ |
| `E` | `E`: `Record`<PropertyKey, unknown\> = `Record`<PropertyKey, never\> |

#### Parameters

| Name | Type |
| :------ | :------ |
| `options` | [ServerOptions](../interfaces/server.serveroptions.md)<[Extra](../interfaces/use_fastify_websocket.extra.md) & `Partial`<E\>\> |
| `keepAlive` | `number` |

#### Returns

`fastifyWebsocket.WebsocketHandler`
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
"require": "./lib/use/uWebSockets.js",
"import": "./lib/use/uWebSockets.mjs"
},
"./lib/use/fastify-websocket": {
"require": "./lib/use/fastify-websocket.js",
"import": "./lib/use/fastify-websocket.mjs"
},
"./package.json": "./package.json"
},
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -90,6 +94,8 @@
"eslint": "^7.27.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"fastify": "^3.17.0",
"fastify-websocket": "^3.2.0",
"glob": "^7.1.7",
"graphql": "^15.5.0",
"jest": "^27.0.4",
Expand Down
12 changes: 12 additions & 0 deletions src/tests/use.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import http from 'http';
import ws from 'ws';
import stream from 'stream';
import {
MessageType,
stringifyMessage,
Expand All @@ -12,6 +13,7 @@ import {
tServers,
WSExtra,
UWSExtra,
FastifyExtra,
waitForDone,
} from './utils';

Expand Down Expand Up @@ -108,6 +110,16 @@ for (const { tServer, startTServer } of tServers) {
expect((ctx.extra as WSExtra).request).toBeInstanceOf(
http.IncomingMessage,
);
} else if (tServer === 'fastify-websocket') {
expect((ctx.extra as FastifyExtra).connection).toBeInstanceOf(
stream.Duplex,
);
expect(
(ctx.extra as FastifyExtra).connection.socket,
).toBeInstanceOf(ws);
expect((ctx.extra as FastifyExtra).request.constructor.name).toBe(
'Request',
);
} else {
throw new Error('Missing test case for ' + tServer);
}
Expand Down
Loading

0 comments on commit b62fc95

Please sign in to comment.