apollo
apollo copied to clipboard
onResult has *confusing* behavior
Describe the bug The onResult hook of useQuery is called in a (for me) confusing manner. Its behavior is not documented.
// call without any non-default fetch-policy
const { onResult } = useQuery(query, ...);
onResult((...agrs) => console.log(args))
// when no result in cache and call goes to server, onResult is called twice
// 1st with no data prop, loading === true and partial == true
// 2nd with data prop, loading === false, partial === undefined
// when result in cache, onResult is only called once
// with data prop, loading === false, partial === undefined
// call without any non-default fetch-policy
const { onResult } = useQuery(query, ..., { fetchPolicy: "no-cache"});
onResult((...agrs) => console.log(args))
// onResult is only called once
// with data prop, loading === false, partial === undefined
- Go to '...'
- Click on '....'
- Scroll down to '....'
- See error
Expected behavior A clear and concise description of what you expected to happen.
Versions vue: 3.5.16 vue-apollo: 4.2.2 @apollo/client:3.13.8
I tested this a bit more, and I guess onResult is called whenever there is a result given back from either the cache or the server. Even when the result is undefined or not different than the last result.
So when using cache-first, it is called when the result from cache is coming in, even if it is undefined. It is then called a second time when the server result comes in.