Reputation: 9810
I am using graphql subscriptions to get a stream of live messages. The question I have is how to combine this with a query as there are messages from the past.
My example is I have a MessagesQuery
component that fetches all previous messages, but to display newer messages I am using a MessageSubscription
component.
Can anyone explain the best approach for this? I found this GitHub issue but unfortunately no feedback has been. given.
Upvotes: 0
Views: 778
Reputation: 84807
That issue describes the typical approach -- use the Query
component or useQuery
hook and call subscribeToMore
to tack on a subscription. That user's problem seemed to be that the subscription unsubscribes on unmount but that is appropriate and expected behavior.
Example usage:
const { subscribeToMore, ...result } = useQuery(MESSAGES_QUERY, otherOptions)
subscribeToMore({
document: MESSAGES_SUBSCRIPTION,
updateQuery: (prev, { subscriptionData }) => {
// merge the subscription data into the query result
},
})
See the docs for additional details.
Upvotes: 1