Registry / messaging / graphql-workers-subscriptions

graphql-workers-subscriptions

JSON →
library0.1.6jsnpmunverified

A library for adding topic-based GraphQL subscriptions to Cloudflare Workers using Durable Objects and D1. Version 0.1.6 is the current stable release, with no mention of regular release cadence. Key differentiators include easy integration with existing GraphQL stacks (e.g., graphql-yoga, Apollo), in-database JSON filtering, and built-in authentication support for WebSocket connections. It leverages Cloudflare's Durable Objects for managing WebSocket connections and D1 for persisting subscription data, making it suitable for serverless environments with limited state.

npm install graphql-workers-subscriptions
INSTALL
IMPORT
SIG · GRAPHQL-WORKERS-SU
G
graphql-workers-subscriptions
messagingjavascriptv0.1.6
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.

handleSubscriptions
import { handleSubscriptions } from 'graphql-workers-subscriptions'
const { handleSubscriptions } = require('graphql-workers-subscriptions')
ESM-only library; CommonJS require will fail because the library is distributed as ESM modules.
createWsConnectionPoolClass
import { createWsConnectionPoolClass } from 'graphql-workers-subscriptions'
import { createWsConnectionPoolClass } from 'graphql-workers-subscriptions/methods'
The correct import path is the main package, not a subpath. Typings are included.
subscribe
import { subscribe } from 'graphql-workers-subscriptions'
Used as a resolver factory for subscription fields. The subscribe function is a named export.
DefaultPublishableContext
import { DefaultPublishableContext } from 'graphql-workers-subscriptions'
import type { DefaultPublishableContext } from 'graphql-workers-subscriptions'
This is a type export; can be used with 'import type' for TypeScript.

Sets up a Cloudflare Workers GraphQL endpoint with subscriptions using Durable Objects and D1.

import { makeExecutableSchema } from "@graphql-tools/schema"; import { createYoga } from "graphql-yoga"; import { handleSubscriptions, createWsConnectionPoolClass, subscribe, DefaultPublishableContext, createDefaultPublishableContext, } from "graphql-workers-subscriptions"; const schema = makeExecutableSchema<DefaultPublishableContext<ENV>>({ typeDefs: ` type Query { ping: String } type Subscription { greetings(greeting: String): Greeting } type Mutation { greet(greeting: String!): String } type Greeting { greeting: String } `, resolvers: { Query: { ping: () => "pong" }, Mutation: { greet: async (root, args, context) => { context.publish("GREETINGS", { greetings: { greeting: args.greeting } }); return "ok"; }, }, Subscription: { greetings: { subscribe: subscribe("GREETINGS", { filter: (root, args) => (args.greeting ? { greetings: { greeting: args.greeting } } : {}), }), }, }, }, }); const settings = { schema, wsConnectionPool: (env: ENV) => env.WS_CONNECTION_POOL, subscriptionsDb: (env: ENV) => env.SUBSCRIPTIONS, }; const yoga = createYoga<DefaultPublishableContext<ENV>>({ schema, graphiql: { subscriptionsProtocol: "WS" } }); const baseFetch: ExportedHandlerFetchHandler<ENV> = (request, env, executionCtx) => yoga.handleRequest(request, createDefaultPublishableContext({ env, executionCtx, ...settings })); const fetch = handleSubscriptions({ fetch: baseFetch, ...settings }); export default { fetch }; export const WsConnectionPool = createWsConnectionPoolClass(settings);
Debug
Known issues
gotchaWebSocket connections in development must use wrangler dev --local with D1 and Durable Objects properly configured.
fix
Run 'wrangler d1 create SUBSCRIPTIONS --local' and 'wrangler d1 migrations apply SUBSCRIPTIONS --local' before 'wrangler dev'.
affects: >=0.1.0
gotchaThe subscribe function's filter must return an object matching the subscription field's shape, or subscription may not work as expected.
fix
Ensure the filter returns an object with a key matching the subscription field name (e.g., { greetings: { greeting: args.greeting } }).
affects: >=0.1.0
deprecatedThis library is in early stages; APIs may change without major version bumps.
fix
Pin to a specific version and watch the repository for updates.
affects: 0.1.x
gotchaD1 database bindings must be defined in wrangler.toml with the correct binding name 'SUBSCRIPTIONS'.
fix
Add [[d1_databases]] binding with name = 'SUBSCRIPTIONS' as shown in the README.
affects: >=0.1.0
Errors
Common errors & fixes
Error: Cannot find module 'graphql-workers-subscriptions'
The package is not installed or the import path is wrong.
fix
Run 'npm install graphql-workers-subscriptions' and ensure import is from 'graphql-workers-subscriptions'.
TypeError: yoga.handleRequest is not a function
graphql-yoga version mismatch or incorrect initialization.
fix
Ensure graphql-yoga is version 3.x and that you are using 'createYoga' correctly.
D1 database 'SUBSCRIPTIONS' not found
The D1 database has not been created or the binding name is incorrect.
fix
Run 'wrangler d1 create SUBSCRIPTIONS' and verify the binding name in wrangler.toml matches 'SUBSCRIPTIONS'.
Upgrade
Version history
0.1.6latest on npm
Audit
Dependencies
graphql-yogaoptionalRecommended for creating a GraphQL server with WebSocket support
@graphql-tools/schemaoptionalUsed to create executable schemas
graphql-wsoptionalUsed by graphql-yoga for WebSocket subscriptions
Agent activity
12 hits · last 30 days
node
10
Amazon
1
OpenAI (training)
1
Resources
graphql-workers-subscriptions — npm install graphql-workers-subscriptions · libregistry