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.

useMutation data is always undefined, only works in onCompleted

See original GitHub issue

Intended outcome: I am using the below mutation to update a tile object. The data in the onCompleted callback is correctly returning the updated data object. The data I’m returning in my wrapper is always undefined now. This seems to have recently changed and I’m not sure why

const useUpdateTile = () => {
  const [mutation, { data, loading, error }] = useMutation(UPDATE_TILE, {
    errorPolicy: 'all',
    onCompleted: (data) => {
      console.log('LOG: mutation updateTile completed', data);
    },
  });

  console.log('LOG: mutation updateTile', data);

  return {
    updateTile: (input) => {
      mutation({
        variables: {
          input,
        },
      });
    },
    loading,
    error,
    data: data?.updateTile || {},
  };
};

Screen Shot 2021-01-08 at 2 52 48 PM

Actual outcome:

I expect the data returned from the useMutation to contain the updated data object instead of being undefined. This was recently working as intended.

How to reproduce the issue: Haven’t been able to replicate in a codesandbox environment, it must be some other factor. https://codesandbox.io/s/competent-visvesvaraya-ifzp9?file=/src/App.js

Versions

System:
    OS: macOS 10.15.6
  Binaries:
    Node: 12.18.3 - ~/.nvm/versions/node/v12.18.3/bin/node
    npm: 6.14.10 - ~/mycode/node_modules/.bin/npm
  Browsers:
    Chrome: 85.0.4183.102
    Firefox: 68.11.0
    Safari: 13.1.2
  npmPackages:
    @apollo/client: 3.3.6 => 3.3.6 

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
brainkimcommented, Aug 18, 2021

Closing because it seems like this has been fixed at some point by some person 😶

3reactions
dylanwulfcommented, Jun 17, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

useMutation always returns data undefined - Stack Overflow
useMutation should return a function to call to execute the mutation. ... Use the onCompleted callback and remember to pass the data object:...
Read more >
Mutations in Apollo Client - Apollo GraphQL Docs
The useMutation React hook is the primary API for executing mutations in an Apollo application. To execute a mutation, you first call useMutation...
Read more >
useQuery | TanStack Query Docs
const { data, ... Required, but only if no default query function has been defined See ... If set to "always" , the...
Read more >
Apollo useMutation React hook - Hasura
We will use the Apollo Client useMutation from @apollo/client in React app as an example to insert new data and update cache locally...
Read more >
Should I Directly Access Data From the Apollo Client or From ...
Therefore, you would need to check if data is undefined or not ... you only perform the checks for the data inside the...
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