question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mutation response is undefined, but shouldn't be

See original GitHub issue

Intended 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:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
extjbhlegocommented, Mar 17, 2021

@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 the useMutation 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 the useMutation/useQuery configs. From the types and documentation, I would expect that in this case

  1. the callback will be invoked
  2. response will still be defined as the types indicate, but data will be null, and errors will instead be populated: image
0reactions
extjbhlegocommented, Apr 9, 2021

@jcreighton Can you give any estimate as to when we can expect this to land in a patch? 😃 Thanks

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found