Description
Hello,
Description
One important feature of a graphql-client to me is the ability to refetch/update the data of a query after one or multiple mutations executions.
I really liked the idea of "a query subscribes to one or more mutations" here (see onMutation parameter) : https://github.com/arackaf/micro-graphql-react#building-queries
I started playing with the idea that we could leverage the useEffect
wrapping the query fetching call and pass extra inputs to its second argument to automatically refetch the query again.
The extra inputs would take the form of a list of mutations results so whenever a subcribed mutation happens, the extra inputs changed and the query is fired again.
Suggested implementation
useQuery(GET_TODO, { id }, { refetch: ['addTodo', 'updateTodo', 'deleteTodo'] })
I made a working example of the idea here : https://codesandbox.io/s/m3nq0315m9
The general idea is to maintain of global registry of mutations results in the Context.
From here, useQuery
could accept a refetch
options taking a list of mutations names. Then it's just a matter of mapping the list of mutations names into a list of mutations results, and voilà, we have our extra inputs to give to useEffect
.
The approach is naive but quite effective, we could complexify it a bit by accepting a tuple with an extra function given the mutation result, so we have control over the generated input given to useEffect :
useQuery(GET_TODO, { id }, { refetch: [['removeTodo', ({ id: removedId }) => id === removedId]] })