-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (85 loc) · 2.15 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
const express = require("express");
const { ApolloServer } = require("@apollo/server");
const { expressMiddleware } = require("@apollo/server/express4");
const bodyParser = require("body-parser");
const cors = require("cors");
const axios = require("axios");
const app = express();
const fakeData = [
{
id: 1,
title: "something title",
description: "Ad aute cupidatat sit in.",
completed: false,
},
{
id: 2,
title: "something lorem title",
description:
"Dolore ullamco officia qui aute magna Lorem exercitation commodo eiusmod ut id Lorem voluptate.",
completed: false,
},
{
id: 3,
title: "something fake title",
description:
"Eiusmod sit ullamco aliquip Lorem tempor dolor qui esse ut sint velit magna consectetur.",
completed: false,
},
{
id: 1,
title: "something trial title",
description:
"Fugiat nisi ut exercitation ipsum aliqua deserunt in elit adipisicing pariatur.",
completed: false,
},
];
async function startTodoServer() {
const server = new ApolloServer({
typeDefs: `
type Todo{
id: ID!
title: String!,
completed: Boolean!,
user: User
}
type User{
id: ID!
name: String!
username: String!
email: String!
phone: String!
}
type Query{
getTodos: [Todo]
getUsers: [User]
getUser(id: ID!): User
}
`,
resolvers: {
Todo: {
user: async (todo) =>
(
await axios.get(
`https://jsonplaceholder.typicode.com/users/${todo.userId}`
)
).data,
},
Query: {
getTodos: async () =>
(await axios.get("https://jsonplaceholder.typicode.com/todos")).data,
getUsers: async () =>
(await axios.get("https://jsonplaceholder.typicode.com/users")).data,
getUser: async (parent, { id }) =>
(await axios.get(`https://jsonplaceholder.typicode.com/users/${id}`))
.data,
},
},
});
await server.start();
app.use(bodyParser.json());
app.use(cors());
app.use("/graphql", expressMiddleware(server));
app.listen(8000, () => console.log("Server up and running at port 8000"));
}
startTodoServer();