I have a <Query />
in my Home.js
file
Home.js
<Query
query={GET_TODOS_BY_PRODUCT}
variables={{ id: state.get("selectedProduct.id"), completed: true }}
>
{({ data: { product } }) => {
return <Main todos={product.todos} hashtag={product.hashtag} />;
}}
</Query>
In my Main.js
file I have <Mutation />
component -
Main.js
<Mutation
key={v4()}
mutation={SWITCH_SELECTED_PRODUCT}
refetchQueries={() => {
console.log("refetchQueries", product.id);
return {
query: GET_TODOS_BY_PRODUCT,
variables: { id: product.id }
};
}}
>
{switchSelectedProduct => (
<Product
onClick={() => {
switchSelectedProduct({
variables: { id: product.id, name: product.name }
});
}}
highlight={
data.selectedProduct
? product.name === data.selectedProduct.name
: i === 0
}
>
<Name>{product.name}</Name>
</Product>
)}
</Mutation>
When switchSelectedProduct
is called inside <Mutation />
component, it runs refetchQueries
as I see the console.log("refetchQueries", product.id);
statement but I don't see the updated results in the <Query />
component in Home.js
file.
How do I tell <Query />
component in Home.js
to get notified when refetchQueries
is run in Main.js
file?
Any suggestions?
I solved it with some help. There is apparently a bug in
refetchQueries
as it does not update data from another component. ButrefetchQueries
was not needed at all.I wrapped my
Home
component with anotherQuery
like:Home.js
Then I also wrapped my
Main
component with anotherQuery
so it changes state when mutation is applied. This way you don't needrefetchQueries
at all. Just wrap yourMutation
orQuery
component whenMutation
from another or same component is performed.Main.js
From docs:
refetchQueries: (mutationResult: FetchResult) => Array<{ query: DocumentNode, variables?: TVariables} | string>
, so probably you need to return an array instead of just the object