Registry /
messaging / graphql-workers-subscriptions
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);
Errors
Common errors & fixes
Error: Cannot find module 'graphql-workers-subscriptions'
The package is not installed or the import path is wrong.
fixRun '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.
fixEnsure 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.
fixRun 'wrangler d1 create SUBSCRIPTIONS' and verify the binding name in wrangler.toml matches 'SUBSCRIPTIONS'.
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