-
Notifications
You must be signed in to change notification settings - Fork 158
/
index.js
65 lines (50 loc) · 1.23 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
import { gql } from 'graphql-request';
import { hygraph } from '../../lib/_hyraph';
const limit = 1;
function IndexPage({ products }) {
return products.map(({ node: product }) => (
<pre>{JSON.stringify(product, 2, null)}</pre>
));
}
export async function getStaticProps() {
const query = gql`
query indexPageQuery($limit: Int!, $offset: Int!) {
productsConnection(first: $limit, skip: $offset) {
products: edges {
node {
id
name
}
}
pageInfo {
hasNextPage
}
}
}
`;
async function* fetchData(query) {
let offset = 0;
let hasNextPage = true;
while (hasNextPage) {
const {
productsConnection: { products, pageInfo },
} = await hygraph.request(query, { limit, offset });
hasNextPage = pageInfo.hasNextPage;
offset += limit;
yield products;
}
}
async function paginatedQuery({ query }) {
const iterator = fetchData(query);
let data = [];
for await (const products of iterator) data = [...data, ...products];
return data;
}
const products = await paginatedQuery({ query });
return {
props: {
products,
},
};
}
export default IndexPage;