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.

How to prevent apollo from updating cache with child objects in query?

See original GitHub issue

I apologize in advance for posting this question here but I really don’t want to sign up for another website IE spectrum for asking a singular question.

I have a query for a Reading object that goes something like this

readings(queryObject: $queryObject) {
      items {
        id
        meeting {
          id
          startDateTime
          endDateTime
        }
        nomination {
          id
          book {
            name
            author
            imageUrl
          }
          pitchedByFullName
          date
        }
        ...
      }
      hasMore
    }

It’s a big object with lots of children. In a different part of my application I make a query for a Meeting object. As part of this query I need two pieces of specific information about the Reading object

meetings(queryObject: $queryObject) {
      items {
        id
         ...
        amIGoing
        reservationId
        readings {
          id
          nomination {
            book {
              imageUrl
            }
          }
        }
      }

When I do this query apollo is going to clear out the cache for the Reading object with that id and replace it with this partial fetch. How do I prevent this from happening? One of the great things about GraphQL is that I can tailor queries to get just the information that I need but your caching sends the message that if i get the id of the object I better get every field I could possibly want because that’s what will end up in the cache.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
maheshinder19commented, Sep 11, 2020

Is it possible to make Apollo smarter? If I query 3 out of 10 fields on an object and then later on query 10 out of 10, can Apollo understands that the second query requires more fields and not replace the object but update the object with a new query?

0reactions
xinghulcommented, Sep 6, 2020

I would say that updating the cache automatically with child objects, based on the __typename and id, do more good than harm. Often times you will have mutations that have impacts on multiple queries, and it’s super hard to manually update all the queries affected by a single mutation.

In your case, if you don’t want it to update the cache automatically, maybe don’t include the id in your child document schema?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reading and writing data to the cache - Apollo GraphQL Docs
Access the fields of any cached object without composing an entire query to reach that object. Directly modifying cached fields, cache.modify, Manipulate cached...
Read more >
React Apollo - prevent automatic invalidating cached object ...
I'm implementing graphql client side implementation with React and Apollo. Situation. One of the queries created in server side is a wrapper ...
Read more >
Advanced Topics on Caching – Angular
If you want to clear the store but don't want to refetch active queries, use client.clearStore() instead of client.resetStore() . Cache ...
Read more >
Managing State with React and Apollo Client | DoltHub Blog
We use Apollo Client and hooks to query and mutate data throughout DoltHub. ... reusing fragments, and manually updating the cache.
Read more >
Avoiding refetch to update Apollo Client cached data (React)
const GET_POST = gql` query GetPost($id: ID!) { post(id: $id) { id title description } } ` const UPDATE_POST = gql` mutation UpdatePost( ......
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