-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
192 lines (175 loc) · 5.4 KB
/
server.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const express = require('express')
const db = require("./db.json")
const {
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
GraphQLNonNull,
GraphQLInt,
GraphQLList
} = require('graphql');
const {
getGraphQLParameters,
processRequest,
renderGraphiQL,
shouldRenderGraphiQL
} = require('graphql-helix');
// [start] GraphQL Types and Schema
const AuthorType = new GraphQLObjectType({
name: "authors",
fields: ()=>({
id: {type:new GraphQLNonNull(GraphQLInt)},
name: {type: GraphQLString},
books: {
type: new GraphQLList(BookType),
resolve: (parent) => db.books.filter(itm => itm.authorId == parent.id)
}
})
})
const BookType = new GraphQLObjectType({
name: "book",
fields: ()=>({
id: {type: new GraphQLNonNull(GraphQLInt)},
name: {type: GraphQLString},
authorId: {type: GraphQLInt},
author: {
type: AuthorType,
resolve: (parent) => db.authors.find(itm => itm.id == parent.authorId)
}
})
})
const RootQuery = new GraphQLObjectType({
name: 'RootQuery',
fields: {
authors: {
type: new GraphQLList(AuthorType),
resolve: () => db.authors,
},
books: {
type: new GraphQLList(BookType),
resolve: () => db.books
},
book: {
type: BookType,
args:{id:{type: GraphQLInt}},
resolve: (parent, args) => db.books.find(itm => args.id == itm.id)
},
author: {
type: AuthorType,
args: {id: {type:GraphQLInt}},
resolve: (parent, args) => db.authors.find(itm => itm.id == args.id)
}
},
})
const RootMutation = new GraphQLObjectType({
name: "RootMutation",
fields: {
addBook:{
type: BookType,
args: {
name: {type: new GraphQLNonNull(GraphQLString)},
authorId: {type: new GraphQLNonNull(GraphQLInt)},
},
resolve: (parent, args) => {
const book = {id: db.books.length+1, name: args.name, authorId: args.authorId};
db.books.push(book);
return book;
}
},
updateBook:{
type: BookType,
args: {
id: {type: new GraphQLNonNull(GraphQLInt)},
name: {type: GraphQLString},
authorId: {type: GraphQLInt},
},
resolve: (parent, args) => {
const index = db.books.findIndex(itm => itm.id == args.id)
db.books[index].authorId = args.authorId || db.books[index].authorId
db.books[index].name = args.name || db.books[index].name
return db.books[index];
}
},
deleteBook:{
type: BookType,
args: {
id: {type: new GraphQLNonNull(GraphQLInt)},
},
resolve: (parent, args) => {
const book = db.books.find(itm => itm.id == args.id);
db.books = db.books.filter(itm => itm.id !== args.id);
return book;
}
},
addAuthor:{
type: AuthorType,
args: {
name: {type: new GraphQLNonNull(GraphQLString)},
},
resolve: (parent, args) => {
const author = {id: db.authors.length+1, name: args.name};
db.authors.push(author);
return author;
}
},
deleteAuthor:{
type: AuthorType,
args: {
id: {type: new GraphQLNonNull(GraphQLInt)},
},
resolve: (parent, args) => {
const author = db.authors.find(itm => itm.id == args.id);
db.authors = db.authors.filter(itm => itm.id !== args.id);
return author;
}
},
updateAuthor:{
type: AuthorType,
args: {
id: {type: new GraphQLNonNull(GraphQLInt)},
name: {type: GraphQLString}
},
resolve: (parent, args) => {
const index = db.authors.findIndex(itm => itm.id == args.id)
db.authors[index].name = args.name || db.authors[index].name
return db.authors[index];
}
},
}
})
const schema = new GraphQLSchema({
query: RootQuery,
mutation: RootMutation
});
// [end] GraphQL Types and Schema
const app = express();
app.use(express.json());
app.use('/graphql', async (req, res) => {
const request = {
body: req.body,
headers: req.headers,
method: req.method,
query: req.query,
};
if (shouldRenderGraphiQL(request)) {
res.send(renderGraphiQL());
} else {
const { operationName, query, variables } = getGraphQLParameters(request);
const result = await processRequest({
operationName,
query,
variables,
request,
schema,
});
if (result.type === 'RESPONSE') {
result.headers.forEach(({ name, value }) => res.setHeader(name, value));
res.status(result.status);
res.json(result.payload);
} else {
// graphql-helix also supports subscriptions and incremental delivery (i.e. @defer and @stream directives)
// out of the box. See the repo for more complete examples that also implement those features.
}
}
});
app.listen(5000, () => console.log("Server is running on Port 5000"));