graphql-editor / graphql-zeus

GraphQL client and GraphQL code generator with GraphQL autocomplete library generation ⚡⚡⚡ for browser,nodejs and react native ( apollo compatible )
https://graphqleditor.com/docs/tools/zeus/index/
MIT License
1.94k stars 105 forks source link

Expose complete function from the graphql-ws #389

Open tintin10q opened 9 months ago

tintin10q commented 9 months ago

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 commented 9 months ago

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;
};