Skip to content

Commit 29fdf41

Browse files
committed
docs(subscription.md) correction re: subscription handler name
1 parent 9e9ca89 commit 29fdf41

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

content/graphql/subscriptions.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ GraphQLModule.forRoot({
1616

1717
#### Code first
1818

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, we use the `@Subscription()` decorator and the `PubSub` class from the `graphql-subscriptions` package, which provides a simple **publish/subscribe API**.
2020

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.
2222

2323
```typescript
2424
const pubSub = new PubSub();
@@ -33,8 +33,6 @@ export class AuthorResolver {
3333
}
3434
```
3535

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-
3836
> info **Hint** All decorators are exported from the `@nestjs/graphql` package, while the `PubSub` class is exported from the `graphql-subscriptions` package.
3937
4038
> 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,7 @@ type Subscription {
4745
}
4846
```
4947

50-
To publish the event, 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:
48+
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:
5149

5250
```typescript
5351
@@filename(posts/posts.resolver)
@@ -57,11 +55,34 @@ async addComment(
5755
@Args('comment', { type: () => Comment }) comment: CommentInput,
5856
) {
5957
const newComment = this.commentsService.addComment({ id: postId, comment });
60-
pubSub.publish('commentAdded', newComment);
58+
pubSub.publish('commentAdded', { commentAdded: newComment });
6159
return newComment;
6260
}
6361
```
6462

63+
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. Note that the subscription, by definition, returns a value and that value has a shape. Look again at the generated SDL for our `commentAdded` subscription:
64+
65+
```graphql
66+
type Subscription {
67+
commentAdded(): Comment!
68+
}
69+
```
70+
71+
This tells us that the subscription must return an object with a property name of `commentAdded` that has a value which is a `Comment` object. The important point to note is that the payload of the `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.
72+
73+
The above requirement can create undesired coupling between the `@Subscription()` method handler name (`commentAdded()`) and the event payload. To specify a name for the subscription that is independent of the method handler name, use the `name` option in the `@Subscription()` decorator:
74+
75+
```typescript
76+
@Subscription(returns => Comment, {
77+
name: 'commentAdded',
78+
})
79+
addCommentHandler() {
80+
return pubSub.asyncIterator('commentAdded');
81+
}
82+
```
83+
84+
### Filtering subscriptions
85+
6586
To filter out specific events, set the `filter` property to a filter function. This function acts similar to the function passed to an array `filter`. It takes two arguments: `payload` containing the event payload (as sent by the event publisher), and `variables` taking any arguments passed in during the subscription request. It returns a boolean determining whether this event should be published to client listeners.
6687

6788
```typescript
@@ -74,6 +95,8 @@ commentAdded(@Args('title') title: string) {
7495
}
7596
```
7697

98+
### Mutating subscription payloads
99+
77100
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.
78101

79102
```typescript

0 commit comments

Comments
 (0)