Skip to content

Commit

Permalink
fix: allow errors to be ignored by links
Browse files Browse the repository at this point in the history
  • Loading branch information
mccraveiro committed Dec 11, 2022
1 parent 0daf29f commit 1d78d48
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 21 deletions.
14 changes: 10 additions & 4 deletions src/core/QueryInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,15 @@ export function shouldWriteResult<T>(
const ignoreErrors =
errorPolicy === "ignore" ||
errorPolicy === "all";
let writeWithErrors = !graphQLResultHasError(result);
if (!writeWithErrors && ignoreErrors && result.data) {
writeWithErrors = true;
const hasErrors = graphQLResultHasError(result);

if (!result.data) {
return false;
}

if (hasErrors && !ignoreErrors) {
return false;
}
return writeWithErrors;

return true;
}
55 changes: 54 additions & 1 deletion src/core/__tests__/QueryManager/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ import { itAsync, MockSubscriptionLink } from '../../../testing/core';

// core
import { QueryManager } from '../../QueryManager';
import { NextLink, Operation, Reference } from '../../../core';
import { FetchResult, NextLink, Operation, Reference } from '../../../core';
import { invariant } from '../../../utilities/globals';

jest.spyOn(invariant, "error");

beforeEach(() => {
jest.clearAllMocks();
})

describe('Link interactions', () => {
itAsync('includes the cache on the context for eviction links', (resolve, reject) => {
Expand Down Expand Up @@ -360,4 +367,50 @@ describe('Link interactions', () => {
});
});
});
itAsync('allow errors to be ignored for mutations', (resolve, reject) => {
const mutation = gql`
mutation UpdateLuke {
people_one(id: 1) {
name
friends {
name
}
}
}
`;

const errorLink = (operation: Operation, forward: NextLink) => {
const ignoreErrorResult: FetchResult = {
data: null,
errors: [],
}

return new Observable<FetchResult>(observer => {
forward(operation).subscribe({
next: observer.next.bind(observer),
complete: observer.complete.bind(observer),
error() {
observer.next(ignoreErrorResult);
}
})
});
};

const mockLink = new MockSubscriptionLink();
const link = ApolloLink.from([errorLink, mockLink]);
const queryManager = new QueryManager({
cache: new InMemoryCache({ addTypename: false }),
link,
});

queryManager.mutate({ mutation })
.then(() => {
expect(invariant.error).not.toBeCalled();
resolve();
})
.catch(reject);

// fire off error
mockLink.simulateResult({ error: new Error("UNAUTHENTICATED") });
});
});
3 changes: 1 addition & 2 deletions src/react/hooks/__tests__/useMutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,7 @@ describe('useMutation Hook', () => {
});

expect(fetchResult).toEqual({});
expect(errorMock).toHaveBeenCalledTimes(1);
expect(errorMock.mock.calls[0][0]).toMatch("Missing field");
expect(errorMock).not.toBeCalled();
errorMock.mockRestore();
});
});
Expand Down
16 changes: 2 additions & 14 deletions src/react/hooks/__tests__/useSubscription.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,7 @@ describe('useSubscription Hook', () => {
await expect(waitForNextUpdate({ timeout: 20 }))
.rejects.toThrow('Timed out');

expect(errorSpy).toHaveBeenCalledTimes(1);
expect(errorSpy.mock.calls[0][0]).toBe(
"Missing field 'car' while writing result {}",
);
expect(errorSpy).not.toBeCalled();
errorSpy.mockRestore();
});

Expand Down Expand Up @@ -588,16 +585,7 @@ describe('useSubscription Hook', () => {
await expect(waitForNextUpdate({ timeout: 20 }))
.rejects.toThrow('Timed out');

expect(errorSpy).toHaveBeenCalledTimes(3);
expect(errorSpy.mock.calls[0][0]).toBe(
"Missing field 'car' while writing result {}",
);
expect(errorSpy.mock.calls[1][0]).toBe(
"Missing field 'car' while writing result {}",
);
expect(errorSpy.mock.calls[2][0]).toBe(
"Missing field 'car' while writing result {}",
);
expect(errorSpy).not.toBeCalled();
errorSpy.mockRestore();
});

Expand Down

0 comments on commit 1d78d48

Please sign in to comment.