-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
52 lines (44 loc) · 1.37 KB
/
server.js
File metadata and controls
52 lines (44 loc) · 1.37 KB
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
const express = require("express");
const app = express();
const PORT = process.env.PORT || 4000;
const { ApolloServer, gql } = require("apollo-server-express");
/*The Query itself is a menu of actions that can be done
hello: String, this says that if you ask for hello, It will return a String */
const typeDefs = gql`
type Book {
title: String
author: String
}
type Query {
getBooks: [Book]
}
`;
const Books = [
{
title: "The lord of the Rings",
author: "J.R.R Tolkein",
},
{
title: "The Hobbit",
author: "J.R.R Tolkein",
},
];
/* resolvers are the code thats executed, the Schema defines what the resolver is called and what it will return
the resolver itself defines what will happen when the user requests hello */
const resolvers = {
Query: {
getBooks: () => Books,
},
};
// creating an instance of the apollo server, passing the typeDefs and the resolvers into it.
const server = new ApolloServer({ typeDefs, resolvers });
// Apply middleware to connect ApolloServer with Express, this means that when a request is
// received the request will check if it should be routed through the apollo server
/* The below automatically creates a /graphql endpoint */
server.applyMiddleware({ app });
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});