-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
33 lines (28 loc) · 1.16 KB
/
server.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
const { readFileSync } = require("fs");
const express = require("express");
const bodyParser = require("body-parser");
const { graphqlExpress, graphiqlExpress } = require("apollo-server-express");
const { makeExecutableSchema } = require("graphql-tools");
const salad = { avocado: 1, mango: 1, tomato: 0.2, arugula: true, onion: true };
const burger = { buns: 2, shrimp: 1, egg: 1, lettuce: 2.5, mayo: true };
const salads = new Array(100).fill(salad);
const burgers = new Array(100).fill(burger);
const get = (what, count) => what.splice(0, parseInt(count) || 1);
const schema = makeExecutableSchema({
typeDefs: readFileSync("02-graphql/schema.graphql", "utf8"),
resolvers: {
Query: {
salads: (_, { count }) => get(salads, count),
burgers: (_, { count }) => get(burgers, count)
}
}
});
const app = express();
app.get("/salads", ({ query: { count } }, res) => res.json(get(salads, count)));
app.get("/burgers", ({ query: { count } }, res) =>
res.json(get(burgers, count))
);
app.use("/graphql", bodyParser.json(), graphqlExpress({ schema }));
app.use("/graphiql", graphiqlExpress({ endpointURL: "/graphql" }));
app.listen(4000);
module.exports = app;