diff --git a/src/__tests__/mutationResults.ts b/src/__tests__/mutationResults.ts index aa9a6183937..7f140dc17a0 100644 --- a/src/__tests__/mutationResults.ts +++ b/src/__tests__/mutationResults.ts @@ -2,7 +2,7 @@ import { cloneDeep } from "lodash"; import gql from "graphql-tag"; import { GraphQLError } from "graphql"; -import { ApolloClient } from "../core"; +import { ApolloClient, FetchResult } from "../core"; import { InMemoryCache } from "../cache"; import { ApolloLink } from "../link/core"; import { @@ -1819,5 +1819,40 @@ describe("mutation results", () => { }, reject); } ); + + itAsync( + "data might be undefined in case of failure with errorPolicy = ignore", + async (resolve, reject) => { + const client = new ApolloClient({ + cache: new InMemoryCache(), + link: new ApolloLink( + () => + new Observable>((observer) => { + observer.next({ + errors: [new GraphQLError("Oops")], + }); + observer.complete(); + }) + ).setOnError(reject), + }); + + const ignoreErrorsResult = await client.mutate({ + mutation: gql` + mutation Foo { + foo + } + `, + fetchPolicy: "no-cache", + errorPolicy: "ignore", + }); + + expect(ignoreErrorsResult).toEqual({ + data: undefined, + errors: undefined, + }); + + resolve(); + } + ); }); }); diff --git a/src/core/ApolloClient.ts b/src/core/ApolloClient.ts index 55bae5b11a9..e564a249c10 100644 --- a/src/core/ApolloClient.ts +++ b/src/core/ApolloClient.ts @@ -426,7 +426,8 @@ export class ApolloClient implements DataProxy { /** * This resolves a single mutation according to the options specified and returns a * Promise which is either resolved with the resulting data or rejected with an - * error. + * error. In some cases both `data` and `errors` might be undefined, for example + * when `errorPolicy` is set to `'ignore'`. * * It takes options as an object with the following keys and values: */ diff --git a/src/link/core/types.ts b/src/link/core/types.ts index 0e0e815e192..37fdae3cb57 100644 --- a/src/link/core/types.ts +++ b/src/link/core/types.ts @@ -85,6 +85,7 @@ export interface SingleExecutionResult< TContext = DefaultContext, TExtensions = Record, > extends ExecutionResult { + // data might be undefined if errorPolicy was set to 'ignore' data?: TData | null; context?: TContext; }