@@ -20,6 +20,9 @@ const quickSearchResultsCache: { [cacheKey: string]: QuickSearchResult[] } = {};
20
20
21
21
/**
22
22
* This module gets a bit messy, we'll probably refactor it at some point :-)
23
+ *
24
+ * N.B.: I haven't used a specific GraphQL client library like Apollo, because I always send the same static queries and
25
+ * I don't need to add the size of such a client lib to my JS.
23
26
*/
24
27
export class BooksGraphqlRepository implements BooksRepository {
25
28
public async getFeaturedBooks ( lang : Lang ) : Promise < BooksById > {
@@ -36,7 +39,7 @@ query {
36
39
slug
37
40
coverPath
38
41
genres
39
-
42
+
40
43
author {
41
44
authorId
42
45
firstName
@@ -47,8 +50,8 @@ query {
47
50
nbBooks
48
51
}
49
52
}
50
-
51
- }
53
+
54
+ }
52
55
` ;
53
56
const response = await this . requestGraphql ( graphqlQuery , null ) ;
54
57
const featuredBooks : Book [ ] = response . data . data . featuredBooks . map ( mapBookFromServer ) ;
@@ -71,7 +74,7 @@ query bookById($bookId: BookId!) {
71
74
genres
72
75
epubSize
73
76
mobiSize
74
-
77
+
75
78
author {
76
79
authorId
77
80
firstName
@@ -82,18 +85,18 @@ query bookById($bookId: BookId!) {
82
85
nbBooks
83
86
}
84
87
}
85
-
88
+
86
89
genresStats {
87
90
title
88
91
nbBooks
89
-
92
+
90
93
nbBooksByLang {
91
94
lang
92
95
nbBooks
93
96
}
94
97
}
95
98
}
96
-
99
+
97
100
}
98
101
99
102
` ;
@@ -112,11 +115,27 @@ query bookById($bookId: BookId!) {
112
115
return Promise . resolve ( cacheForThisPatternAndLang ) ;
113
116
}
114
117
115
- const response = await axios . get ( "/rpc/quick_autocompletion" , {
116
- params : { pattern, lang } ,
117
- } ) ;
118
+ const graphqlQuery = `
119
+ query quickSearch($pattern: String!, $lang: String) {
120
+
121
+ quickSearch(search: $pattern, lang: $lang) {
122
+ type
123
+ bookId
124
+ bookLang
125
+ bookTitle
126
+ bookSlug
127
+ authorId
128
+ authorFirstName
129
+ authorLastName
130
+ authorSlug
131
+ authorNbBooks
132
+ highlight
133
+ }
118
134
119
- const matchingBooks = response . data . map ( mapQuickAutocompletionDataFromServer ) ;
135
+ }
136
+ ` ;
137
+ const response = await this . requestGraphql ( graphqlQuery , { pattern, lang } ) ;
138
+ const matchingBooks = response . data . data . quickSearch . map ( mapQuickSearchDataFromServer ) ;
120
139
quickSearchResultsCache [ cacheKey ] = matchingBooks ;
121
140
122
141
return Promise . resolve ( matchingBooks ) ;
@@ -127,19 +146,53 @@ query bookById($bookId: BookId!) {
127
146
lang : Lang ,
128
147
pagination : PaginationRequestData
129
148
) : Promise < PaginatedBooksList > {
130
- const response = await axios . get ( "/rpc/get_books_by_genre" , {
131
- params : {
132
- genre,
133
- lang,
134
- page : pagination . page ,
135
- nb_per_page : pagination . nbPerPage ,
136
- } ,
149
+ const graphqlQuery = `
150
+ query booksByGenre($genre: String!, $lang: String, $page: Int, $nbPerPage: Int) {
151
+
152
+ booksByGenre(genre: $genre, lang: $lang, page: $page, nbPerPage: $nbPerPage) {
153
+
154
+ books {
155
+ bookId
156
+ lang
157
+ title
158
+ subtitle
159
+ slug
160
+ coverPath
161
+ genres
162
+
163
+ author {
164
+ authorId
165
+ firstName
166
+ lastName
167
+ birthYear
168
+ deathYear
169
+ slug
170
+ nbBooks
171
+ }
172
+ }
173
+
174
+ meta {
175
+ page
176
+ nbPerPage
177
+ nbResults
178
+ nbResultsForAllLangs
179
+ }
180
+
181
+ }
182
+
183
+ }
184
+ ` ;
185
+ const response = await this . requestGraphql ( graphqlQuery , {
186
+ genre,
187
+ lang,
188
+ page : pagination . page ,
189
+ nbPerPage : pagination . nbPerPage ,
137
190
} ) ;
138
191
139
192
const booksWithPagination : ServerResponse . BooksDataWithPagination < ServerResponse . BookData > =
140
- response . data [ 0 ] ;
193
+ response . data . data . booksByGenre ;
141
194
const paginationData : PaginationResponseData = getPaginationResponseDataFromServerResponse (
142
- booksWithPagination . pagination
195
+ booksWithPagination . meta
143
196
) ;
144
197
const booksForThisGenre = getBooksByIdFromBooksArray (
145
198
( booksWithPagination . books || [ ] ) . map ( mapBookFromServer )
@@ -166,9 +219,9 @@ query bookById($bookId: BookId!) {
166
219
} ) ;
167
220
168
221
const booksWithPagination : ServerResponse . BooksDataWithPagination < ServerResponse . BookData > =
169
- response . data [ 0 ] ;
222
+ response . data . data . booksByAuthor ;
170
223
const paginationData : PaginationResponseData = getPaginationResponseDataFromServerResponse (
171
- booksWithPagination . pagination
224
+ booksWithPagination . meta
172
225
) ;
173
226
const booksForThisGenre = getBooksByIdFromBooksArray (
174
227
( booksWithPagination . books || [ ] ) . map ( mapBookFromServer )
@@ -187,7 +240,7 @@ query bookIntro($bookId: BookId!) {
187
240
book(bookId: $bookId) {
188
241
intro
189
242
}
190
-
243
+
191
244
}
192
245
193
246
@@ -256,51 +309,49 @@ function mapBookWithGenreStatsFromServer(
256
309
}
257
310
258
311
function mapGenreWithStatsFromServer ( row : ServerResponse . GenreWithStats ) : GenreWithStats {
259
- let nbBooksByLang : { [ lang : string ] : number } = { } ;
312
+ const nbBooksByLang : { [ lang : string ] : number } = { } ;
260
313
for ( const nbBooksForLang of row . nbBooksByLang ) {
261
314
nbBooksByLang [ nbBooksForLang . lang ] = nbBooksForLang . nbBooks ;
262
315
}
263
316
264
317
return {
265
318
title : row . title ,
266
319
nbBooks : row . nbBooks ,
267
- nbBooksByLang : nbBooksByLang ,
320
+ nbBooksByLang,
268
321
} ;
269
322
}
270
323
271
- function mapQuickAutocompletionDataFromServer (
272
- row : ServerResponse . QuickAutocompletionData
273
- ) : QuickSearchResult {
324
+ function mapQuickSearchDataFromServer ( row : ServerResponse . QuickSearchData ) : QuickSearchResult {
274
325
const rowType = row . type ;
275
326
return {
276
327
resultType : rowType ,
277
328
book :
278
329
"book" === rowType
279
330
? {
280
- id : row . book_id as string ,
281
- title : row . book_title as string ,
282
- lang : row . book_lang as string ,
283
- slug : row . book_slug as string ,
331
+ id : row . bookId as string ,
332
+ title : row . bookTitle as string ,
333
+ lang : row . bookLang as string ,
334
+ slug : row . bookSlug as string ,
284
335
}
285
336
: null ,
286
337
author : {
287
- id : row . author_id ,
288
- firstName : row . author_first_name ,
289
- lastName : row . author_last_name ,
290
- slug : row . author_slug ,
291
- nbBooks : row . author_nb_books ,
338
+ id : row . authorId ,
339
+ firstName : row . authorFirstName ,
340
+ lastName : row . authorLastName ,
341
+ slug : row . authorSlug ,
342
+ nbBooks : row . authorNbBooks ,
292
343
} ,
293
344
highlight : row . highlight ,
294
345
} ;
295
346
}
296
347
297
348
function getPaginationResponseDataFromServerResponse (
298
- responsePagination : ServerResponse . PaginationResponseData
349
+ responsePagination : ServerResponse . PaginationMetaData
299
350
) : PaginationResponseData {
300
351
return {
301
352
page : responsePagination . page ,
302
- nbPerPage : responsePagination . nb_per_page ,
303
- nbResultsTotal : responsePagination . nb_results_total ,
304
- nbResultsTotalForAllLangs : responsePagination . nb_results_total_for_all_langs ,
353
+ nbPerPage : responsePagination . nbPerPage ,
354
+ nbResultsTotal : responsePagination . nbResults ,
355
+ nbResultsTotalForAllLangs : responsePagination . nbResultsForAllLangs ,
305
356
} ;
306
357
}
0 commit comments