File tree 1 file changed +18
-8
lines changed
web/api-modules/products/use-cases
1 file changed +18
-8
lines changed Original file line number Diff line number Diff line change @@ -11,10 +11,10 @@ const toProduct = (product) => ({
11
11
} ) )
12
12
} ) ;
13
13
14
- async function findProducts ( client ) {
14
+ async function findProducts ( client , after ) {
15
15
const response = await client . request ( `
16
- {
17
- products(first: 250, sortKey: TITLE) {
16
+ query findProducts($after: String) {
17
+ products(first: 250, after: $after, sortKey: TITLE) {
18
18
edges {
19
19
node {
20
20
id
@@ -59,19 +59,29 @@ async function findProducts(client) {
59
59
}
60
60
}
61
61
}
62
- }
62
+ },
63
+ pageInfo {
64
+ hasPreviousPage
65
+ hasNextPage
66
+ startCursor
67
+ endCursor
68
+ }
63
69
}
64
70
}
65
- ` ) ;
71
+ ` , { variables : { after } } ) ;
66
72
67
73
if ( response . errors ) {
68
74
console . error ( 'Failed to load Products' , JSON . stringify ( response . errors ) ) ;
69
75
throw new Error ( 'Failed to load Products' ) ;
70
76
}
71
77
72
- return response ?. data ?. products ?. edges . map ( ( { node : product } ) =>
73
- toProduct ( product )
74
- ) ;
78
+ const thisPage = response ?. data ?. products ?. edges . map ( ( { node : product } ) =>
79
+ toProduct ( product ) ) ;
80
+
81
+ if ( response ?. data ?. products ?. pageInfo . hasNextPage ) {
82
+ const rest = await findProducts ( client , response ?. data ?. products ?. pageInfo . endCursor ) ;
83
+ return [ ...thisPage , ...rest ] ;
84
+ } return thisPage ;
75
85
}
76
86
77
87
const getProducts = async ( { session } ) => {
You can’t perform that action at this time.
0 commit comments