This repository was archived by the owner on Apr 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
61 lines (56 loc) · 1.78 KB
/
Copy pathindex.js
File metadata and controls
61 lines (56 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const { createServer } = require('http');
const express = require('express');
const { execute, subscribe } = require('graphql');
const { ApolloServer } = require('apollo-server-express');
const { PubSub } = require('graphql-subscriptions');
const { SubscriptionServer } = require('subscriptions-transport-ws');
const { makeExecutableSchema } = require('@graphql-tools/schema');
const { typeDefs } = require('./schema');
(async () => {
const PORT = 4004;
const pubsub = new PubSub();
const app = express();
const httpServer = createServer(app);
let currentNumber = 0;
const resolvers = {
Query: {
currentNumber() {
return currentNumber;
},
},
Subscription: {
numberIncremented: {
subscribe: () => pubsub.asyncIterator(['NUMBER_INCREMENTED']),
},
},
};
const schema = makeExecutableSchema({ typeDefs, resolvers });
const server = new ApolloServer({ schema, stopOnTerminationSignals: true });
await server.start();
server.applyMiddleware({ app });
const subscriptionServer = SubscriptionServer.create(
{ schema, execute, subscribe },
{ server: httpServer, path: server.graphqlPath },
);
['SIGINT', 'SIGTERM'].forEach(async (signal) => {
process.on(signal, () => {
subscriptionServer.close();
process.exit(0);
});
});
httpServer.listen(PORT, () => {
console.log(
`🚀 Query endpoint ready at http://localhost:${PORT}${server.graphqlPath}`,
);
console.log(
`🚀 Subscription endpoint ready at ws://localhost:${PORT}${server.graphqlPath}`,
);
});
function incrementNumber() {
currentNumber += 1;
pubsub.publish('NUMBER_INCREMENTED', { numberIncremented: currentNumber });
setTimeout(incrementNumber, 10000);
}
// Start incrementing
incrementNumber();
})();