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

Add onError option to useSubscription hook #9495

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
53 changes: 52 additions & 1 deletion src/react/hooks/__tests__/useSubscription.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { renderHook } from '@testing-library/react-hooks';
import gql from 'graphql-tag';

import { ApolloClient, ApolloLink, concat } from '../../../core';
import { ApolloClient, ApolloError, ApolloLink, concat } from '../../../core';
import { InMemoryCache as Cache } from '../../../cache';
import { ApolloProvider } from '../../context';
import { MockSubscriptionLink } from '../../../testing';
Expand Down Expand Up @@ -61,6 +61,57 @@ describe('useSubscription Hook', () => {
expect(result.current.data).toEqual(results[3].result.data);
});

it('should call onError after error results', async () => {
const subscription = gql`
subscription {
car {
make
}
}
`;

const results = ['Audi', 'BMW', 'Mercedes', 'Hyundai'].map(make => ({
result: { data: { car: { make } } }
}));

const errorResult = {
error: new ApolloError({ errorMessage: "test" }),
result: { data: { car: { make: null } } },
};

const link = new MockSubscriptionLink();
const client = new ApolloClient({
link,
cache: new Cache({ addTypename: false })
});


const onError = jest.fn();
const { result, waitForNextUpdate } = renderHook(
() => useSubscription(subscription, { onError }),
{
wrapper: ({ children }) => (
<ApolloProvider client={client}>
{children}
</ApolloProvider>
),
},
);

expect(result.current.loading).toBe(true);
expect(result.current.error).toBe(undefined);
expect(result.current.data).toBe(undefined);
setTimeout(() => link.simulateResult(results[0]));
await waitForNextUpdate();
expect(result.current.loading).toBe(false);
expect(result.current.data).toEqual(results[0].result.data);
setTimeout(() => link.simulateResult(errorResult));
await waitForNextUpdate();
setTimeout(() => {
expect(onError).toHaveBeenCalledTimes(1);
});
});

it('should cleanup after the subscription component has been unmounted', async () => {
const subscription = gql`
subscription {
Expand Down
1 change: 1 addition & 0 deletions src/react/hooks/useSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export function useSubscription<TData = any, TVariables = OperationVariables>(
error,
variables: options?.variables,
});
ref.current.options?.onError?.(error);
},
complete() {
ref.current.options?.onSubscriptionComplete?.();
Expand Down
1 change: 1 addition & 0 deletions src/react/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ export interface BaseSubscriptionOptions<
skip?: boolean;
context?: DefaultContext;
onSubscriptionData?: (options: OnSubscriptionDataOptions<TData>) => any;
onError?: (error: ApolloError) => void;
onSubscriptionComplete?: () => void;
Comment on lines 213 to 215
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you feel about calling this onSubscriptionError, for consistency with the other onSubscription* options? I like onError better too (for brevity), but to my eyes it looks a little out of place here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hwillson @brainkim thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like onError more as well, but we can't change the others in 3.7 so I'm 👍 with making it onSubscriptionError.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benjamn confirmed we can release this change, but we want to consider a future improvement to change on

onSubscriptionData and onSubscriptionComplete to onData and onComplete

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

Expand Down