Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Pass request (upgradeReq) to ConnectionContext (#369)
Browse files Browse the repository at this point in the history
* Pass request (upgradeReq) to ConnectionContext

* Test to check if request is available in ConnectionContext

* Link pr

* Pass context to onDisconnect()
  • Loading branch information
kamilkisiela authored and mxstbr committed Mar 19, 2018
1 parent 698e546 commit bff322c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

### vNEXT
- pass `request` (`upgradeReq`) to `ConnectionContext` [PR #369](https://github.com/apollographql/subscriptions-transport-ws/pull/369)
- pass `ConnectionContext` to `onDisconnect()` as second argument [PR #369](https://github.com/apollographql/subscriptions-transport-ws/pull/369)

### 0.9.6
- fix shallow cloning on contexts which are classes
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ ReactDOM.render(
* `subscribe?: (schema, document, rootValue, contextValue, variableValues, operationName) => Promise<ExecutionResult | AsyncIterator<ExecutionResult>>` : GraphQL `subscribe` function, provide the default one from `graphql` package.
* `onOperation?: (message: SubscribeMessage, params: SubscriptionOptions, webSocket: WebSocket)` : optional method to create custom params that will be used when resolving this operation
* `onOperationComplete?: (webSocket: WebSocket, opId: string)` : optional method that called when a GraphQL operation is done (for query and mutation it's immeditaly, and for subscriptions when unsubscribing)
* `onConnect?: (connectionParams: Object, webSocket: WebSocket)` : optional method that called when a client connects to the socket, called with the `connectionParams` from the client, if the return value is an object, its elements will be added to the context. return `false` or throw an exception to reject the connection. May return a Promise.
* `onDisconnect?: (webSocket: WebSocket)` : optional method that called when a client disconnects
* `onConnect?: (connectionParams: Object, webSocket: WebSocket, context: ConnectionContext)` : optional method that called when a client connects to the socket, called with the `connectionParams` from the client, if the return value is an object, its elements will be added to the context. return `false` or throw an exception to reject the connection. May return a Promise.
* `onDisconnect?: (webSocket: WebSocket, context: ConnectionContext)` : optional method that called when a client disconnects
* `keepAlive?: number` : optional interval in ms to send `KEEPALIVE` messages to all clients

- `socketOptions: {WebSocket.IServerOptions}` : options to pass to the WebSocket object (full docs [here](https://github.com/websockets/ws/blob/master/doc/ws.md))
Expand Down
20 changes: 10 additions & 10 deletions docs/source/lifecycle-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ title: Lifecycle Events
const subscriptionsServer = new SubscriptionServer(
{
subscriptionManager: subscriptionManager,
onConnect: (connectionParams, webSocket) => {
// ...
onConnect: (connectionParams, webSocket, context) => {
// ...
},
onOperation: (message, params, webSocket) => {
// ...
// ...
},
onOperationDone: (webSocket) => {
// ...
onOperationDone: webSocket => {
// ...
},
onDisconnect: (webSocket, context) => {
// ...
},
onDisconnect: (webSocket) => {
// ...
}
},
{
server: websocketServer
}
server: websocketServer,
},
);
```
5 changes: 4 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type ConnectionContext = {
initPromise?: Promise<any>,
isLegacy: boolean,
socket: WebSocket,
request: IncomingMessage,
operations: {
[opId: string]: ExecutionIterator,
},
Expand Down Expand Up @@ -143,6 +144,7 @@ export class SubscriptionServer {
connectionContext.initPromise = Promise.resolve(true);
connectionContext.isLegacy = false;
connectionContext.socket = socket;
connectionContext.request = request;
connectionContext.operations = {};

const connectionClosedHandler = (error: any) => {
Expand All @@ -162,7 +164,7 @@ export class SubscriptionServer {
this.onClose(connectionContext);

if (this.onDisconnect) {
this.onDisconnect(socket);
this.onDisconnect(socket, connectionContext);
}
};

Expand Down Expand Up @@ -466,3 +468,4 @@ export class SubscriptionServer {
);
}
}

22 changes: 22 additions & 0 deletions src/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,16 @@ describe('Server', function () {
}, 200);
});

it('should trigger onConnect with the request available in ConnectionContext', (done) => {
new SubscriptionClient(`ws://localhost:${EVENTS_TEST_PORT}/`);

setTimeout(() => {
assert(eventsOptions.onConnect.calledOnce);
expect(eventsOptions.onConnect.getCall(0).args[2].request).to.be.an.instanceof(IncomingMessage);
done();
}, 200);
});

it('should trigger onConnect and return GQL_CONNECTION_ERROR with error', (done) => {
const connectionCallbackSpy = sinon.spy();

Expand Down Expand Up @@ -1507,6 +1517,18 @@ describe('Server', function () {
}, 200);
});

it('should trigger onDisconnect with ConnectionContext as second argument', (done) => {
const client = new SubscriptionClient(`ws://localhost:${EVENTS_TEST_PORT}/`);
setTimeout(() => {
client.client.close();
}, 100);
setTimeout(() => {
assert(eventsOptions.onDisconnect.calledOnce);
expect(eventsOptions.onConnect.getCall(0).args[1]).to.not.be.undefined;
done();
}, 200);
});

it('should call unsubscribe when client closes the connection', (done) => {
const client = new SubscriptionClient(`ws://localhost:${EVENTS_TEST_PORT}/`);
const spy = sinon.spy(eventsServer as any, 'unsubscribe');
Expand Down

0 comments on commit bff322c

Please sign in to comment.