Update data after mutations for queries with different variables
See original GitHub issueHello
I have difficulties with add/delete data after mutation, when data used in different components, that have queries with different variables. What is the best way for data update?
For example: I have something like this in Component1:
export const QUERY1 = gql`
query UsersNotFiltered {
usersList {
id
name
}
}
`;
graphql(QUERY1 )(Component1);
Component2:
export const QUERY2 = gql`
query UsersFilteredOrder($orderBy: OrderBy){
usersList(filter: { orderBy: $orderBy }) {
id
name
}
}
`;
graphql(QUERY2, { options: { variables: { orderBy: "name" } } } )(Component2);
Component3:
export const QUERY3 = gql`
query UsersFilteredSearch($search: Search){
usersList(filter: { search: $search}) {
id
name
}
}
`;
graphql(QUERY3, { options: { variables: { search: "apo" } } } )(Component3);
And I have a mutation to delete User in Component4:
import { QUERY1 } from 'Component1'
const MUTATION = gql`
mutation UserDelete($userId: ID!) {
userDelete(userId: $userId) {
success
}
}
`;
graphql(MUTATION, {
props: ({ mutate }) => ({
onDelete: (id) => mutate({
variables: { userId: id}
update: (store) => {
const data = store.readQuery({ query: QUERY1 });
_.remove(data.groups, ['id', id]);
store.writeQuery({ query: GROUPS_QUERY, data });
},
}),
}),
})(Component4);
The problem is that list is updated only for Component1 because Component2 and Component3 has different queries, the same problem if import QUERY2 and QUERY3
I can import all 3 queries and make writeQuery for each, but I think its very ugly and It should work in another way.
Also I know that I can refetchQueries
refetchQueries: [
{ query: QUERY1},
{ query: QUERY2, variables: { orderBy: ""} }
{ query: QUERY3, variables: { search: ""} }
],
but in that case I should know about variables for each of them, so that forces to take out variables to redux store and sometimes it’s not usefull
SO, the main question is what is the best practice to update all queries, connected with usersList in apollo store after mutations, when I have queries with different variables?
Version
- apollo-client@<1.9.3>
- react-apollo@<1.4.16>
Issue Analytics
- State:
- Created 6 years ago
- Comments:5
Top GitHub Comments
@VasiliKubarka did you reached a solution? I have similar problem
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions to Apollo Client!