I am using react-query for api calls in my react native app. And backend is based on graphql. For query and mutation requests there is no problem. I simply use useMutation and useQuery to fetch data. My problem is that I should notify user of new notifications that comes from graphql subscription. And I don't know how to use it with react-query. I couldn't find any documentation in react-query docs. thanks for any help.
1 Answer
Since GraphQL subscriptions are usually based on WebSockets, the best approach is to open a connection to the socket in an effect. When you get data, you can use queryClient.setQueryData.
You can read more about the topic here:
https://react-query.tanstack.com/community/tkdodos-blog#7-using-websockets-with-react-query
2 Comments
BeginnerProgrammer
I study the link you provided but I really don't know where to add my subscription query?
TkDodo
> "Additionally, you can setup an app-wide useEffect that connects you to your WebSocket endpoint." So I would do it in a useEffect in your App component ?