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

onError for useSubscription #122

Open
ArnaudBarre opened this issue Jul 1, 2019 · 1 comment
Open

onError for useSubscription #122

ArnaudBarre opened this issue Jul 1, 2019 · 1 comment
Labels
🪝 react hooks Feature requests related to React hooks project-apollo-client (legacy) LEGACY TAG DO NOT USE

Comments

@ArnaudBarre
Copy link

ArnaudBarre commented Jul 1, 2019

Problem:

Do a side effect on subscription error using the useSubscription hook.

Current solution:

const { error } = useSubscription<UserInteraction>(
  SUBSCRIBE_USER_INTERACTIONS,
  {
    onSubscriptionData: ({ subscriptionData }) => {
      if (subscriptionData.error) // bad typing, never set
      if (!subscriptionData.data) return; // bad typing, not called if no data
      ... // side effect on data
    },
  }
);

useEffect(() => {
  if (!error) return;
  ... // side effect on error
}, [error]);

Desired API:

useSubscription<UserInteraction>(
  SUBSCRIBE_USER_INTERACTIONS,
  {
    onSubscriptionData: ({ subscriptionData }) => {
      ... // side effect on data (with subscriptionData.data! and no subscriptionData.error)
    },
    onError: (error: ApolloError!) => {
      ... // side effect on error
    },
  }
);

Why:

I think this will make the API more consistent with useQuery and avoid null checks in callbacks, making the code more readable.

@eddie-atkinson
Copy link

Did anyone find any other ways of achieving this? Has there been any progress on this request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🪝 react hooks Feature requests related to React hooks project-apollo-client (legacy) LEGACY TAG DO NOT USE
Projects
None yet
Development

No branches or pull requests

3 participants