Skip to content
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

MockLink tweaks to avoid uncaught exceptions #7110

Merged
merged 6 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
- `HttpLink` will now automatically strip any unused `variables` before sending queries to the GraphQL server, since those queries are very likely to fail validation, according to the [All Variables Used](https://spec.graphql.org/draft/#sec-All-Variables-Used) rule in the GraphQL specification. If you depend on the preservation of unused variables, you can restore the previous behavior by passing `includeUnusedVariables: true` to the `HttpLink` constructor (which is typically passed as `options.link` to the `ApolloClient` constructor). <br/>
[@benjamn](https://github.com/benjamn) in [#7127](https://github.com/apollographql/apollo-client/pull/7127)

- Ensure `MockLink` (used by `MockedProvider`) returns mock configuration errors (e.g. `No more mocked responses for the query ...`) through the Link's `Observable`, instead of throwing them. These errors are now available through the `error` property of a result. <br/>
[@hwillson](https://github.com/hwillson) in [#7110](https://github.com/apollographql/apollo-client/pull/7110)
> Returning mock configuration errors through the Link's `Observable` was the default behavior in Apollo Client 2.x. We changed it for 3, but the change has been problematic for those looking to migrate from 2.x to 3. We've decided to change this back with the understanding that not many people want or are relying on `MockLink`'s throwing exception approach. If you want to change this functionality, you can define custom error handling through `MockLink.setOnError`.

## Improvements

- Support inheritance of type and field policies, according to `possibleTypes`. <br/>
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1768,7 +1768,7 @@ describe('client', () => {
});

itAsync('fails if network request fails', (resolve, reject) => {
const link = mockSingleLink().setOnError(error => { throw error }); // no queries = no replies.
const link = mockSingleLink(); // no queries = no replies.
const client = new ApolloClient({
link,
cache: new InMemoryCache({ addTypename: false }),
Expand Down Expand Up @@ -2028,7 +2028,7 @@ describe('client', () => {
request: { query: mutation },
result: { data },
error: networkError,
}).setOnError(reject),
}),
cache: new InMemoryCache({ addTypename: false }),
});

Expand Down Expand Up @@ -2475,7 +2475,7 @@ describe('client', () => {
{ request: { query }, result: { data } },
{ request: { query }, error: new Error('This is an error!') },
{ request: { query }, result: { data: dataTwo } }
).setOnError(reject);
);
const client = new ApolloClient({
link,
cache: new InMemoryCache({ addTypename: false }),
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/fetchMore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ describe('fetchMore on an observable query', () => {
request: { query, variables: variablesMore },
error: fetchMoreError,
delay: 5,
}).setOnError(reject);
});

const client = new ApolloClient({
link,
Expand Down Expand Up @@ -674,7 +674,7 @@ describe('fetchMore on an observable query', () => {
request: { query, variables: variablesMore },
error: fetchMoreError,
delay: 5,
}).setOnError(reject);
});

let calledFetchMore = false;

Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/mutationResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('mutation results', () => {
link: mockSingleLink({
request: { query: queryWithTypename } as any,
result,
}, ...mockedResponses).setOnError(reject),
}, ...mockedResponses),
cache: new InMemoryCache({
dataIdFromObject: (obj: any) => {
if (obj.id && obj.__typename) {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/optimistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('optimistic mutation results', () => {
const link = mockSingleLink({
request: { query },
result,
}, ...mockedResponses).setOnError(reject);
}, ...mockedResponses);

const client = new ApolloClient({
link,
Expand Down
8 changes: 4 additions & 4 deletions src/core/__tests__/fetchPolicies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ const createLink = (reject: (reason: any) => any) =>
result: { data: result },
}).setOnError(reject);

const createFailureLink = (reject: (reason: any) => any) =>
const createFailureLink = () =>
mockSingleLink({
request: { query },
error: new Error('query failed'),
}, {
request: { query },
result: { data: result },
}).setOnError(reject);
});

const createMutationLink = (reject: (reason: any) => any) =>
// fetch the data
Expand Down Expand Up @@ -149,7 +149,7 @@ describe('network-only', () => {
});

const client = new ApolloClient({
link: inspector.concat(createFailureLink(reject)),
link: inspector.concat(createFailureLink()),
cache: new InMemoryCache({ addTypename: false }),
});

Expand Down Expand Up @@ -280,7 +280,7 @@ describe('no-cache', () => {
});

const client = new ApolloClient({
link: inspector.concat(createFailureLink(reject)),
link: inspector.concat(createFailureLink()),
cache: new InMemoryCache({ addTypename: false }),
});

Expand Down
20 changes: 17 additions & 3 deletions src/link/core/ApolloLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,25 @@ export class ApolloLink {
throw new InvariantError('request is not implemented');
}

protected onError(reason: any) {
throw reason;
protected onError(
error: any,
observer: ZenObservable.Observer<FetchResult>,
): false | void {
if (observer && observer.error) {
observer.error(error);
// Returning false indicates that observer.error does not need to be
// called again, since it was already called (on the previous line).
// Calling observer.error again would not cause any real problems,
// since only the first call matters, but custom onError functions
// might have other reasons for wanting to prevent the default
// behavior by returning false.
return false;
}
// Throw errors will be passed to observer.error.
throw error;
}

public setOnError(fn: (reason: any) => any): this {
public setOnError(fn: ApolloLink["onError"]): this {
this.onError = fn;
return this;
}
Expand Down
Loading