-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache redirects don't work with interfaces specified by possibleTypes #382
Comments
Hi @bluepichu 👋🏻 thanks for opening this issue and thanks for your patience. First, it's totally understandable that a user would expect |
Thanks for getting back to me! I think this is definitely worth looking into in the future since the expectation that diff --git a/src/index.jsx b/src/index.jsx
index afc7a5f..9a2e416 100644
--- a/src/index.jsx
+++ b/src/index.jsx
@@ -127,6 +127,9 @@ const client = new ApolloClient({
}
}
}
+ },
+ Widget: {
+ keyFields: (object) => `Widget:${object.id}`
}
},
possibleTypes: { Due to the That said, it wasn't particularly obvious from the documentation or the name of the field that you could do this with |
Issue Description
I asked this question on the Discord server and got no response. I think this is a bug, but it's possible I'm misunderstanding if/how these two features are supposed to interact.
I have a schema that looks like this:
I have a page where I'm fetching a full list of available widgets via
query { widgets { id name } }
, and then rendering the list of widgets. However, the "widget card" component used within that list is written in such a way that it can be reused in multiple locations, possibly including some where we don't already have the relevant data, and therefore is in charge of fetching its own data viaquery($id: ID!) { widget(id: $id) { id name } }
. Naturally, this means that we end up round-tripping once for the list and then again for each list item.The documented solution to this is to use a cache redirect, so I've set up my cache like this:
My understanding is that this should allow the individual cards to fetch their data directly from the cache since all of the data they're requesting is already being loaded as part of the list query. However, I still see a second network request for each card going through. Stepping through the code in the developer tools, I noticed that when
id
isabc
, then Apollo is reaching an error state because there is no value in the cache with keyWidget:abc
(in particular, the messageDangling reference to missing Widget:abc object
is being propagated up toObservableQuery.getCurrentResult
). This is because the normalized key isFooWidget:abc
since that's the actual concrete type of the widget in question. It seems like thepossibleTypes
configuration is not being respected with regards to checking for that key in the cache.Link to Reproduction
https://github.com/bluepichu/react-apollo-cache-issue
Reproduction Steps
possibleTypes
) was loaded both as a complete list and as the individual items, despite the fact that the list query should provide sufficient information for the individual item queries to use the cacheThe text was updated successfully, but these errors were encountered: