Open sjdemartini opened 4 months ago
Hi @sjdemartini 👋 Thanks for opening this issue - it definitely sounds like the documentation should be updated, but in terms of the TS type of the return value from cache.writeFragment
I'll need to dig in a bit more and chat with the team.
We're coming up on a long weekend but I'll get back to you as soon as I can, thanks!
Issue Description
As of https://github.com/apollographql/apollo-client/pull/10895 and https://github.com/apollographql/apollo-client/pull/11206, the types for the field modifiers in
cache.modify
are explicit and more strict, where each field modifier is expected to return a list of references or objects when you specify the generic withcache.modify<MyObjectType>
.However, Apollo's documented examples lead to code that doesn't satisfy this more strict typing, such as https://www.apollographql.com/docs/react/caching/cache-interaction/#example-updating-the-cache-after-a-mutation:
Specifically, in the above,
newCommentRef
will beReference | undefined
based onwriteFragment
's longstanding type https://github.com/apollographql/apollo-client/blob/d502a69654d8ffa31e09467da028304a934a9874/src/cache/core/cache.ts#L312-L318.As such, if you update the above to use
cache.modify<CommentType>
, this leads to a TS error sinceundefined
is not an allowed return value from a field Modifier function:Two questions based on this:
const newCommentRef = cache.writeFragment(...)!
(i.e. add!
) to mark that thewriteFragment
return value won't beundefined
? It's not clear to me based on the docs whenwriteFragment
would returnundefined
, but I guess if it's possible to beundefined
with normal usage, we'd want to be more defensive and not simply override the type with the!
operator.Link to Reproduction
Not a runtime bug, just a simple TS error
Reproduction Steps
cache.modify<YourObjectType>
and a field modifier which returns a value fromcache.writeFragment
Reference | undefined
is not an allowed field modifier return value, despite documentation suggesting usage of this pattern@apollo/client
version3.10.4