-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (32 loc) · 1.42 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
const {ApolloServer} = require('apollo-server-express')
const express = require('express')
const {MongoClient} = require('mongodb')
require('dotenv').config()
// 建立 express app
// var app = express()
// const server = new AppoServer({typeDefs, resolvers})
// server.applyMiddleware({app})
// 建立路由
// / 是 homepage
// app.get('/', (req, res)=>res.send('there you are.'))
// 建立其他路由
// 建立 /graphql 此一端點
const expressPlayground = require('graphql-playground-middleware-express').default
// app.get('/playground', expressPlayground({endpoint: '/graphql'}))
// 監聽埠號(指定))
// app.listen({port:4000}, () => console.log('hi! graphQL server starts on & running now, infoPath is ${server.graphqlPath}'))
// 建立非同步函式
async function start(){
const app = express()
const MONGO_DB = process.env.DB_HOST // 詳見 readme 第二點
const client = await MongoClient.connect(MONGO_DB, { useNewUrlParser: true})
const db = client.db()
const context = {db} // 資料物件裝入 context 命名的變數容器
const server = new AppoServer({typeDefs, resolvers})
server.applyMiddleware({app})
app.get('/', (req, res)=>res.send('there you are.'))
app.get('/playground', expressPlayground({endpoint: '/graphql'}))
app.listen({port:4000}, () => console.log('hi! graphQL server starts on & running now, infoPath is ${server.graphqlPath}'))
}
// 啟動伺服器
start()