Replies: 3 comments 1 reply
-
|
+1 @ItzaMi Did you ever figure it out? |
Beta Was this translation helpful? Give feedback.
1 reply
-
|
@ItzaMi I did figure it out. So I actually did find the proper way to do this fully typed without hooks: Make sure to include a .graphql subscription in your code: subscription reportWeekUpdated {
reportWeekModelUpdated {
...ReportWeekFragment // <--- use fragments always, that way you get auto created typed interfaces as well.
}
}
```
Then when you run codegen it will generate the required types as below.
```typescript
subscribeToMore<ReportWeekUpdatedSubscription, ReportWeekUpdatedVariables>({ // <--- ReportWeekUpdatedSubscription generated by codgen
document: ReportWeekUpdatedDocument, // <--- Document also done by code gen
updateQuery: (prev, data) =>
produce(prev, (draft) => { // <--- produce is from immerJS to make immutable updates easy.
const newGroup = data.subscriptionData.data.reportWeekModelUpdated; // <--- typed autocomplete
...work
return draft;
}),
});
```
|
Beta Was this translation helpful? Give feedback.
0 replies
-
|
A second way is to use the hooks which require less typing of types. const { data, refetch, updateQuery } = useGetItemQueriesQuery();
useItemUpdateSubscriptionsSubscription({
onSubscriptionData: (x) => {
const incomingValue = x.subscriptionData.data?.itemUpdated;
if (!incomingValue) return draft;
updateQuery((prev) =>
produce(prev, (draft) => {
for (const incoming of incomingValue) {
if (incoming.reason === SubscriptionUpdateReason.Delete) {
draft.itemBoxs = draft.itemBoxs.filter((x) => x.id !== incoming.itemBox.id);
} else {
const found = draft.itemBoxs.findIndex((x) => x.id === incoming.itemBox.id);
if (found !== -1) {
draft.itemBoxs[found] = incoming.itemBox;
} else {
draft.itemBoxs.push(incoming.itemBox);
}
}
}
return draft;
}),
);
},
});This way is fully typed without having to import them yourselves. I'd still recommend Immer for the easy immutable updates. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm currently trying to implement subscriptions into a project but I'm having some issues understand how I need to adapt my setup with the autogenerated hooks.
Initially my code looked like this:
And it's query is
Now I want to implement a subscription
And connect my
eventThreatsLiveCountto it but I'm unsure how I should proceed.I've looked into
subscribeToMorebut can't figure out what I should use ondocument:, for example.Could someone help me?
Beta Was this translation helpful? Give feedback.
All reactions