You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/graphql/subscriptions.md
+30-21Lines changed: 30 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -295,34 +295,30 @@ GraphQLModule.forRoot({
295
295
}),
296
296
```
297
297
298
-
#### Authorization over WebSocket
298
+
#### Authentication over WebSocket
299
299
300
-
Checking that the user is authenticated should be done inside the `onConnect` property of the `subscriptions` options (read [more](https://www.apollographql.com/docs/graphql-subscriptions/authentication/)).
301
-
The `onConnect` will receive as first argument the `connectionParams` passed to the `SubscriptionClient` (read [more](https://www.apollographql.com/docs/react/data/subscriptions/#4-authenticate-over-websocket-optional)).
300
+
Checking that the user is authenticated should be done inside the `onConnect` callback function that you can specify in the `subscriptions` options.
301
+
302
+
The `onConnect` will receive as a first argument the `connectionParams` passed to the `SubscriptionClient` (read [more](https://www.apollographql.com/docs/react/data/subscriptions/#5-authenticate-over-websocket-optional)).
302
303
303
304
```typescript
304
305
GraphQLModule.forRoot({
305
-
installSubscriptionHandlers: true,
306
306
subscriptions: {
307
-
onConnect: (connectionParams) => {
308
-
// extract the token
309
-
const authToken =connectionParams.authToken;
310
-
// validate the token (e.g., signature, expiration for jwt)
311
-
if (!isValid(authToken)) {
312
-
thrownewError('Token is not valid');
313
-
}
314
-
// extract user information from token
315
-
constuser =parseToken(authToken);
316
-
//return user info to add them to the context later
317
-
return { user };
318
-
},
307
+
'subscriptions-transport-ws': {
308
+
onConnect: (connectionParams) => {
309
+
const authToken =connectionParams.authToken;
310
+
if (!isValid(authToken)) {
311
+
thrownewError('Token is not valid');
312
+
}
313
+
// extract user information from token
314
+
constuser =parseToken(authToken);
315
+
// return user info to add them to the context later
316
+
return{ user};
317
+
},
318
+
}
319
319
},
320
320
context: ({ connection }) => {
321
-
// connection.context will be equal to what was returned by onConnect
322
-
// now user info is available inside context.req.user
323
-
return {
324
-
req: connection?.context?? {},
325
-
};
321
+
// connection.context will be equal to what was returned by the "onConnect" callback
326
322
},
327
323
}),
328
324
```
@@ -331,3 +327,16 @@ The `authToken` in this example is only sent once by the client, when the connec
331
327
All subscriptions made with this connection will have the same `authToken`, and thus the same user info.
332
328
333
329
> warning **Note** There is a bug in `subscriptions-transport-ws` that allows connections to skip the `onConnect` phase (read [more](https://github.com/apollographql/subscriptions-transport-ws/issues/349)). You should not assume that `onConnect` was called when the user starts a subscription, and always check that the `context` is populated.
330
+
331
+
If you're using the `graphql-ws` package, the signature of the `onConnect` callback will be slightly different:
332
+
333
+
```typescript
334
+
subscriptions: {
335
+
'graphql-ws': {
336
+
onConnect: (context:Context<any>) => {
337
+
const { connectionParams } =context;
338
+
// the rest will remain the same as in the example above
<p>If you need to add some custom logic around the serialization of responses on the client side, you can use a custom class that extends the <code>ClientProxy</code> class or one of its child classes. For modifying successful requests you can override the <code>serializeResponse</code> method, and for modifying any errors that go through this client you can override the <code>serializeError</code> method. To make use of this custom class, you can pass the class itself to the <code>ClientsModule.register()</code> method using the <code>customClass</code> property. Below is an example of a custom <code>ClientProxy</code> that serializes each error into an <code>RpcException</code>.</p>
0 commit comments