Avoid sending extra query request after invalidating the cache.
See original GitHub issueAfter 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:
- Created 3 years ago
- Comments:8 (8 by maintainers)
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 inupdates
😃That’s outstanding, so I don’t even need to duplicate the code. Perfect!