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
+35-6Lines changed: 35 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,9 +16,9 @@ GraphQLModule.forRoot({
16
16
17
17
#### Code first
18
18
19
-
To create a subscription using the code first approach, we'll make use of the `@Subscription()` decorator and the `PubSub` class from the `graphql-subscriptions` package, which provides a simple **publish/subscribe API**.
19
+
To create a subscription using the code first approach, weuse the `@Subscription()` decorator and the `PubSub` class from the `graphql-subscriptions` package, which provides a simple **publish/subscribe API**.
20
20
21
-
The subscription handler takes care of **subscribing** to an event by calling `PubSub#asyncIterator`. This method takes a single argument, the `triggerName` which corresponds to an event topic name.
21
+
The following subscription handler takes care of **subscribing** to an event by calling `PubSub#asyncIterator`. This method takes a single argument, the `triggerName`, which corresponds to an event topic name.
22
22
23
23
```typescript
24
24
const pubSub =newPubSub();
@@ -33,8 +33,6 @@ export class AuthorResolver {
33
33
}
34
34
```
35
35
36
-
The current implementation also requires that the method handler name match the `triggerName`. That is, in the code sample above, the method name `commentAdded()` must match the `triggerName` passed in to `pubSub.asyncIterator('commentAdded')`.
37
-
38
36
> info **Hint** All decorators are exported from the `@nestjs/graphql` package, while the `PubSub` class is exported from the `graphql-subscriptions` package.
39
37
40
38
> warning **Note**`PubSub` is a class that exposes a simple `publish` and `subscribe API`. Read more about it [here](https://www.apollographql.com/docs/graphql-subscriptions/setup.html). Note that the Apollo docs warn that the default implementation is not suitable for production (read more [here](https://github.com/apollographql/graphql-subscriptions#getting-started-with-your-first-subscription)). Production apps should use a `PubSub` implementation backed by an external store (read more [here](https://github.com/apollographql/graphql-subscriptions#pubsub-implementations)).
@@ -47,7 +45,22 @@ type Subscription {
47
45
}
48
46
```
49
47
50
-
Topublishtheevent, usethe `PubSub#publish` method. This is often used within a mutation to trigger a client-side update when a part of the object graph has changed. For example:
This construct produces the same SDL as the previous code sample, but allows us to decouple the method name from the subscription.
60
+
61
+
### Publishing
62
+
63
+
Now, to publish the event, we use the `PubSub#publish` method. This is often used within a mutation to trigger a client-side update when a part of the object graph has changed. For example:
The `PubSub#publish` method takes a `triggerName` (again, think of this as an event topic name) as the first parameter, and an event payload as the second parameter. As mentioned, the subscription, by definition, returns a value and that value has a shape. Look again at the generated SDL for our `commentAdded` subscription:
79
+
80
+
```graphql
81
+
typeSubscription {
82
+
commentAdded(): Comment!
83
+
}
84
+
```
85
+
86
+
Thistellsusthatthesubscriptionmustreturnanobjectwithatop-levelpropertynameof `commentAdded` thathasavaluewhichisa `Comment` object. Theimportantpointtonoteisthattheshapeoftheeventpayloademittedbythe `PubSub#publish` method must correspond to the shape of the value expected to return from the subscription. So, in our example above, the `pubSub.publish('commentAdded', {{ '{' }} commentAdded: newComment {{ '}' }})` statement publishes a `commentAdded` event with the appropriately shaped payload. If these shapes don't match, your subscription will fail during the GraphQL validation phase.
87
+
88
+
### Filtering subscriptions
89
+
65
90
Tofilteroutspecificevents, setthe `filter` propertytoafilterfunction. Thisfunctionactssimilartothefunctionpassedtoanarray `filter`. Ittakestwoarguments: `payload` containingtheeventpayload (as sent by the event publisher), and `variables` takinganyargumentspassedinduringthesubscriptionrequest. Itreturnsabooleandeterminingwhetherthiseventshouldbepublishedtoclientlisteners.
To mutate the published event payload, set the `resolve` property to a function. The function receives the event payload (as sent by the event publisher) and returns the appropriate value.
78
105
79
106
```typescript
@@ -85,6 +112,8 @@ commentAdded() {
85
112
}
86
113
```
87
114
115
+
> warning **Note** If you use the `resolve` option, you should return the unwrapped payload (e.g., with our example, return a `newComment` object directly, not a `{{ '{' }} commentAdded: newComment {{ '}' }}` object).
116
+
88
117
If you need to access injected providers (e.g., use an external service to validate the data), use the following construction.
0 commit comments