This repository has been archived by the owner on Aug 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
/
start.js
115 lines (93 loc) · 2.7 KB
/
start.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import {MongoClient, ObjectId} from 'mongodb'
import express from 'express'
import bodyParser from 'body-parser'
import {graphqlExpress, graphiqlExpress} from 'graphql-server-express'
import {makeExecutableSchema} from 'graphql-tools'
import cors from 'cors'
import {prepare} from "../util/index"
const app = express()
app.use(cors())
const homePath = '/graphiql'
const URL = 'http://localhost'
const PORT = 3001
const MONGO_URL = 'mongodb://localhost:27017/blog'
export const start = async () => {
try {
const db = await MongoClient.connect(MONGO_URL)
const Posts = db.collection('posts')
const Comments = db.collection('comments')
const typeDefs = [`
type Query {
post(_id: String): Post
posts: [Post]
comment(_id: String): Comment
}
type Post {
_id: String
title: String
content: String
comments: [Comment]
}
type Comment {
_id: String
postId: String
content: String
post: Post
}
type Mutation {
createPost(title: String, content: String): Post
createComment(postId: String, content: String): Comment
}
schema {
query: Query
mutation: Mutation
}
`];
const resolvers = {
Query: {
post: async (root, {_id}) => {
return prepare(await Posts.findOne(ObjectId(_id)))
},
posts: async () => {
return (await Posts.find({}).toArray()).map(prepare)
},
comment: async (root, {_id}) => {
return prepare(await Comments.findOne(ObjectId(_id)))
},
},
Post: {
comments: async ({_id}) => {
return (await Comments.find({postId: _id}).toArray()).map(prepare)
}
},
Comment: {
post: async ({postId}) => {
return prepare(await Posts.findOne(ObjectId(postId)))
}
},
Mutation: {
createPost: async (root, args, context, info) => {
const res = await Posts.insertOne(args)
return prepare(res.ops[0]) // https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#~insertOneWriteOpResult
},
createComment: async (root, args) => {
const res = await Comments.insert(args)
return prepare(await Comments.findOne({_id: res.insertedIds[1]}))
},
},
}
const schema = makeExecutableSchema({
typeDefs,
resolvers
})
app.use('/graphql', bodyParser.json(), graphqlExpress({schema}))
app.use(homePath, graphiqlExpress({
endpointURL: '/graphql'
}))
app.listen(PORT, () => {
console.log(`Visit ${URL}:${PORT}${homePath}`)
})
} catch (e) {
console.log(e)
}
}