Skip to content

Commit efab4dd

Browse files
committed
update from comments
1 parent 29fdf41 commit efab4dd

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

content/graphql/subscriptions.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ type Subscription {
4545
}
4646
```
4747

48+
Note that subscriptions, by definition, return an object with a single top level property whose key is the name of the subscription. This name is either inherited from the name of the subscription handler method (i.e., `commentAdded` above), or is provided explicitly by passing an option with the key `name` as the second argument to the `@Subscription()` decorator, as shown below.
49+
50+
```typescript
51+
@Subscription(returns => Comment, {
52+
name: 'commentAdded',
53+
})
54+
addCommentHandler() {
55+
return pubSub.asyncIterator('commentAdded');
56+
}
57+
```
58+
59+
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+
4863
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:
4964

5065
```typescript
@@ -60,26 +75,15 @@ async addComment(
6075
}
6176
```
6277

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:
78+
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:
6479

6580
```graphql
6681
type Subscription {
6782
commentAdded(): Comment!
6883
}
6984
```
7085

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-
```
86+
This tells us that the subscription must return an object with a top-level property name of `commentAdded` that has a value which is a `Comment` object. The important point to note is that the shape of the event payload emitted by 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.
8387

8488
### Filtering subscriptions
8589

@@ -108,6 +112,8 @@ commentAdded() {
108112
}
109113
```
110114

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+
111117
If you need to access injected providers (e.g., use an external service to validate the data), use the following construction.
112118

113119
```typescript

0 commit comments

Comments
 (0)