Registry / development / typed-graphql-subscriptions

typed-graphql-subscriptions

JSON →
library0.0.1jsnpmunverified

typed-graphql-subscriptions (v0.0.1) is a TypeScript library that provides a type-safe wrapper around the graphql-subscriptions PubSub engine. It enforces channel names and payload types via generics, reducing runtime errors in GraphQL subscription publishing. The library is maintained by ContraWork and ships with TypeScript definitions. Key differentiators: strict type safety over plain graphql-subscriptions, support for composite channel names like chatMessageReceived:42, and compatibility with any PubSubEngine implementation.

npm install typed-graphql-subscriptions
INSTALL
IMPORT
SIG · TYPED-GRAPHQL-SUBS
T
typed-graphql-subscriptions
developmentjavascriptv0.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TypedPubSub
import { TypedPubSub } from 'typed-graphql-subscriptions'
import TypedPubSub from 'typed-graphql-subscriptions'
Named export only; no default export. Must use destructuring.
TypedPubSub class instantiation
const typedPubSub = new TypedPubSub<Channels>(pubSub)
const typedPubSub = new TypedPubSub(pubSub)
Generic type parameter is required for type safety; omitting it will use defaults and lose type checking.
Channels type
type Channels = { channelName: [PayloadType] };
type Channels = { channelName: PayloadType };
Channel values must be tuples representing the arguments to publish. For single payload, use [PayloadType].

Install package, define channel types, instantiate TypedPubSub, subscribe and publish with type safety.

import { PubSub } from 'graphql-subscriptions'; import { TypedPubSub } from 'typed-graphql-subscriptions'; type Channels = { announcementCreated: [{ body: string }]; chatMessageReceived: [number, { body: string }]; }; const pubSub = new PubSub(); const typedPubSub = new TypedPubSub<Channels>(pubSub); // Subscribe to a channel const asyncIterator = typedPubSub.asyncIterator('announcementCreated'); // Publish to a channel await typedPubSub.publish('announcementCreated', { body: 'Hello world' });
Debug
Known issues
gotchaGeneric type parameter must match the exact channel shape; mismatched tuples cause compilation errors.
fix
Ensure Channels type maps each channel name to a tuple of arguments precisely.
affects: >=0.0.0
gotchaThe underlying PubSub instance must implement PubSubEngine (from graphql-subscriptions) – custom engines may not be compatible.
fix
Use a PubSub instance that conforms to PubSubEngine interface, e.g., from graphql-subscriptions or graphql-amqp-subscriptions.
affects: >=0.0.0
Errors
Common errors & fixes
Type '...' is not assignable to type '...'
Incorrect payload type provided to publish method.
fix
Ensure the payload matches the tuple type defined in the Channels mapping.
Cannot find name 'TypedPubSub'
TypedPubSub not imported correctly.
fix
Use correct import: import { TypedPubSub } from 'typed-graphql-subscriptions'
Upgrade
Version history
0.0.1latest on npm
Audit
Dependencies
graphql-subscriptionsrequiredRequired as peer dependency; TypedPubSub wraps PubSubEngine from graphql-subscriptions.
Agent activity
60 hits · last 30 days
node
48
OpenAI (training)
1
Resources
typed-graphql-subscriptions — npm install typed-graphql-subscriptions · libregistry