-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
149 lines (127 loc) · 3.52 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* @Name: First NodeJs GraphQL
* @Author: Max Base
* @Repository: https://github.com/BaseMax/first-nodejs-graphql
* @Date: 2020-12-24
*/
const mongoose = require('mongoose');
const Author = require('./models/author');
const Book = require('./models/book');
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLList,
GraphQLNonNull
} = require('graphql');
const app = express();
// initialize .env
require('dotenv').config({ path: './config/.env' });
// connect to mong db
const db = mongoose.connect(`${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}`);
db.then(() => console.log('Connected to MongoDB!')).catch(err => console.error('MongoDB connection error:', err));
const AuthorType = new GraphQLObjectType({
name: 'Author',
description: 'This represents an author of a book',
fields: () => ({
id: { type: GraphQLNonNull(GraphQLString) },
name: { type: GraphQLNonNull(GraphQLString) },
books: {
type: new GraphQLList(BookType),
resolve: async (author) => {
return await Book.find({ authorId: author.id });
}
}
})
});
const BookType = new GraphQLObjectType({
name: 'Book',
description: 'This represents a book written by an author',
fields: () => ({
id: { type: GraphQLNonNull(GraphQLString) },
name: { type: GraphQLNonNull(GraphQLString) },
authorId: { type: GraphQLNonNull(GraphQLString) },
author: {
type: AuthorType,
resolve: async (book) => {
return await Author.findById(book.authorId);
}
}
})
});
const RootQueryType = new GraphQLObjectType({
name: 'Query',
description: 'Root Query',
fields: () => ({
book: {
type: BookType,
description: 'A Single Book',
args: {
id: { type: GraphQLString }
},
resolve: async (parent, args) => await Book.findById(args.id),
},
books: {
type: new GraphQLList(BookType),
description: 'List of All Books',
resolve: async () => await Book.find(),
},
author: {
type: AuthorType,
description: 'A Single Author',
args: {
id: { type: GraphQLString }
},
resolve: async (parent, args) => await Author.findById(args.id),
},
authors: {
type: new GraphQLList(AuthorType),
description: 'List of All Authors',
resolve: async () => await Author.find(),
}
})
});
const RootMutationType = new GraphQLObjectType({
name: 'Mutation',
description: 'Root Mutation',
fields: () => ({
addBook: {
type: BookType,
description: 'Add a book',
args: {
name: { type: GraphQLNonNull(GraphQLString) },
authorId: { type: GraphQLNonNull(GraphQLString) },
},
resolve: async (parent, args) => {
const book = new Book({ name: args.name, authorId: args.authorId });
await book.save();
return book;
}
},
addAuthor: {
type: AuthorType,
description: 'Add an author',
args: {
name: { type: GraphQLNonNull(GraphQLString) },
},
resolve: async (parent, args) => {
const author = new Author({ name: args.name });
await author.save();
return author;
}
}
})
});
const schema = new GraphQLSchema({
query: RootQueryType,
mutation: RootMutationType,
});
app.use('/graphql', graphqlHTTP({
schema: schema,
graphiql: true
}));
app.listen(process.env.PORT, () => {
console.log(`Server running on port ${process.env.PORT}`);
});