forked from moharnadreza/jsonplaceholder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (44 loc) · 1.14 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
// use env file to config
const dotenv = require('dotenv').config()
// required packages
const jsonServer = require('json-server')
const clone = require('clone')
const {
ApolloServer,
gql
} = require('apollo-server-express');
// data
const data = require('./data.json')
// configs
const port = process.env.PORT || 3000
// make app
const app = jsonServer.create()
// define router
const router = jsonServer.router(clone(data), {
_isFake: true
})
// required controllers and configs for graphql
const typeDefs = require('./graphql/typeDefs');
const resolvers = require('./graphql/resolvers');
// make apollo server
const server = new ApolloServer({
typeDefs,
resolvers
})
// apply app to apollo server
server.applyMiddleware({
app
})
app.use((req, res, next) => {
if (req.path === '/') return next()
router.db.setState(clone(data))
next()
})
app.use(jsonServer.defaults({
logger: process.env.NODE_ENV !== 'production'
}))
app.use(router)
// listen to port
app.listen(port, () => {
console.log(`Perisan JSONPlaceholder listening on http://localhost:${port} and graphql is running on http://localhost:${port}${server.graphqlPath}`)
})