/
monolith.js
160 lines (149 loc) · 2.89 KB
/
monolith.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const { gql } = require("graphql-tag");
const { makeExecutableSchema } = require('@graphql-tools/schema');
const typeDefs = gql`
type Query {
me: User
users: [User]
topProducts(first: Int): [Product]
}
type User {
id: ID!
name: String
username: String
reviews: [Review]
}
type Product {
upc: String!
weight: Int
price: Int
name: String
inStock: Boolean
shippingEstimate: Int
reviews: [Review]
}
type Review {
id: ID!
body: String
author: User
product: Product
}
`;
const resolvers = {
Query: {
me() {
return users[0];
},
users() {
return users;
},
topProducts(_, args) {
return args.first ? products.slice(0, args.first) : products;
},
},
User: {
reviews(user) {
return reviews.filter((review) => review.authorID === user.id);
},
username(user) {
const found = usernames.find((username) => username.id === user.id);
return found ? found.username : null;
},
},
Product: {
shippingEstimate(object) {
// free for expensive items
if (object.price > 1000) return 0;
// estimate is based on weight
return object.weight * 0.5;
},
reviews(product) {
return reviews.filter((review) => review.productUpc === product.upc);
},
inStock(product) {
return inventory.find((inv) => inv.upc === product.upc)?.inStock;
},
},
Review: {
author(review) {
return users.find((user) => user.id === review.authorID);
},
product(review) {
return products.find(product => review.productUpc === product.upc)
}
},
};
module.exports = () => makeExecutableSchema({
typeDefs,
resolvers
});
const users = [
{
id: "1",
name: "Ada Lovelace",
birthDate: "1815-12-10",
username: "@ada",
},
{
id: "2",
name: "Alan Turing",
birthDate: "1912-06-23",
username: "@complete",
},
];
const inventory = [
{ upc: "1", inStock: true },
{ upc: "2", inStock: false },
{ upc: "3", inStock: true },
];
const listSize = parseInt(process.env.PRODUCTS_SIZE || '3');
const definedProducts = [
{
upc: '1',
name: 'Table',
price: 899,
weight: 100,
},
{
upc: '2',
name: 'Couch',
price: 1299,
weight: 1000,
},
{
upc: '3',
name: 'Chair',
price: 54,
weight: 50,
},
];
const products = Array(listSize).fill({}).map((_, index) => definedProducts[index % 3]);
const usernames = [
{ id: "1", username: "@ada" },
{ id: "2", username: "@complete" },
];
const reviews = [
{
id: '1',
authorID: '1',
productUpc: '1',
body: 'Love it!',
},
{
id: '2',
authorID: '1',
productUpc: '2',
body: 'Too expensive.',
},
{
id: '3',
authorID: '2',
productUpc: '3',
body: 'Could be better.',
},
{
id: '4',
authorID: '2',
productUpc: '1',
body: 'Prefer something else.',
},
];