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

[Question] Syntax Errors in onCompleted causes onError to occur #12008

Open
dep opened this issue Aug 16, 2024 · 2 comments
Open

[Question] Syntax Errors in onCompleted causes onError to occur #12008

dep opened this issue Aug 16, 2024 · 2 comments
Labels
🏓 awaiting-team-response requires input from the apollo team

Comments

@dep
Copy link

dep commented Aug 16, 2024

We ran into this in our platform this week and we thought it was interesting. Essentially a syntax error in onCompleted caused the JS to run onError. Here's an example:

const [createDashboard, { loading: createLoading }] = useMutation(CreateDashboard, {
  // Mutation was successful so onCompleted fires...
  onCompleted({ 
    createDashboard: { 
      dashboard: { dashboardUrl } 
    } 
  }) {
    toggleModal();
    // There was an error in this function...
    navigateToDashboard(dashboardUrl);
  },
  onError: () => {
    // Since there was an error in the function above, this code ran...
    showToast("Unable to create dashboard, please try again.", { variant: "error" });
  }
});

The Mutation was fine. There was a 200 from the GQL event. We found it interesting that since there was a JS error in onCompleted, the execution jumped over into onError, which made us initially feel like there was a GQL problem. Overall it just felt unintuitive.

I imagine there was probably a lot of thought that went into this behavior. Curious your thoughts!

@jerelmiller
Copy link
Member

Hey @dep 👋

You're right, I wouldn't expect this myself! I don't think this is intentional behavior, more I think this is an unintended side effect of the way the code is currently written.

Looking at the code, I can see the onCompleted call is made in the .then callback to the promise returned by the mutate function:

onCompleted?.(
response.data!,
clientOptions as MutationOptions<TData, OperationVariables>

We use .catch here to handle rejected promises from the mutate call, but I think that syntax error is getting caught up in that .catch callback since .catch will handle errors from any previous promise. From there, we call onError if its available:

if (onError) {
onError(
error,
clientOptions as MutationOptions<TData, OperationVariables>
);

I think we could probably improve this by moving the error handling to the 2nd argument of the first .then so that it only handles errors created by the mutate promise. Let me take this to our next team meeting on Monday and double check that we would be ok with this approach.

Thanks for reporting!

@jerelmiller jerelmiller added the 🏓 awaiting-team-response requires input from the apollo team label Aug 16, 2024
@dep
Copy link
Author

dep commented Aug 21, 2024

Appreciate that, and look forward to the follow-up @jerelmiller !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏓 awaiting-team-response requires input from the apollo team
Projects
None yet
Development

No branches or pull requests

2 participants