-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.js
325 lines (311 loc) · 8.15 KB
/
schema.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
var {
makeExecutableSchema
} = require('graphql-tools');
const typeDefs = `
type Vendor {
nr: ID
label: String
comment: String
homepage: String
country: String
publisher: Int
publishDate: String
offers: [Offer] @Offer(id: "nr", relation: "vendor")
}
type Offer {
nr: ID
price: Float
validFrom: String
validTo: String
deliveryDays: Int
offerWebpage: String
product: Product @Offer(id: "product", relation: "nr")
vendor: Vendor @Offer(id: "vendor", relation: "nr")
}
type Product {
nr: ID
label: String
comment: String
offers: [Offer] @Offer(id: "nr", relation: "product")
producer: Producer @Product(id: "producer", relation: "nr")
type: ProductType @producttypeproduct(id: "productType", relation: "product")
productFeature(limit: Int): [ProductFeature] @Productfeatureproduct(id: "productFeature", relation: "product")
reviews: [Review] @Review(id: "nr", relation: "product")
}
type ProductType {
nr: ID
label: String
comment: String
products: [Product] @producttypeproduct(id: "product", relation: "productType")
}
type ProductFeature {
nr: ID
label: String
comment: String
products(limit: Int): [Product] @Productfeatureproduct(id: "product", relation: "productFeature")
}
type Producer {
nr: ID
label: String
comment: String
homepage: String
country: String
products: [Product] @Product(id: "nr", relation: "producer")
}
type Review {
nr: ID
title: String
text: String
reviewDate: String
rating1: Int
rating2: Int
rating3: Int
rating4: Int
reviewFor: Product @Review(id: "product", relation: "nr")
reviewer: Person @Review(id: "person", relation: "nr")
}
type Person {
nr: ID
name: String
mbox_sha1sum: String
country: String
reviews: [Review] @Review(id: "nr", relation: "person")
knows: [Person] @Knowsperson(id: "friend", relation: "person")
}
# the schema allows the following query:
type Query {
Review(nr: ID!): Review @Review(id: "nr", relation: "nr")
Person(nr: ID!): Person @Person(id: "nr", relation: "nr")
Product(nr: ID!): Product @Product(id: "nr", relation: "nr")
Offer(nr: ID!): Offer @Offer(id: "nr", relation: "nr")
}
`;
const resolvers = {
Query: {
Review(_, {nr}, context){
return context.reviewLoader.load(nr);
},
Person(_, {nr}, context){
return context.personLoader.load(nr);
},
Product(_, {nr}, context){
return context.productLoader.load(nr);
},
Offer(_, {nr}, context){
return context.offerLoader.load(nr);
}
},
Vendor: {
nr(vendor, args, context){
return vendor.nr;
},
label(vendor, args, context){
return vendor.label;
},
comment(vendor, args, context){
return vendor.comment;
},
homepage(vendor, args, context){
return vendor.homepage;
},
country(vendor, args, context){
return vendor.country;
},
publisher(vendor, args, context){
return vendor.publisher;
},
publishDate(vendor, args, context){
return vendor.publishDate;
},
offers(vendor, args, context){
return context.vendorOffersLoader.load(vendor.nr);
}
},
Offer: {
nr(offer, args, context){
return offer.nr;
},
price(offer, args, context){
return offer.price;
},
validFrom(offer, args, context){
return offer.validFrom;
},
validTo(offer, args, context){
return offer.validTo;
},
deliveryDays(offer, args, context){
return offer.deliveryDays;
},
offerWebpage(offer, args, context){
return offer.offerWebpage;
},
vendor(offer, args, context){
return context.offerVendorLoader.load(offer.vendor);
},
product(offer, args, context){
return context.productLoader.load(offer.product);
}
},
Review: {
nr(review, args, context) {
return review.nr;
},
title(review, args, context) {
return review.title;
},
text(review, args, context) {
return review.text;
},
reviewDate(review, args, context) {
return review.reviewDate;
},
rating1(review, args, context) {
return review.rating1;
},
rating2(review, args, context) {
return review.rating2;
},
rating3(review, args, context) {
return review.rating3;
},
rating4(review, args, context) {
return review.rating4;
},
reviewer(review, args, context) {
return context.personLoader.load(review.person);
},
reviewFor(review, args, context) {
return context.productLoader.load(review.product);
}
},
Person: {
nr(person, args, context) {
return person.nr;
},
name(person, args, context) {
return person.name;
},
mbox_sha1sum(person, args, context) {
return person.mbox_sha1sum;
},
country(person, args, context) {
return person.country;
},
reviews(person, args, context) {
return context.personReviewsLoader.load(person.nr);
},
knows(person, args, context){
return context.personKnowsLoader.load(person.nr);
}
},
Product: {
nr(product, args, context) {
return product.nr;
},
label(product, args, context) {
return product.label;
},
comment(product, args, context) {
return product.comment;
},
producer(product, args, context){
return context.productProducerLoader.load(product.producer);
},
type(product, args, context){
return context.productTypeLoader.load(product.nr);
},
productFeature(product, {limit}, context){
if(limit){ // we cannot use the data loader for queries with LIMIT
return new Promise((resolve, reject) => {
const query = 'SELECT p.nr, p.label, p.comment, pfp.product FROM productfeatureproduct pfp INNER JOIN productfeature p ON pfp.productfeature = p.nr WHERE pfp.product = ' + product.nr + ' LIMIT ' + limit;
context.db.all(query, (error, rows) => {
if (error) {
reject(error);
}
else {
return resolve( rows.map(row => row) );
}
});
});
}
else{
return context.productProductFeatureLoader.load(product.nr);
}
},
reviews(product, args, context) {
return context.productReviewsLoader.load(product.nr);
},
offers(product, args, context){
return context.productOffersLoader.load(product.nr);
}
},
ProductFeature: {
nr(feature, args, context) {
return feature.nr;
},
label(feature, args, context) {
return feature.label;
},
comment(feature, args, context) {
return feature.comment;
},
products(feature, {limit}, context){
if (limit){ // we cannot use the data loader for queries with LIMIT
return new Promise((resolve, reject) => {
const query = 'SELECT p.nr, p.label, p.comment, p.producer, pfp.productfeature FROM productfeatureproduct pfp INNER JOIN product p ON pfp.product = p.nr WHERE pfp.productfeature = ' + feature.nr + ' LIMIT ' + limit;
context.db.all(query, (error, rows) => {
if (error) {
reject(error);
}
else {
return resolve( rows.map(row => row) );
}
});
});
}
else{
return context.productFeatureProductsLoader.load(feature.nr);
}
}
},
ProductType: {
nr(type, args, context) {
return type.nr;
},
label(type, args, context) {
return type.label;
},
comment(type, args, context) {
return type.comment;
},
products(type, args, context){
return context.productTypeProductsLoader.load(type.nr);
}
},
Producer: {
nr(producer, args, context) {
return producer.nr;
},
label(producer, args, context) {
return producer.label;
},
comment(producer, args, context) {
return producer.comment;
},
homepage(producer, args, context) {
return producer.homepage;
},
country(producer, args, context) {
return producer.country;
},
products(producer, args, context){
return context.producerProductsLoader.load(producer.nr);
}
}
};
const Schema = makeExecutableSchema({
typeDefs,
resolvers
});
module.exports = Schema;