-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Changed return type of useMutation when ignoreResult is explicitly set to true to hide unset result #12277
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@apollo/client": patch | ||
--- | ||
|
||
Changed return type of useMutation when ignoreResult is explicitly set to true to avoid using unset values of the result |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ import * as React from "rehackt"; | |||||||||||
import type { DocumentNode } from "graphql"; | ||||||||||||
import type { TypedDocumentNode } from "@graphql-typed-document-node/core"; | ||||||||||||
import type { | ||||||||||||
MutationFunction, | ||||||||||||
MutationFunctionOptions, | ||||||||||||
MutationHookOptions, | ||||||||||||
MutationResult, | ||||||||||||
|
@@ -69,6 +70,38 @@ import { useIsomorphicLayoutEffect } from "./internal/useIsomorphicLayoutEffect. | |||||||||||
* @param options - Options to control how the mutation is executed. | ||||||||||||
* @returns A tuple in the form of `[mutate, result]` | ||||||||||||
*/ | ||||||||||||
export function useMutation< | ||||||||||||
TData = any, | ||||||||||||
TVariables = OperationVariables, | ||||||||||||
TContext = DefaultContext, | ||||||||||||
TCache extends ApolloCache<any> = ApolloCache<any>, | ||||||||||||
>( | ||||||||||||
mutation: DocumentNode | TypedDocumentNode<TData, TVariables>, | ||||||||||||
options: { ignoreResults: true } & MutationHookOptions< | ||||||||||||
NoInfer<TData>, | ||||||||||||
NoInfer<TVariables>, | ||||||||||||
TContext, | ||||||||||||
TCache | ||||||||||||
> | ||||||||||||
): [ | ||||||||||||
MutationFunction<TData, TVariables, TContext, TCache>, | ||||||||||||
// result is not reliable when ignoreResults is true | ||||||||||||
Pick<MutationResult<TData>, "reset">, | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like we still return apollo-client/src/react/hooks/useMutation.ts Lines 88 to 92 in eeb5f3c
Can we include those properties here as well? I see no reason not to be able to access them, even if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The biggest problem I've seen is that while the properties are there at runtime, their value is always unset. In my opinion, I feel that at runtime the unreliable fields should not be returned, but that would be breaking change and I wasn't keen on going down that route. I see that Now that I review it a bit more, I now notice that in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We've got a planning meeting tomorrow so let me take this to the team and get their thoughts on this. |
||||||||||||
]; | ||||||||||||
export function useMutation< | ||||||||||||
TData = any, | ||||||||||||
TVariables = OperationVariables, | ||||||||||||
TContext = DefaultContext, | ||||||||||||
TCache extends ApolloCache<any> = ApolloCache<any>, | ||||||||||||
>( | ||||||||||||
mutation: DocumentNode | TypedDocumentNode<TData, TVariables>, | ||||||||||||
options?: MutationHookOptions< | ||||||||||||
NoInfer<TData>, | ||||||||||||
NoInfer<TVariables>, | ||||||||||||
TContext, | ||||||||||||
TCache | ||||||||||||
> | ||||||||||||
): MutationTuple<TData, TVariables, TContext, TCache>; | ||||||||||||
export function useMutation< | ||||||||||||
TData = any, | ||||||||||||
TVariables = OperationVariables, | ||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -394,6 +394,8 @@ export declare type MutationFunction< | |
TCache extends ApolloCache<any> = ApolloCache<any>, | ||
> = ( | ||
options?: MutationFunctionOptions<TData, TVariables, TContext, TCache> | ||
// TODO This FetchResult<TData> seems strange here, as opposed to an | ||
// ApolloQueryResult<TData> | ||
) => Promise<FetchResult<MaybeMasked<TData>>>; | ||
|
||
export interface MutationHookOptions< | ||
|
@@ -418,11 +420,7 @@ export type MutationTuple< | |
TContext = DefaultContext, | ||
TCache extends ApolloCache<any> = ApolloCache<any>, | ||
> = [ | ||
mutate: ( | ||
options?: MutationFunctionOptions<TData, TVariables, TContext, TCache> | ||
// TODO This FetchResult<TData> seems strange here, as opposed to an | ||
// ApolloQueryResult<TData> | ||
) => Promise<FetchResult<MaybeMasked<TData>>>, | ||
mutate: MutationFunction<TData, TVariables, TContext, TCache>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hahah yeah, I went down the route of making a new type... only to realize it already existed 😅 |
||
result: MutationResult<TData>, | ||
]; | ||
|
||
|
There was a problem hiding this comment.
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
andcalled
as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, I rewrote it a bit, the destructuring was not needed and made the test longer than needed