From 6d45df3f5c4a0f946a23cac25b37feb0b04171a6 Mon Sep 17 00:00:00 2001 From: Dan Schultz Date: Thu, 14 Mar 2019 13:51:32 -0400 Subject: [PATCH] Add subscription skeleton This follows the first few steps in the [Apollo Documentation](https://www.apollographql.com/docs/graphql-subscriptions/subscriptions-to-schema.html) for adding subscriptions to graphql. It sets up the schema to handle subscriptions and also creates the very initial integration with the PubSub implementation baked into the graphql-subscriptions library. As noted in that library, it is only appropriate for demos, not production. We will eventually want to replace it with a more robust PubSub implementation. Issue #44 Issue #43 --- package.json | 1 + src/server/schema/index.js | 2 ++ .../schema/sentences/fields/subscriptions.js | 13 +++++++++++++ src/server/schema/subscription.js | 16 ++++++++++++++++ yarn.lock | 9 ++++++++- 5 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/server/schema/sentences/fields/subscriptions.js create mode 100644 src/server/schema/subscription.js diff --git a/package.json b/package.json index 0cd7663..bc9b9b4 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "express": "^4.16.3", "express-graphql": "^0.6.12", "graphql": "^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0", + "graphql-subscriptions": "^1.0.0", "graphql-tag": "^2.10.0", "moment": "^2.24.0", "node-schedule": "^1.3.0", diff --git a/src/server/schema/index.js b/src/server/schema/index.js index b226a5c..f48d2aa 100644 --- a/src/server/schema/index.js +++ b/src/server/schema/index.js @@ -4,11 +4,13 @@ import { GraphQLSchema } from 'graphql' // App Imports import query from './query' import mutation from './mutation' +import subscription from './subscription' // Schema const schema = new GraphQLSchema({ query, mutation, + subscription, }) export default schema diff --git a/src/server/schema/sentences/fields/subscriptions.js b/src/server/schema/sentences/fields/subscriptions.js new file mode 100644 index 0000000..b8f6940 --- /dev/null +++ b/src/server/schema/sentences/fields/subscriptions.js @@ -0,0 +1,13 @@ +import { PubSub } from 'graphql-subscriptions' + +// App Imports +import SentenceType from '../type' + +const pubsub = new PubSub() + +const sentenceAdded = { + type: SentenceType, + resolve: () => pubsub.asyncIterator('sentenceAdded'), +} + +export default sentenceAdded diff --git a/src/server/schema/subscription.js b/src/server/schema/subscription.js new file mode 100644 index 0000000..4e9ebba --- /dev/null +++ b/src/server/schema/subscription.js @@ -0,0 +1,16 @@ +import { GraphQLObjectType } from 'graphql' + +// App Imports +import * as sentence from './sentences/fields/subscriptions' + +// Compile into a single export +const subscription = new GraphQLObjectType({ + name: 'subscription', + description: '...', + + fields: { + ...sentence, + }, +}) + +export default subscription diff --git a/yarn.lock b/yarn.lock index 2d782bd..05de66b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3624,6 +3624,13 @@ graphql-anywhere@^4.1.0-alpha.0: apollo-utilities "^1.1.3" tslib "^1.9.3" +graphql-subscriptions@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/graphql-subscriptions/-/graphql-subscriptions-1.0.0.tgz#475267694b3bd465af6477dbab4263a3f62702b8" + integrity sha512-+ytmryoHF1LVf58NKEaNPRUzYyXplm120ntxfPcgOBC7TnK7Tv/4VRHeh4FAR9iL+O1bqhZs4nkibxQ+OA5cDQ== + dependencies: + iterall "^1.2.1" + graphql-tag@^2.10.0, graphql-tag@^2.4.2: version "2.10.1" resolved "https://registry.yarnpkg.com/graphql-tag/-/graphql-tag-2.10.1.tgz#10aa41f1cd8fae5373eaf11f1f67260a3cad5e02" @@ -4367,7 +4374,7 @@ isstream@~0.1.2: resolved "https://registry.yarnpkg.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a" integrity sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo= -iterall@^1.2.2: +iterall@^1.2.1, iterall@^1.2.2: version "1.2.2" resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.2.2.tgz#92d70deb8028e0c39ff3164fdbf4d8b088130cd7" integrity sha512-yynBb1g+RFUPY64fTrFv7nsjRrENBQJaX2UL+2Szc9REFrSNm1rpSXHGzhmAy7a9uv3vlvgBlXnf9RqmPH1/DA==