Skip to content

refetch a specific query on error e.g. on network error and reconnect #436

Answered by enisdenjo
efstathiosntonas asked this question in Q&A
Discussion options

You must be logged in to vote

Of course! By leveraging the client events, you can detect a reconnect and act accordingly. Here's an adapted client:

import { createClient, Client, ClientOptions } from 'graphql-ws';
import { refetchSomeQueries } from './on-reconnected';

interface ClientWithOnReconnected extends Client {
  onReconnected(cb: () => void): () => void;
}

function createClientWithOnReconnected(
  options: ClientOptions,
): ClientWithOnReconnected {
  let abruptlyClosed = false;
  const reconnectedCbs: (() => void)[] = [];

  const client = createClient({
    ...options,
    on: {
      ...options.on,
      closed: (event) => {
        options.on?.closed?.(event);
        // non-1000 close codes are abrupt c…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by efstathiosntonas
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants