forked from hidjou/classsed-graphql-mern-apollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (41 loc) · 866 Bytes
/
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');
const gql = require('graphql-tag');
const mongoose = require('mongoose');
const Post = require('./models/Post');
const { MONGODB } = require('./config.js');
const typeDefs = gql`
type Post {
id: ID!
body: String!
createdAt: String!
username: String!
}
type Query {
getPosts: [Post]
}
`;
const resolvers = {
Query: {
async getPosts() {
try {
const posts = await Post.find();
return posts;
} catch (err) {
throw new Error(err);
}
}
}
};
const server = new ApolloServer({
typeDefs,
resolvers
});
mongoose
.connect(MONGODB, { useNewUrlParser: true })
.then(() => {
console.log('MongoDB Connected');
return server.listen({ port: 5000 });
})
.then((res) => {
console.log(`Server running at ${res.url}`);
});