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

Changed return type of useMutation when ignoreResult is explicitly set to true to hide unset result #12277

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Cellule
Copy link

@Cellule Cellule commented Jan 15, 2025

We've encountered numerous bugs around this where the default (somehow) was to set ignoreResult: true when calling useMutation
Then devs would try to use data or loading from the tuple without realizing it would never be set.
I've been using this patch for a while and it does help avoid bugs

Copy link

changeset-bot bot commented Jan 15, 2025

🦋 Changeset detected

Latest commit: ac38202

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@apollo/client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

netlify bot commented Jan 15, 2025

👷 Deploy request for apollo-client-docs pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit ac38202

@svc-apollo-docs
Copy link

svc-apollo-docs commented Jan 15, 2025

✅ Docs preview has no changes

The preview was not built because there were no changes.

Build ID: b33425ab3a332267bc7cbdd2

@Cellule Cellule force-pushed the useMutation-ignoreResults branch from ef8b036 to ac38202 Compare January 15, 2025 17:08
@jerelmiller
Copy link
Member

Hey @Cellule 👋

These are on my TODO list to review tomorrow (Friday). Just wanted you to know I saw these come in and will get to these very soon!

Copy link
Member

@jerelmiller jerelmiller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is great and I do agree it should help prevent some crashes due to the types being wrong.

I had one minor change for accessing fields that are there at runtime even with ignoreResults: true. Once that is updated, I'd be happy to get this into the next release. Thanks for the contribution!

): [
MutationFunction<TData, TVariables, TContext, TCache>,
// result is not reliable when ignoreResults is true
Pick<MutationResult<TData>, "reset">,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we still return loading, called, and client properties even when ignoreResults are set. They just happen to use their initial values:

const [result, setResult] = React.useState<Omit<MutationResult, "reset">>({
called: false,
loading: false,
client,
});

Can we include those properties here as well? I see no reason not to be able to access them, even if loading or called never changes.

// TODO This FetchResult<TData> seems strange here, as opposed to an
// ApolloQueryResult<TData>
) => Promise<FetchResult<MaybeMasked<TData>>>,
mutate: MutationFunction<TData, TVariables, TContext, TCache>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 I have no idea why this wasn't that way to begin with. Good find!

expectTypeOf(result).toMatchTypeOf<{ reset: () => void }>();
expectTypeOf(result).not.toMatchTypeOf<MutationResult<Mutation>>();
expectTypeOf(mutate()).toMatchTypeOf<Promise<FetchResult<Mutation>>>();
const {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update this to check client and called as well?

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 this pull request may close these issues.

3 participants