-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (75 loc) · 2.34 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
require('dotenv').config();
const { ApolloServer, AuthenticationError, gql } = require('apollo-server');
const uuid = require('uuid/v4');
const { getToken, getUserIdFromToken, getUser } = require('./auth');
const typeDefs = gql`
type Quote {
id: ID!
phrase: String!
quotee: String
}
type Query {
quotes: [Quote]
}
type Mutation {
login(username: String!, password: String!): Authentication
addQuote(phrase: String!, quotee: String): Quote
editQuote(id: ID!, phrase: String, quotee: String): Quote
deleteQuote(id: ID!): DeleteResponse
}
type Authentication {
token: String!
}
type DeleteResponse {
ok: Boolean!
}
`;
const quotes = {};
const addQuote = quote => {
const id = uuid();
return quotes[id] = { ...quote, id };
};
addQuote({ phrase: "I'm a leaf on the wind. Watch how I soar.", quotee: "Wash" });
addQuote({ phrase: "We're all stories in the end.", quotee: "The Doctor" });
addQuote({ phrase: "Woah!", quotee: "Neo" });
const resolvers = {
Query: {
quotes: () => Object.values(quotes),
},
Mutation: {
login: async (parent, { username, password }) => ({
token: await getToken({ username, password }),
}),
addQuote: async (parent, quote, context) => {
if (!context.user) throw new AuthenticationError("You must be logged in to perform this action");
return addQuote(quote);
},
editQuote: async (parent, { id, ...quote }, context) => {
if (!context.user) throw new AuthenticationError("You must be logged in to perform this action");
if (!quotes[id]) {
throw new Error("Quote doesn't exist");
}
quotes[id] = {
...quotes[id],
...quote,
};
return quotes[id];
},
deleteQuote: async (parent, { id }, context) => {
if (!context.user) throw new AuthenticationError("You must be logged in to perform this action");
const ok = Boolean(quotes[id]);
delete quotes[id];
return { ok };
},
},
};
const context = async ({ req }) => {
const [, token] = (req.headers.authorization || '').split("Bearer ");
return {
user: await getUser(await getUserIdFromToken(token)),
};
};
const server = new ApolloServer({ typeDefs, resolvers, context });
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`); // eslint-disable-line no-console
});