-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (35 loc) · 985 Bytes
/
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
const { ApolloServer, gql } = require("apollo-server");
// import schema from '.schema.graphql'
const quotes = require("./quotes.json");
const typeDefs = gql`
# Comments in GraphQL strings (such as this one) start with the hash (#) symbol.
type Quote {
quote: String!
author: String
}
type Query {
randomQuote: Quote
allQuotes: [Quote]
}
`;
const resolvers = {
Query: {
randomQuote: () => {
const quote = quotes[Math.floor(Math.random() * quotes.length)];
// do i need to return this as an object, or can I simply return the quote found?
return {
quote: quote.quote,
author: quote.author ? quote.author : null,
};
},
allQuotes: () => quotes,
//TODO: add quote by author query, but relegate to DB
},
};
const server = new ApolloServer({
typeDefs, // Your schema
resolvers, // Your resolver functions
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});