Skip to content

Commit

Permalink
Add subscription skeleton
Browse files Browse the repository at this point in the history
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
  • Loading branch information
slifty committed Mar 14, 2019
1 parent b1dce9b commit 6d45df3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/server/schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions src/server/schema/sentences/fields/subscriptions.js
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions src/server/schema/subscription.js
Original file line number Diff line number Diff line change
@@ -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
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down

0 comments on commit 6d45df3

Please sign in to comment.