Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v4: onResult is called on error, but not if setting errorPolicy is none (which should be default) #1316

Closed
SimonSimCity opened this issue Feb 8, 2022 · 1 comment · Fixed by #1318

Comments

@SimonSimCity
Copy link

SimonSimCity commented Feb 8, 2022

Describe the bug

When running a query using useQuery you get an object containing the onResult function. Using this function you can register function to be called - well - on a result I thought. Following the typings, I happily went on, until I discovered that the data property sometimes is null ... which - according to the docs (https://www.apollographql.com/docs/react/data/queries/#result) - should only happen if the value is set to another value but none. According to the docs, none is the default value, which seems to be different on this package.

I suspect these lines here to be responsible:

if (currentOptions.value?.errorPolicy !== 'none') {
processNextResult((query.value as ObservableQuery<TResult, TVariables>).getCurrentResult())
}

'Cause they do have a difference if the property errorPolicy is set or not. I've tested it, and in my tests this property was undefined, which is why I get a call on my function registered at onResult.

To Reproduce

const { onResult } = useQuery(
  gql`
    {
      queryThatDoesNotExistOnTheServer
    }
  `,
  // { errorPolicy: "none" } // <-- Apply this line and you'll see that the function registered at onResult is not called
);

onResult((queryResult) => {
  console.log(queryResult)
});

Expected behavior

The function registered at onResult should only be called if there is a valid result - as described in the manual: https://v4.apollo.vuejs.org/guide-composable/error-handling.html#error-handling

In fact, if I explicitly set the errorPolicy to none, it works as expected.

Versions
vue: 3.2.29
@vue/apollo-composable: 4.0.0-alpha.16
@apollo/client: 3.5.8

Additional context

Is there any way to set a general errorPolicy for all queries?

@SimonSimCity
Copy link
Author

If someone else stumbles upon this, we're currently using the following work-around:

const { result } = useQuery(
  gql`
    {
      queryThatDoesNotExistOnTheServer
    }
  `
);

watch(result, (value) => {
  console.log(value)
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant