-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (71 loc) · 1.84 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
var express = require('express');
var bodyParser = require('body-parser');
var { ApolloServer} = require('apollo-server-express');
var { makeExecutableSchema } = require('graphql-tools');
var book = require('./services/index.js')
var typeDefs = [`
type Query {
book(id: String!): Book,
books: [Book]
}
input bookInput {
author: String
title: String
}
type Book {
id: String!
author: String
contributor: String
contributor_note: String
description: String
price: String
primary_isbn10: String
reOrderQuantity: String
primary_isbn13: String
publisher: Boolean
title: String,
authors: [Author]
}
type Author {
id: String!
first_name: String
last_name: String
email: String
num_of_books: String
}
type Mutation {
addBook(book: bookInput): Book,
updateBook(id: String!, book: bookInput): Book,
deleteBook(id: String!): [Book]
}`
];
var resolvers = {
Query: {
book: function (_source, args, _context) {
return book.findOne(args.id)
},
books: function (_source, _args, _context) {
return book.findAll()
}
},
Mutation: {
addBook: function (_source, args, _context) {
return book.createBook(args.book)
},
updateBook: function (_source, args, _context) {
return book.updateBook(args.id, args.book)
},
deleteBook: function (_source, args, _context) {
return book.deleteBook(args.id)
}
},
};
var schema = makeExecutableSchema({ typeDefs, resolvers });
var app = express();
var PORT = 4001
app.use(bodyParser.urlencoded({ extended: false }))
var server = new ApolloServer({ schema })
server.applyMiddleware({ app })
app.listen({ port: PORT }, function () {
console.log(`🚀 Server ready at http://localhost:${PORT}${server.graphqlPath}`)
});