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

Expose complete function from the graphql-ws #389

Open
tintin10q opened this issue Jan 11, 2024 · 1 comment
Open

Expose complete function from the graphql-ws #389

tintin10q opened this issue Jan 11, 2024 · 1 comment

Comments

@tintin10q
Copy link

Instead of creating a new connection I would like to just resubscribe instead. graphql-ws allows you to complete queries by sending a complete message. This can easily be done by calling the function that is returned when you call client.subscribe.

It would be great if this functionality is exposed. All that would be needed is to go from this:

client.subscribe(
      { query },
      {
        next({ data }) {
          onMessage && onMessage(data);
        },
        error(error) {
          onError && onError(error);
        },
        complete() {
          onClose && onClose();
        },
      },
    );

    return {
      ws,
      on(listener: typeof onMessage) {
        onMessage = listener;
      },
      error(listener: typeof onError) {
        onError = listener;
      },
      open(listener: (socket: unknown) => void) {
        client.on("opened", listener);
      },
      off(listener: typeof onClose) {
        onClose = listener;
      },
    }

to:

const complete = client.subscribe(
      { query },
      {
        next({ data }) {
          onMessage && onMessage(data);
        },
        error(error) {
          onError && onError(error);
        },
        complete() {
          onClose && onClose();
        },
      },
    );

    return {
      ws,
      complete, 
      on(listener: typeof onMessage) {
        onMessage = listener;
      },
      error(listener: typeof onError) {
        onError = listener;
      },
      open(listener: (socket: unknown) => void) {
        client.on("opened", listener);
      },
      off(listener: typeof onClose) {
        onClose = listener;
      },
    }
@tintin10q
Copy link
Author

You also have to add it here:

export type SubscriptionToGraphQL<Z, T, SCLR extends ScalarDefinition> = {
  complete: () => void;
  ws: WebSocket;
  on: (fn: (args: InputType<T, Z, SCLR>) => void) => void;
  off: (
    fn: (e: {
      data?: InputType<T, Z, SCLR>;
      code?: number;
      reason?: string;
      message?: string;
    }) => void,
  ) => void;
  error: (fn: (e: { data?: InputType<T, Z, SCLR>; errors?: string[] }) => void) => void;
  open: () => void;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant