Intended outcome:
I have InMemoryCache with the setup below and every object ID is a unique global ID provided from a backend:
const cache = new InMemoryCache({
dataIdFromObject: obj => obj.id
});
This allows me to do the following:
mutate({
variables: {
input: { id, deleted }
},
update: (proxy, { data }) => {
proxy.writeFragment({
id,
fragment: SomeFragment,
data: data.propName.payload
});
}
})
Actual outcome:
The problem is, when I run the following code somewhere else, I'll get a warning You are using the simple (heuristic) fragment matcher, but your queries contain union or interface types. from HeuristicFragmentMatcher.prototype.match
// mutation DeleteSomething($input: DeleteSomethingInput!) {
// deleteSomething(input: $input) {
// id
// }
// }
mutate({
variables: {
input: { id }
}
});
I can remove dataIdFromObject: obj => obj.id from the InMemoryCache options and warning is gone but then I have to use proxy.writeFragment({ id: ``Typename:${id}`` ... which is inconvenient. If I understand the warning correctly, it's about a possibility of a wrong ID but if every object has a unique ID that warning in unnecessary.
Version
Would you have a suggestion how to avoid typing __typename manually for every write/readFragment call or how to prevent the warning if I keep dataIdFromObject as described above? Is it safe to ignore this warning for now?
I ran into this issue today. What is the best way to use dataIdFromObject with out getting this warning
warnOnce.js:14 You are using the simple (heuristic) fragment matcher, but your queries contain union or interface types.
Apollo Client will not be able to able to accurately map fragments.To make this error go away, use the IntrospectionFragmentMatcher as described in the docs: https://www.apollographql.com/docs/react/recipes/fragment-matching.html
We also guarantee that every GraphQL Type has an ID and this ID is globally unique for all records. I added a custom FragmentMatcher to suppress the warning provided in the default matcher. Matchers must implement FragmentMatcherInterface ( https://github.com/apollographql/apollo-client/blob/master/packages/apollo-cache-inmemory/src/types.ts#L82). Works ok so far.
export const apolloClient = new ApolloClient({
cache: new InMemoryCache({
dataIdFromObject: obj => obj.id,
addTypename: false,
fragmentMatcher: {
match: ({ id }, typeCond, context) => !!context.store.get(id)
}
}),
link: ApolloLink.from([
new ErrorLink(handleErrors),
new HttpLink({
uri: "/graphql",
credentials: "same-origin"
})
])
});
Any progress on it?
Thanks for reporting this. There hasn't been any activity here in quite some time, so we'll close this issue for now. If this is still a problem (using a modern version of Apollo Client), please let us know. Thanks!