-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (71 loc) · 1.85 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
const { ApolloServer, gql } = require('apollo-server-micro');
const books = [
{ id: 1, title: 'The Trials of Brother Jero', rating: 8, authorId: 1 },
{ id: 2, title: 'Half of a Yellow Sun', rating: 9, authorId: 3 },
{ id: 3, title: 'Americanah', rating: 9, authorId: 3 },
{ id: 4, title: 'King Baabu', rating: 9, authorId: 1 },
{ id: 5, title: 'Children of Blood and Bone', rating: 8, authorId: 2 },
];
const authors = [
{ id: 1, firstName: 'Wole', lastName: 'Soyinka' },
{ id: 2, firstName: 'Tomi', lastName: 'Adeyemi' },
{ id: 3, firstName: 'Chimamanda', lastName: 'Adichie' },
];
const typeDefs = gql`
type Author {
id: Int!
firstName: String!
lastName: String!
books: [Book]! # the list of books by this author
}
type Book {
id: Int!
title: String!
rating: Int!
author: Author!
}
# the schema allows the following query
type Query {
books: [Book!]!
book(id: Int!): Book!
author(id: Int!): Author!
}
# this schema allows the following mutation
type Mutation {
addBook(title: String!, rating: Int!, authorId: Int!): Book!
}
`;
let bookId = 5;
const resolvers = {
Query: {
books: () => books,
book: (_, { id }) => books.find(book => book.id === id),
author: (_, { id }) => authors.find(author => author.id === id),
},
Mutation: {
addBook: (_, { title, rating, authorId }) => {
bookId++;
const newBook = {
id: bookId,
title,
rating,
authorId,
};
books.push(newBook);
return newBook;
},
},
Author: {
books: author => books.filter(book => book.authorId === author.id),
},
Book: {
author: book => authors.find(author => author.id === book.authorId),
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
introspection: true,
playground: true,
});
module.exports = server.createHandler();