/*** UTILITY ------------------------------------------ ***/ import { executeSchema, gql, GraphQLHTTP, GraphQLWS, PubSub } from "./entry.ts"; const pubsub = new PubSub(); const schema = executeSchema({ resolvers: { Mutation: { ping: (_: unknown, { msg }: { msg: string }) => { pubsub.publish("PING", { pinged: msg }); return msg; } }, Query: { hello: () => "world" }, Subscription: { pinged: { subscribe: () => pubsub.asyncIterator(["PING"]) } } }, typeDefs: gql` type Query { hello: String } type Mutation { ping(msg: String!): String } type Subscription { pinged: String } ` }); const subscriptions = GraphQLWS({ schema }); const handler = GraphQLHTTP({ graphiql: true, playgroundOptions: { title: "Subscriptions Example" }, schema, subscriptions }); /*** PROGRAM ------------------------------------------ ***/ Deno.serve({ port: 4000 }, handler); setInterval(() => { pubsub.publish("PING", { pinged: `tick @ ${new Date().toISOString()}` }); }, 2000); /*** deno run -A subscription-example.ts ***/