This repository has been archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
LoadMoreFragments.re
65 lines (60 loc) · 1.68 KB
/
LoadMoreFragments.re
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
module GetAllPersonsQuery = [%graphql
{|
query getAllPersons($skip: Int!, $first: Int!) {
allPersons(skip: $skip, first: $first) {
...Fragments.PersonFragment.Person
}
}
|}
];
let personsPerPage = 5;
[@react.component]
let make = () => {
let (_simple, full) =
ApolloHooks.useQuery(
~variables=
GetAllPersonsQuery.makeVariables(~skip=0, ~first=personsPerPage, ()),
~notifyOnNetworkStatusChange=true,
GetAllPersonsQuery.definition,
);
let handleLoadMore = _ => {
let skip =
switch (full) {
| {data: Some(data)} => data##allPersons->Belt.Array.length
| _ => 0
};
full.fetchMore(
~variables=
GetAllPersonsQuery.makeVariables(~skip, ~first=personsPerPage, ()),
~updateQuery=[%bs.raw
{|
function(prevResult, { fetchMoreResult, ...rest }) {
if (!fetchMoreResult) return prevResult;
return {
...fetchMoreResult,
allPersons: prevResult.allPersons.concat(fetchMoreResult.allPersons)
};
}
|}
],
(),
)
|> ignore;
};
<div className="person-list">
{switch (full) {
| {loading: true, data: None} => <p> {React.string("Loading...")} </p>
| {data: Some(data)} =>
<>
<Persons persons={data##allPersons} />
<button
onClick=handleLoadMore disabled={full.networkStatus === FetchMore}>
{React.string("Load more")}
</button>
</>
| {error: Some(_)} => <p> {React.string("Error")} </p>
| {error: None, data: None, loading: false} =>
<p> {React.string("Not asked")} </p>
}}
</div>;
};