Resolvers should never destructively modify the contextValue argument. This ensures consistency across all resolvers and prevents unexpected errors.
I'd like some help unpacking this statement.
My issue is this:
setUserDetails
mutation which can update the user.setUserDetails
followed by sendWelcomeEmail
), the second mutation sees the original user details, not the updated ones unless I mutate the context.This leads to "unexpected errors" which was the thing we were trying to avoid in the first place.
So my question is: is it OK to mutate the context in a mutation resolver? Or is there another recommended approach to avoid this issue?
You seem to be quite aware of the issues that can originate from mutating the context value. I think the intention of the docs is to prevent people from using the context to pass around values. I think, ideally the context is created once and then never modified.
I think in your case, your context also somewhat serves as a cache (which is not uncommon), so I think it should be fine to invalidate or update cache entries after an update.
Nevertheless, I would consider the following:
{ id: 1, companyId: 2, role: 'USER' }
and load everything else from the db, when we need it. Your mutation is probably called very seldomly and does not slow down significantly, if you load the email from the database in the resolver.