Mutation response is undefined, but shouldn't be
See original GitHub issueIntended outcome:
Using Apollo Client and hooks, the typings tell me that the response from an operation will always be defined, but that response.data
might not be. As such, we always use response.data?.something
when unwrapping the response.
We use the built-in options for onCompleted
and onError
, and as such, do not wrap our calls in try/catch
assuming that any failures in the request will propagate to the onError
callback.
Actual outcome:
We get Sentry reports that says TypeError: undefined is not an object (evaluating 'n.data')
How to reproduce the issue:
const response = await ourMutation({
variables: {
input: {
id: someId,
otherVar: theValue
},
},
});
if (response.data?.ourMutation) {
setShowSuccess(true);
}
Versions
System:
OS: macOS 10.15.7
Binaries:
Node: 14.15.4 - /usr/local/bin/node
Yarn: 1.22.10 - ~/Documents/git/MMS-Ticket/node_modules/.bin/yarn
npm: 6.14.10 - /usr/local/bin/npm
Browsers:
Chrome: 88.0.4324.192
Safari: 14.0.3
npmPackages:
@apollo/client: 3.3.6 => 3.3.6
apollo: 2.31.0 => 2.31.0
react-apollo: 3.1.5 => 3.1.5
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Return value from mutation is empty and/or undefined
GraphQL (Apollo-Client in this case), expects an object formed like the response, you tell it you want. In this case: updateInventoryItem( ...
Read more >Mutations in Apollo Client - Apollo GraphQL Docs
When a mutation's response is insufficient to update all modified fields in your cache (such as certain list fields), you can define an...
Read more >Mutation & Revalidation - SWR
It is functionally equivalent to the global mutate function in the previous section but does not require the key parameter: import useSWR from...
Read more >GraphQL specification
A document may contain operations (queries, mutations, and subscriptions) as ... include additional arguments not defined in the interface field, but any ...
Read more >Mutation isn't always bad in JavaScript - DEV Community
The truth is React works based on referential equality, so its only opinion on mutation is that you shouldn't mutate an object if...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jcreighton - Eureka! 😄
Today, I accidentally stumbled across a way to reproduce the error. It turns out that it can be reproduced if you provide an
onError
callback to theuseMutation
config.I’ve set up an updated sandbox that produces the issue when you press the button:
https://codesandbox.io/s/late-sound-ettg7?file=/src/App.js
We have a default error handling function that displays a nice default error message for the user in the few cases where we don’t handle errors explicitly, which we provide to the
onError
callback in theuseMutation
/useQuery
configs. From the types and documentation, I would expect that in this caseresponse
will still be defined as the types indicate, butdata
will be null, anderrors
will instead be populated:@jcreighton Can you give any estimate as to when we can expect this to land in a patch? 😃 Thanks