diff options
| author | netop://ウィビ <paul@webb.page> | 2026-05-03 12:40:04 -0700 |
|---|---|---|
| committer | netop://ウィビ <paul@webb.page> | 2026-05-03 12:40:04 -0700 |
| commit | 52ee736bd2ac407952863c8e8397770bf1495a45 (patch) | |
| tree | f982218600682f7714a5a5157e940acc0e0e8a13 /subscription-example.ts | |
| parent | 83f2352fef7dc93396e3cf8f224acba271f1ef9d (diff) | |
| download | gq-52ee736bd2ac407952863c8e8397770bf1495a45.tar.gz gq-52ee736bd2ac407952863c8e8397770bf1495a45.zip | |
adds support for subscriptions
Diffstat (limited to 'subscription-example.ts')
| -rw-r--r-- | subscription-example.ts | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/subscription-example.ts b/subscription-example.ts new file mode 100644 index 0000000..6dc1213 --- /dev/null +++ b/subscription-example.ts @@ -0,0 +1,59 @@ + + + +/*** 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 ***/ |