@graphql-mesh/types
Version:
56 lines (55 loc) • 3.01 kB
TypeScript
import type { PubSub as HivePubSub } from '@graphql-hive/pubsub';
import { type MaybePromise } from '@graphql-tools/utils';
import { DisposableSymbols } from '@whatwg-node/disposablestack';
export type { HivePubSub };
export type AllHooks = {
destroy: void;
[key: string]: any;
};
export type HookName = keyof AllHooks & string;
export interface MeshPubSub {
publish<THook extends HookName>(triggerName: THook, payload: AllHooks[THook]): void;
subscribe<THook extends HookName>(triggerName: THook, onMessage: (data: AllHooks[THook]) => void, options?: any): number;
unsubscribe(subId: number): void;
getEventNames(): Iterable<string>;
asyncIterator<THook extends HookName>(triggers: THook): AsyncIterable<AllHooks[THook]>;
}
/**
* Converts the {@link PubSub Hive PubSub interface} to the legacy {@link MeshPubSub}
* Please avoid using this class directly because it will be completely removed in
* the future, instead migrate your project to use the {@link PubSub new interface}.
*
* @deprecated This class is deprecated and will be removed in the future. Implement and use the new {@link HivePubSub Hive PubSub interface} instead.
*/
export declare class MeshFromHivePubSub implements MeshPubSub {
#private;
constructor(pubsub: HivePubSub);
static from(pubsub: undefined): undefined;
static from(pubsub: HivePubSub): MeshFromHivePubSub;
static from(pubsub: undefined | HivePubSub): undefined | MeshFromHivePubSub;
publish<THook extends HookName>(triggerName: THook, payload: AllHooks[THook]): void;
subscribe<THook extends HookName>(triggerName: THook, onMessage: (data: AllHooks[THook]) => void): number;
unsubscribe(subId: number): void;
getEventNames(): Iterable<string>;
asyncIterator<THook extends HookName>(triggerName: THook): AsyncIterable<AllHooks[THook]>;
dispose(): MaybePromise<void>;
[DisposableSymbols.asyncDispose](): MaybePromise<void>;
}
/**
* Checks whether the provided {@link pubsub} is a {@link HivePubSub}. It is only
* accurate when dealing with `@graphql-hive/pubsub` v2 and above.
*/
export declare function isHivePubSub(pubsub: undefined | MeshPubSub | HivePubSub): pubsub is HivePubSub;
/**
* A utility function ensuring the provided {@link pubsub} is always the legacy {@link MeshPubSub}.
* It does so by converting a {@link HivePubSub} to a {@link MeshPubSub} if provided, or leaving it
* as is if it's already a {@link MeshPubSub}.
*
* Internally uses a WeakMap to cache the conversion for performance and to avoid creating too many
* unnecessary instances if called multiple times.
*/
export declare function toMeshPubSub(pubsub: undefined): undefined;
export declare function toMeshPubSub(pubsub: MeshPubSub): MeshPubSub;
export declare function toMeshPubSub(pubsub: HivePubSub): MeshPubSub;
export declare function toMeshPubSub(pubsub: HivePubSub | MeshPubSub): MeshPubSub;
export declare function toMeshPubSub(pubsub: HivePubSub | MeshPubSub | undefined): MeshPubSub | undefined;