-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (56 loc) · 1.46 KB
/
index.js
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
62
63
const {
GraphQLSchema,
GraphQLObjectType,
GraphQLInt
} = require('graphql')
const express = require('express');
const {Server} = require('http');
const {SubscriptionServer} = require('subscriptions-transport-ws');
const {execute, subscribe} = require('graphql');
const GraphHTTP = require('express-graphql');
let counter = 0;
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'query',
fields: {
data: {
args: {
srand: {
type: GraphQLInt
}
},
name: 'data',
type: new GraphQLObjectType({
name: 'type',
fields: {
test: {type: GraphQLInt}
}
}),
resolve(_, {}) {
let retval=counter++;
// 1s latency
return new Promise(res => setTimeout(() => res({test: retval}), 1000))
},
},
}
}),
});
const app = express();
app.use('/graphql', GraphHTTP({
schema: schema,
graphiql: true
}));
const server = new Server(app);
/** GraphQL Websocket definition **/
SubscriptionServer.create({
schema,
execute,
subscribe,
}, {
server: server,
path: '/ws',
},);
const port = 7766
server.listen(port, () => {
console.log('Server started here -> http://0.0.0.0:' + port);
});