Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6f4849f

Browse files
committed
initial implementation of subscriptions
1 parent 2525b39 commit 6f4849f

File tree

5 files changed

+81
-3
lines changed

5 files changed

+81
-3
lines changed

api/index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,19 @@ import { GitHubConnector } from './github/connector';
1313
import { Repositories, Users } from './github/models';
1414
import { Entries, Comments } from './sql/models';
1515

16+
import { createServer } from 'http';
17+
import { Server } from 'subscriptions-transport-ws';
18+
import { subscriptionManager } from './subscriptions';
19+
1620
import schema from './schema';
1721

1822
let PORT = 3010;
1923
if (process.env.PORT) {
2024
PORT = parseInt(process.env.PORT, 10) + 100;
2125
}
2226

27+
const WS_PORT = process.env.WS_PORT || 8080;
28+
2329
const app = express();
2430

2531
app.use(bodyParser.urlencoded({ extended: true }));
@@ -91,3 +97,35 @@ app.get('/', (req, res) => {
9197
app.listen(PORT, () => console.log( // eslint-disable-line no-console
9298
`API Server is now running on http://localhost:${PORT}`
9399
));
100+
101+
// WebSocket server for subscriptions
102+
const httpServer = createServer((request, response) => {
103+
response.writeHead(404);
104+
response.end();
105+
});
106+
107+
httpServer.listen(WS_PORT, () => console.log( // eslint-disable-line no-console
108+
`Websocket Server is now running on http://localhost:${WS_PORT}`
109+
));
110+
111+
// eslint-disable-next-line
112+
new Server(
113+
{
114+
subscriptionManager,
115+
onSubscribe: (msg, params) => {
116+
const gitHubConnector = new GitHubConnector({
117+
clientId: GITHUB_CLIENT_ID,
118+
clientSecret: GITHUB_CLIENT_SECRET,
119+
});
120+
return Object.assign({}, params, {
121+
context: {
122+
Repositories: new Repositories({ connector: gitHubConnector }),
123+
Users: new Users({ connector: gitHubConnector }),
124+
Entries: new Entries(),
125+
Comments: new Comments(),
126+
},
127+
});
128+
},
129+
},
130+
httpServer
131+
);

api/schema.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { merge } from 'lodash';
22
import { schema as gitHubSchema, resolvers as gitHubResolvers } from './github/schema';
33
import { schema as sqlSchema, resolvers as sqlResolvers } from './sql/schema';
44
import { makeExecutableSchema } from 'graphql-tools';
5+
import { pubsub } from './subscriptions';
56

67
const rootSchema = [`
78
@@ -73,9 +74,15 @@ type Mutation {
7374
): Comment
7475
}
7576
77+
type Subscription {
78+
# Subscription fires on every comment added
79+
commentAdded(repoFullName: String!): Comment
80+
}
81+
7682
schema {
7783
query: Query
7884
mutation: Mutation
85+
subscription: Subscription
7986
}
8087
8188
`];
@@ -126,9 +133,14 @@ const rootResolvers = {
126133
commentContent
127134
)
128135
))
129-
.then(([id]) => (
136+
.then(([id]) =>
130137
context.Comments.getCommentById(id)
131-
));
138+
)
139+
.then(comment => {
140+
// publish subscription notification
141+
pubsub.publish('commentAdded', comment);
142+
return comment;
143+
});
132144
},
133145

134146
vote(root, { repoFullName, type }, context) {
@@ -151,6 +163,12 @@ const rootResolvers = {
151163
));
152164
},
153165
},
166+
Subscription: {
167+
commentAdded(comment) {
168+
// the subscription payload is the comment.
169+
return comment;
170+
},
171+
},
154172
};
155173

156174
// Put schema together into one array of schema strings

api/sql/schema.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ export const schema = [`
44
55
# A comment about an entry, submitted by a user
66
type Comment {
7+
# The SQL ID of this entry
8+
id: Int!
9+
710
# The GitHub user who posted the comment
811
postedBy: User!
912

api/subscriptions.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { PubSub, SubscriptionManager } from 'graphql-subscriptions';
2+
import schema from './schema';
3+
4+
const pubsub = new PubSub();
5+
const subscriptionManager = new SubscriptionManager({
6+
schema,
7+
pubsub,
8+
setupFunctions: {
9+
commentAdded: (options, args) => ({
10+
commentAdded: comment => {
11+
return comment.repository_name === args.repoFullName;
12+
},
13+
}),
14+
},
15+
});
16+
17+
export { subscriptionManager, pubsub };

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@
4848
"express": "^4.13.4",
4949
"express-session": "^1.13.0",
5050
"graphql": "^0.7.0",
51+
"graphql-subscriptions": "^0.1.0",
5152
"graphql-tools": "^0.6.2",
5253
"knex": "^0.11.3",
5354
"lodash": "^4.12.0",
5455
"passport": "^0.3.2",
5556
"passport-github": "^1.1.0",
5657
"request-promise": "^3.0.0",
57-
"sqlite3": "^3.1.4"
58+
"sqlite3": "^3.1.4",
59+
"subscriptions-transport-ws": "^0.1.2"
5860
}
5961
}

0 commit comments

Comments
 (0)