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.

Avoid sending extra query request after invalidating the cache.

See original GitHub issue

After I use cache.invalidate, there is an extra query request sent that I would like to avoid.

I have the following query in my app:

query($pageId: Int!) {
  page(pageId: $pageId) {
    id
    title
    modules {
      id
      type
      config
    }
  }
}

After I delete the module, I would like to update the cache optimistically:

cacheExchange({
  schema: introspection,
  optimistic: {
    removeModule: ({ moduleId }, cache) => {
      cache.invalidate({ __typename: "Module", id: moduleId })
    }
  },
}),

But the problem is, when I delete a module, a new request is sent to fetch the page again. I’ve seen in docs, that using cache.invalidate may lead to additional request unless I use schema awareness. But in my case, I use schema awareness and the request is still sent.

Here it is my schema definition for the additional context:

type Page {
  id: Int!
  title: String!
  modules: [Module!]!
}

type Module {
  id: Int!
  type: ModuleType
  config: JSONObject!
}


extend type Mutation {
  removeModule(moduleId: Int!): Boolean
}

I assume, that it may happen because module list element type is marked as required in the modules field definition - [Module!]!, but if I make it optional([Module]!), I get null value in the array after invalidation which is undesired behavior as well.

Any way I can make the required item to disappear from the array without making an additional request since I exactly know what’s the schema should look like without making a new query?

Best wishes

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
kittencommented, Sep 3, 2020

Just a quick note; the updater will also run for optimistic updates (as long as there’s an optimistic function to provide this update), so you can also just return true there and do the removal from the list only in updates 😃

0reactions
aiven715commented, Sep 3, 2020

That’s outstanding, so I don’t even need to duplicate the code. Perfect!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Should manually updating the cache always be the preferred ...
In the docs I see that there are basically two ways to update the cache after a mutation: Query invalidation (https://react-query.tanstack.com/ ...
Read more >
RFC: Cache resolve & invalidate with partial args · Issue #2713
I'm removing an article from folder 187 . I'm not too interested in the page it is at, all I want is to...
Read more >
Query Invalidation | TanStack Query Docs
When a query is invalidated with invalidateQueries , two things happen: It is marked as stale. This stale state overrides any staleTime configurations...
Read more >
Algorithm for automatic cache invalidation - Google Groups
If we have two queries A and B, such that A is a write (DELETE or INSERT), and B is a read (SELECT),...
Read more >
Cache-Control - HTTP - MDN Web Docs
The Cache-Control HTTP header field holds directives (instructions) — in both requests and responses — that control caching in browsers and ...
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