Skip to content

Commit a319be5

Browse files
authored
usePrefetchableForwardPagination (#551)
* wip * fix test + changelog * changelog about useBlockingPagination
1 parent 1bbc4ed commit a319be5

12 files changed

+1183
-81
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# master
22

33
- Add support for the new Relay `@catch` directive. https://github.com/zth/rescript-relay/pull/549
4+
- Add support for `usePrefetchableForwardPagination`. https://github.com/zth/rescript-relay/pull/551
5+
- Remove `useBlockingPagination` since it's being removed from Relay.
46

57
# 3.1.0
68

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
const t = require("@testing-library/react");
2+
const React = require("react");
3+
const queryMock = require("./queryMock");
4+
const ReactTestUtils = require("react-dom/test-utils");
5+
6+
const {
7+
test_prefetchablePagination,
8+
} = require("./Test_prefetchablePagination.bs");
9+
10+
describe("Prefetchable pagination", () => {
11+
test("prefetching works", async () => {
12+
queryMock.mockQuery({
13+
name: "TestPrefetchablePaginationQuery",
14+
data: {
15+
loggedInUser: {
16+
__typename: "User",
17+
id: "user-1",
18+
friendsConnection: {
19+
pageInfo: {
20+
endCursor: "2",
21+
hasNextPage: true,
22+
startCursor: "",
23+
hasPreviousPage: false,
24+
},
25+
edges: [
26+
{
27+
cursor: "1",
28+
node: {
29+
id: "user-2",
30+
__typename: "User",
31+
},
32+
},
33+
{
34+
cursor: "2",
35+
node: {
36+
id: "user-3",
37+
__typename: "User",
38+
},
39+
},
40+
],
41+
},
42+
},
43+
},
44+
});
45+
46+
queryMock.mockQuery({
47+
name: "TestPrefetchablePaginationRefetchQuery",
48+
variables: {
49+
count: 2,
50+
cursor: "2",
51+
id: "user-1",
52+
},
53+
data: {
54+
node: {
55+
__typename: "User",
56+
id: "user-1",
57+
friendsConnection: {
58+
pageInfo: {
59+
endCursor: "4",
60+
hasNextPage: false,
61+
startCursor: "",
62+
hasPreviousPage: false,
63+
},
64+
edges: [
65+
{
66+
cursor: "3",
67+
node: {
68+
id: "user-4",
69+
__typename: "User",
70+
},
71+
},
72+
{
73+
cursor: "4",
74+
node: {
75+
id: "user-5",
76+
__typename: "User",
77+
},
78+
},
79+
],
80+
},
81+
},
82+
},
83+
});
84+
85+
t.render(test_prefetchablePagination());
86+
await t.screen.findByText("user-2, user-3");
87+
88+
ReactTestUtils.act(() => {
89+
t.fireEvent.click(t.screen.getByText("Load more"));
90+
});
91+
92+
await t.screen.findByText("user-2, user-3, user-4, user-5");
93+
});
94+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
module Query = %relay(`
2+
query TestPrefetchablePaginationQuery {
3+
loggedInUser {
4+
...TestPrefetchablePagination_user
5+
}
6+
}
7+
`)
8+
9+
module Fragment = %relay(`
10+
fragment TestPrefetchablePagination_user on User
11+
@refetchable(queryName: "TestPrefetchablePaginationRefetchQuery")
12+
@argumentDefinitions(
13+
count: { type: "Int", defaultValue: 2 }
14+
cursor: { type: "String" }
15+
) {
16+
friendsConnection(
17+
first: $count
18+
after: $cursor
19+
) @connection(key: "TestPrefetchablePagination_friendsConnection", prefetchable_pagination: true) {
20+
edges {
21+
node {
22+
id
23+
}
24+
}
25+
}
26+
}
27+
`)
28+
29+
module Test = {
30+
@react.component
31+
let make = () => {
32+
let query = Query.use(~variables=())
33+
let {edges, hasNext, isLoadingNext, loadNext} = Fragment.usePrefetchableForwardPagination(
34+
query.loggedInUser.fragmentRefs,
35+
~bufferSize=2,
36+
)
37+
38+
<div>
39+
<div>
40+
{edges
41+
->Belt.Array.keepMap(({node}) => node)
42+
->Belt.Array.map(friend => friend.id)
43+
->Js.Array2.joinWith(", ")
44+
->React.string}
45+
</div>
46+
{hasNext
47+
? <button onClick={_ => loadNext(~count=2)->RescriptRelay.Disposable.ignore}>
48+
{React.string(isLoadingNext ? "Loading..." : "Load more")}
49+
</button>
50+
: React.null}
51+
</div>
52+
}
53+
}
54+
55+
@live
56+
let test_prefetchablePagination = () => {
57+
let network = RescriptRelay.Network.makePromiseBased(~fetchFunction=RelayEnv.fetchQuery)
58+
59+
let environment = RescriptRelay.Environment.make(
60+
~network,
61+
~store=RescriptRelay.Store.make(~source=RescriptRelay.RecordSource.make()),
62+
)
63+
64+
<TestProviders.Wrapper environment>
65+
<Test />
66+
</TestProviders.Wrapper>
67+
}

0 commit comments

Comments
 (0)