useMutation data is always undefined, only works in onCompleted
See original GitHub issueIntended 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 || {},
};
};
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:
- Created 3 years ago
- Reactions:5
- Comments:8 (3 by maintainers)
Top 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 >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
Closing because it seems like this has been fixed at some point by some person 😶
@sfratini The change in that PR is available in v3.3.16: https://github.com/apollographql/apollo-client/blob/main/CHANGELOG.md#apollo-client-3316