Skip to content

Commit

Permalink
Fix optimisticResponse function return type (#11944)
Browse files Browse the repository at this point in the history
* fix(types): explicitly return `IgnoreModifier` from `optimisticResponse` function

* Add changeset

* Add test that allows returning `IgnoreModifier` when inferring from a generic TypedDocumentNode mutation

---------

Co-authored-by: Lenz Weber-Tronic <[email protected]>
  • Loading branch information
sneyderdev and phryneas committed Jul 15, 2024
1 parent c4cf577 commit 8f3d7eb
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .api-reports/api-report-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1391,9 +1391,10 @@ interface MutationBaseOptions<TData = any, TVariables = OperationVariables, TCon
context?: TContext;
errorPolicy?: ErrorPolicy;
onQueryUpdated?: OnQueryUpdated<any>;
// Warning: (ae-forgotten-export) The symbol "IgnoreModifier" needs to be exported by the entry point index.d.ts
optimisticResponse?: TData | ((vars: TVariables, { IGNORE }: {
IGNORE: IgnoreModifier;
}) => TData);
}) => TData | IgnoreModifier);
refetchQueries?: ((result: FetchResult<TData>) => InternalRefetchQueriesInclude) | InternalRefetchQueriesInclude;
update?: MutationUpdaterFunction<TData, TVariables, TContext, TCache>;
updateQueries?: MutationQueryReducersMap<TData>;
Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report-react.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1119,9 +1119,10 @@ interface MutationBaseOptions<TData = any, TVariables = OperationVariables, TCon
errorPolicy?: ErrorPolicy;
// Warning: (ae-forgotten-export) The symbol "OnQueryUpdated" needs to be exported by the entry point index.d.ts
onQueryUpdated?: OnQueryUpdated<any>;
// Warning: (ae-forgotten-export) The symbol "IgnoreModifier" needs to be exported by the entry point index.d.ts
optimisticResponse?: TData | ((vars: TVariables, { IGNORE }: {
IGNORE: IgnoreModifier;
}) => TData);
}) => TData | IgnoreModifier);
refetchQueries?: ((result: FetchResult<TData>) => InternalRefetchQueriesInclude) | InternalRefetchQueriesInclude;
// Warning: (ae-forgotten-export) The symbol "MutationUpdaterFunction" needs to be exported by the entry point index.d.ts
update?: MutationUpdaterFunction<TData, TVariables, TContext, TCache>;
Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report-react_components.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -995,9 +995,10 @@ interface MutationBaseOptions<TData = any, TVariables = OperationVariables, TCon
errorPolicy?: ErrorPolicy;
// Warning: (ae-forgotten-export) The symbol "OnQueryUpdated" needs to be exported by the entry point index.d.ts
onQueryUpdated?: OnQueryUpdated<any>;
// Warning: (ae-forgotten-export) The symbol "IgnoreModifier" needs to be exported by the entry point index.d.ts
optimisticResponse?: TData | ((vars: TVariables, { IGNORE }: {
IGNORE: IgnoreModifier;
}) => TData);
}) => TData | IgnoreModifier);
refetchQueries?: ((result: FetchResult<TData>) => InternalRefetchQueriesInclude) | InternalRefetchQueriesInclude;
// Warning: (ae-forgotten-export) The symbol "MutationUpdaterFunction" needs to be exported by the entry point index.d.ts
update?: MutationUpdaterFunction<TData, TVariables, TContext, TCache>;
Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report-react_context.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -978,9 +978,10 @@ interface MutationBaseOptions<TData = any, TVariables = OperationVariables, TCon
errorPolicy?: ErrorPolicy;
// Warning: (ae-forgotten-export) The symbol "OnQueryUpdated" needs to be exported by the entry point index.d.ts
onQueryUpdated?: OnQueryUpdated<any>;
// Warning: (ae-forgotten-export) The symbol "IgnoreModifier" needs to be exported by the entry point index.d.ts
optimisticResponse?: TData | ((vars: TVariables, { IGNORE }: {
IGNORE: IgnoreModifier;
}) => TData);
}) => TData | IgnoreModifier);
refetchQueries?: ((result: FetchResult<TData>) => InternalRefetchQueriesInclude) | InternalRefetchQueriesInclude;
// Warning: (ae-forgotten-export) The symbol "MutationUpdaterFunction" needs to be exported by the entry point index.d.ts
update?: MutationUpdaterFunction<TData, TVariables, TContext, TCache>;
Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report-react_hoc.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -997,9 +997,10 @@ interface MutationBaseOptions<TData = any, TVariables = OperationVariables, TCon
errorPolicy?: ErrorPolicy;
// Warning: (ae-forgotten-export) The symbol "OnQueryUpdated" needs to be exported by the entry point index.d.ts
onQueryUpdated?: OnQueryUpdated<any>;
// Warning: (ae-forgotten-export) The symbol "IgnoreModifier" needs to be exported by the entry point index.d.ts
optimisticResponse?: TData | ((vars: TVariables, { IGNORE }: {
IGNORE: IgnoreModifier;
}) => TData);
}) => TData | IgnoreModifier);
refetchQueries?: ((result: FetchResult<TData>) => InternalRefetchQueriesInclude) | InternalRefetchQueriesInclude;
// Warning: (ae-forgotten-export) The symbol "MutationUpdaterFunction" needs to be exported by the entry point index.d.ts
update?: MutationUpdaterFunction<TData, TVariables, TContext, TCache>;
Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report-react_hooks.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1068,9 +1068,10 @@ interface MutationBaseOptions<TData = any, TVariables = OperationVariables, TCon
errorPolicy?: ErrorPolicy;
// Warning: (ae-forgotten-export) The symbol "OnQueryUpdated" needs to be exported by the entry point index.d.ts
onQueryUpdated?: OnQueryUpdated<any>;
// Warning: (ae-forgotten-export) The symbol "IgnoreModifier" needs to be exported by the entry point index.d.ts
optimisticResponse?: TData | ((vars: TVariables, { IGNORE }: {
IGNORE: IgnoreModifier;
}) => TData);
}) => TData | IgnoreModifier);
refetchQueries?: ((result: FetchResult<TData>) => InternalRefetchQueriesInclude) | InternalRefetchQueriesInclude;
// Warning: (ae-forgotten-export) The symbol "MutationUpdaterFunction" needs to be exported by the entry point index.d.ts
update?: MutationUpdaterFunction<TData, TVariables, TContext, TCache>;
Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report-react_internal.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1079,9 +1079,10 @@ interface MutationBaseOptions<TData = any, TVariables = OperationVariables, TCon
errorPolicy?: ErrorPolicy;
// Warning: (ae-forgotten-export) The symbol "OnQueryUpdated" needs to be exported by the entry point index.d.ts
onQueryUpdated?: OnQueryUpdated<any>;
// Warning: (ae-forgotten-export) The symbol "IgnoreModifier" needs to be exported by the entry point index.d.ts
optimisticResponse?: TData | ((vars: TVariables, { IGNORE }: {
IGNORE: IgnoreModifier;
}) => TData);
}) => TData | IgnoreModifier);
refetchQueries?: ((result: FetchResult<TData>) => InternalRefetchQueriesInclude) | InternalRefetchQueriesInclude;
// Warning: (ae-forgotten-export) The symbol "MutationUpdaterFunction" needs to be exported by the entry point index.d.ts
update?: MutationUpdaterFunction<TData, TVariables, TContext, TCache>;
Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report-react_ssr.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -963,9 +963,10 @@ interface MutationBaseOptions<TData = any, TVariables = OperationVariables, TCon
errorPolicy?: ErrorPolicy;
// Warning: (ae-forgotten-export) The symbol "OnQueryUpdated" needs to be exported by the entry point index.d.ts
onQueryUpdated?: OnQueryUpdated<any>;
// Warning: (ae-forgotten-export) The symbol "IgnoreModifier" needs to be exported by the entry point index.d.ts
optimisticResponse?: TData | ((vars: TVariables, { IGNORE }: {
IGNORE: IgnoreModifier;
}) => TData);
}) => TData | IgnoreModifier);
refetchQueries?: ((result: FetchResult<TData>) => InternalRefetchQueriesInclude) | InternalRefetchQueriesInclude;
// Warning: (ae-forgotten-export) The symbol "MutationUpdaterFunction" needs to be exported by the entry point index.d.ts
update?: MutationUpdaterFunction<TData, TVariables, TContext, TCache>;
Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report-testing.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1084,9 +1084,10 @@ interface MutationBaseOptions<TData = any, TVariables = OperationVariables, TCon
errorPolicy?: ErrorPolicy;
// Warning: (ae-forgotten-export) The symbol "OnQueryUpdated" needs to be exported by the entry point index.d.ts
onQueryUpdated?: OnQueryUpdated<any>;
// Warning: (ae-forgotten-export) The symbol "IgnoreModifier" needs to be exported by the entry point index.d.ts
optimisticResponse?: TData | ((vars: TVariables, { IGNORE }: {
IGNORE: IgnoreModifier;
}) => TData);
}) => TData | IgnoreModifier);
refetchQueries?: ((result: FetchResult<TData>) => InternalRefetchQueriesInclude) | InternalRefetchQueriesInclude;
// Warning: (ae-forgotten-export) The symbol "MutationUpdaterFunction" needs to be exported by the entry point index.d.ts
update?: MutationUpdaterFunction<TData, TVariables, TContext, TCache>;
Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report-testing_core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1039,9 +1039,10 @@ interface MutationBaseOptions<TData = any, TVariables = OperationVariables, TCon
errorPolicy?: ErrorPolicy;
// Warning: (ae-forgotten-export) The symbol "OnQueryUpdated" needs to be exported by the entry point index.d.ts
onQueryUpdated?: OnQueryUpdated<any>;
// Warning: (ae-forgotten-export) The symbol "IgnoreModifier" needs to be exported by the entry point index.d.ts
optimisticResponse?: TData | ((vars: TVariables, { IGNORE }: {
IGNORE: IgnoreModifier;
}) => TData);
}) => TData | IgnoreModifier);
refetchQueries?: ((result: FetchResult<TData>) => InternalRefetchQueriesInclude) | InternalRefetchQueriesInclude;
// Warning: (ae-forgotten-export) The symbol "MutationUpdaterFunction" needs to be exported by the entry point index.d.ts
update?: MutationUpdaterFunction<TData, TVariables, TContext, TCache>;
Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report-utilities.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1687,9 +1687,10 @@ interface MutationBaseOptions<TData = any, TVariables = OperationVariables, TCon
errorPolicy?: ErrorPolicy;
// Warning: (ae-forgotten-export) The symbol "OnQueryUpdated" needs to be exported by the entry point index.d.ts
onQueryUpdated?: OnQueryUpdated<any>;
// Warning: (ae-forgotten-export) The symbol "IgnoreModifier" needs to be exported by the entry point index.d.ts
optimisticResponse?: TData | ((vars: TVariables, { IGNORE }: {
IGNORE: IgnoreModifier;
}) => TData);
}) => TData | IgnoreModifier);
refetchQueries?: ((result: FetchResult<TData>) => InternalRefetchQueriesInclude) | InternalRefetchQueriesInclude;
// Warning: (ae-forgotten-export) The symbol "MutationUpdaterFunction" needs to be exported by the entry point index.d.ts
update?: MutationUpdaterFunction<TData, TVariables, TContext, TCache>;
Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1572,9 +1572,10 @@ interface MutationBaseOptions<TData = any, TVariables = OperationVariables, TCon
context?: TContext;
errorPolicy?: ErrorPolicy;
onQueryUpdated?: OnQueryUpdated<any>;
// Warning: (ae-forgotten-export) The symbol "IgnoreModifier" needs to be exported by the entry point index.d.ts
optimisticResponse?: TData | ((vars: TVariables, { IGNORE }: {
IGNORE: IgnoreModifier;
}) => TData);
}) => TData | IgnoreModifier);
refetchQueries?: ((result: FetchResult<TData>) => InternalRefetchQueriesInclude) | InternalRefetchQueriesInclude;
update?: MutationUpdaterFunction<TData, TVariables, TContext, TCache>;
updateQueries?: MutationQueryReducersMap<TData>;
Expand Down
5 changes: 5 additions & 0 deletions .changeset/pink-flowers-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Allow `IgnoreModifier` to be returned from a `optimisticResponse` function when inferring from a `TypedDocumentNode` when used with a generic argument.
20 changes: 20 additions & 0 deletions src/__tests__/optimistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ApolloLink,
ApolloCache,
MutationQueryReducersMap,
TypedDocumentNode,
} from "../core";

import { QueryManager } from "../core/QueryManager";
Expand Down Expand Up @@ -1089,6 +1090,25 @@ describe("optimistic mutation results", () => {
resolve();
}
);

it("allows IgnoreModifier as return value when inferring from a TypedDocumentNode mutation", () => {
const mutation: TypedDocumentNode<{ bar: string }> = gql`
mutation foo {
foo {
bar
}
}
`;

const client = new ApolloClient({
cache: new InMemoryCache(),
});

client.mutate({
mutation,
optimisticResponse: (vars, { IGNORE }) => IGNORE,
});
});
});

describe("optimistic updates using `updateQueries`", () => {
Expand Down
5 changes: 4 additions & 1 deletion src/core/watchQueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ export interface MutationBaseOptions<
/** {@inheritDoc @apollo/client!MutationOptionsDocumentation#optimisticResponse:member} */
optimisticResponse?:
| TData
| ((vars: TVariables, { IGNORE }: { IGNORE: IgnoreModifier }) => TData);
| ((
vars: TVariables,
{ IGNORE }: { IGNORE: IgnoreModifier }
) => TData | IgnoreModifier);

/** {@inheritDoc @apollo/client!MutationOptionsDocumentation#updateQueries:member} */
updateQueries?: MutationQueryReducersMap<TData>;
Expand Down

0 comments on commit 8f3d7eb

Please sign in to comment.