-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
80 lines (75 loc) · 1.8 KB
/
app.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const express = require('express')
const { graphiqlExpress, graphqlExpress } = require('graphql-server-express')
const { SubscriptionServer } = require('subscriptions-transport-ws')
const { createServer } = require('http')
const bodyParser = require('body-parser')
const {
GraphQLObjectType,
GraphQLSchema,
execute,
subscribe
} = require('graphql')
const knex = require('knex')
const knexfile = require('./knexfile')
// Queries
const {userQueries, postQueries} = require('./server/queries')
// Mutations
const {userMutations, postMutations} = require('./server/mutations')
// database connection
const database = knex(knexfile.development)
// init the express app
const app = new express()
// build queries
const RootQuery = new GraphQLObjectType({
name: 'RootQuery',
fields: {
...userQueries,
...postQueries
}
})
// build mutations
const mutations = new GraphQLObjectType({
name: 'Mutations',
fields: {
...userMutations,
...postMutations
}
})
// build subscriptions
const subscriptions = require('./server/subscriptions')
// Schema
const schema = new GraphQLSchema({
query: RootQuery,
mutation: mutations,
subscription: subscriptions
})
// set-up the endpoint
const PORT = 3000
/*app.use('/graphql', graphqlHTTP({
schema,
graphiql: true,
context: database
}))*/
app.use('/graphql', bodyParser.json(), graphqlExpress({
schema,
context: database
}))
app.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
subscriptionsEndpoint: `ws://localhost:${PORT}/subscriptions`
}))
// create a server
const server = createServer(app)
server.listen(PORT, () => {
console.log('Server started at port: 3000')
console.log('http://localhost:3000')
// Init subscription
new SubscriptionServer({
execute,
subscribe,
schema
}, {
server,
path: '/subscriptions'
})
})