The issue:
Hello, so I've been using apollo-client for a while on my ReactJS application. I've just noticed that sometimes when I use the useQuery
hook, the execution completely ignores the skip
argument, and just proceed with the onCompleted
(albeit without any data). Interestingly enough, it also does not make an API request to my endpoint either. However, if I just set skip
to false, then everything works properly (as expected).
Also, this doesn't happen every time I use useQuery
with skip
, it seems working on some and not others.
Why is apollo ignoring the skip
argument and just executing onCompleted
immediately with null data? Is there a fix (other than useLazyQuery
)?
Code example:
const userQuery = useQuery(GET_USER, {
variables: {
value: userId,
},
skip: true,
onCompleted: props => {
console.log(`props => `, props)
const {
user: { type, role, firstName, lastName, permissions },
} = props;
setValue('firstName', firstName);
setValue('lastName', lastName);
setValue(
'type',
userTypes.find(({ value }) => value === type),
);
setValue(
'role',
userRoles.find(({ value }) => value === role),
);
},
});
Additional notes:
• The output of logging the props from the onCompleted function is props => undefined
.
• I added skip: true
just to demonstrate the fact that it actually isn't working.
• If I log userQuery
itself, then immediately on the first log, userQuery.called
is equal to true (but like I said previously, no API call actually had been executed)
Dependencies
"@apollo/react-hooks": "^3.1.5",
"apollo-cache-inmemory": "^1.6.3",
"apollo-client": "^2.6.10",
"apollo-datasource-rest": "^0.6.6",
"apollo-link": "^1.2.13",
"apollo-link-context": "^1.0.19",
"apollo-link-error": "^1.1.12",
"apollo-link-rest": "^0.7.3",
"apollo-link-retry": "^2.2.15",
"apollo-link-token-refresh": "^0.2.7",
"apollo-upload-client": "^11.0.0",
"react": "16.10.2",
"react-apollo": "^3.1.3",
Note:
useLazyQuery
seems to be working properly, so as a workaround for this in the meantime you can use that in combination with useEffect
to achieve similar results.