question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update data after mutations for queries with different variables

See original GitHub issue

Hello

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:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

6reactions
pedrocarmonacommented, Jan 17, 2018

@VasiliKubarka did you reached a solution? I have similar problem

6reactions
stale[bot]commented, Nov 13, 2017

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mutations in Apollo Client - Apollo GraphQL Docs
An update function attempts to replicate a mutation's back-end modifications in your client's local cache. These cache modifications are broadcast to all ......
Read more >
Updating Queries on Mutation (for queries issued ... - GitHub
So, suppose I want to move an item from one view to the other: when I mutate the item in "/inbox", my updateQueries...
Read more >
How to update the Apollo Client's cache after a mutation
Basically, the Apollo Client automatically inspects the queries and mutations' traffic on your behalf and uses the latest version of the data it ......
Read more >
Updates from Mutation Responses | TanStack Query Docs
When dealing with mutations that update objects on the server, it's common for the new object to be automatically returned in the response...
Read more >
Refetching Queries after Mutations - Thinkster.io
The other way to update data after a mutation is using the Apollo cache, and we'll cover that in the next tutorial. To...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found