-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
64 lines (59 loc) · 1.67 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
const { ApolloServer } = require('apollo-server');
const typeDefs = require('./schema');
let highlights = [
{
id: '1',
content: 'One day I will find the right words, and they will be simple.',
title: 'Dharma Bums',
author: 'Jack Kerouac'
},
{
id: '2',
content:
'In the limits of a situation there is humor, there is grace, and everything else.',
title: 'Arbitrary Stupid Goal',
author: 'Tamara Shopsin'
}
];
const resolvers = {
Query: {
highlights: () => highlights,
highlight: (parent, args) => {
return highlights.find(highlight => highlight.id === args.id);
}
},
Mutation: {
newHighlight: (parent, args) => {
const highlight = {
id: String(highlights.length + 1),
title: args.title || '',
author: args.author || '',
content: args.content
};
highlights.push(highlight);
return highlight;
},
updateHighlight: (parent, args) => {
const index = highlights.findIndex(highlight => highlight.id === args.id);
const highlight = {
id: args.id,
content: args.content,
author: highlights[index].author,
title: highlights[index].title
};
highlights[index] = highlight;
return highlight;
},
deleteHighlight: (parent, args) => {
const deletedHighlight = highlights.find(
highlight => highlight.id === args.id
);
highlights = highlights.filter(highlight => highlight.id !== args.id);
return deletedHighlight;
}
}
};
const server = new ApolloServer({ typeDefs, resolvers });
server.listen().then(({ url }) => {
console.log(`📚 Highlights server ready at ${url}`);
});