Skip to content

Commit

Permalink
Document undefined data cases for mutations (#6549)
Browse files Browse the repository at this point in the history
Co-authored-by: Jerel Miller <[email protected]>
  • Loading branch information
PowerKiKi and jerelmiller authored Feb 2, 2024
1 parent 51d7988 commit 26fe4a5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
37 changes: 36 additions & 1 deletion src/__tests__/mutationResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<FetchResult<{ foo: string }>>((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();
}
);
});
});
3 changes: 2 additions & 1 deletion src/core/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ export class ApolloClient<TCacheShape> 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:
*/
Expand Down
1 change: 1 addition & 0 deletions src/link/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export interface SingleExecutionResult<
TContext = DefaultContext,
TExtensions = Record<string, any>,
> extends ExecutionResult<TData, TExtensions> {
// data might be undefined if errorPolicy was set to 'ignore'
data?: TData | null;
context?: TContext;
}
Expand Down

0 comments on commit 26fe4a5

Please sign in to comment.