apollo icon indicating copy to clipboard operation
apollo copied to clipboard

onResult has *confusing* behavior

Open cndev123 opened this issue 9 months ago • 1 comments

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
  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. 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

cndev123 avatar May 30 '25 10:05 cndev123

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.

cndev123 avatar May 30 '25 11:05 cndev123