Comments (2)
Hi @bluepichu 👋🏻 thanks for opening this issue and thanks for your patience. First, it's totally understandable that a user would expect possibleTypes
to factor into a toReference
so thanks for this feedback. I believe that updating the default behavior would be a breaking change so I don't see that happening in version 3.x. That said, this is an interesting idea and something we want to explore further 🙏🏻
from apollo-feature-requests.
Thanks for getting back to me!
I think this is definitely worth looking into in the future since the expectation that toReference
would work with inheritance is fairly reasonable, but in the meantime I've found a suitable workaround: using the keyFields
type policy to customize the cache id. In my example repo, I just made this diff:
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 typePolicy
inheritance enabled by possibleTypes
, FooWidget
and BarWidget
inherit this keyFields
configuration and have cache keys of the form Widget:<id>
instead of FooWidget:<id>
or BarWidget:<id>
. This makes the cache lookups resolve properly, and in my case is sufficient because it's guaranteed that widgets (even those of different types) will never have conflicting ids.
That said, it wasn't particularly obvious from the documentation or the name of the field that you could do this with keyFields
; the docs made it sound like this behavior could only be accomplished via dataIdFromObject
. I only figured this out because I was poking around in the TS types while checking the type of something unrelated. It might be good to add an example of using a function to fully specify the cache key in this section of the documentation, and might even be worth considering changing the name of the field in a future version since in this case the function generates the cache key directly rather than having anything in particular to do with fields of the object as the name suggests.
from apollo-feature-requests.
Related Issues (20)
- Remove React dependency when importing gql from @apollo/client HOT 2
- Support `select` QueryHookOptions in `useQuery`
- @defer timeout in clients HOT 3
- Allow passing in a client object to the `useFragment` hook HOT 2
- Remove "React" from the docs and keep it generic HOT 1
- Support skipToken in useReadQuery HOT 6
- Support for a Cache type policy when field is Garbage Collected HOT 2
- Add cache eviction of a query via query DocumentNode HOT 1
- Run a callback before the internal state of useMutation is updated.
- Local Resolvers: Support returning errors and data together for partial results
- Dependency Dashboard
- Revisit `refetchQueries` API
- Add an option to MockProvider to throw when a request fails to match a mock
- Please add polyfill for `globalThis` as it breaks compatibility for Chrome < v71 HOT 2
- Refetch on window focus HOT 2
- Drop `ApolloClient.query()` in next major HOT 1
- Keyword search in Job postings filter HOT 1
- warnAboutDataLoss fails with stringifying error and breaks application
- Query consolidation HOT 1
- Compare variables based on their serialized value HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from apollo-feature-requests.