aboutsummaryrefslogtreecommitdiff
path: root/subscription-example.ts
diff options
context:
space:
mode:
Diffstat (limited to 'subscription-example.ts')
-rw-r--r--subscription-example.ts59
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 ***/