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
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
+
48
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:
49
64
50
65
```typescript
@@ -60,26 +75,15 @@ async addComment(
60
75
}
61
76
```
62
77
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:
64
79
65
80
```graphql
66
81
typeSubscription {
67
82
commentAdded(): Comment!
68
83
}
69
84
```
70
85
71
-
Thistellsusthatthesubscriptionmustreturnanobjectwithapropertynameof `commentAdded` thathasavaluewhichisa `Comment` object. Theimportantpointtonoteisthatthepayloadofthe `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.
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.
83
87
84
88
### Filtering subscriptions
85
89
@@ -108,6 +112,8 @@ commentAdded() {
108
112
}
109
113
```
110
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
+
111
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