Skip to content

Commit bb5d095

Browse files
Fix usePaginationFragment/fromData incompatibility
1 parent d9edfe5 commit bb5d095

4 files changed

Lines changed: 126 additions & 53 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# master
22

33
- fix remove-unused-fields command. https://github.com/zth/rescript-relay/pull/636
4-
- Fix `useRefetchableFragment` to be compatible with `fromData`.
4+
- Fix `useRefetchableFragment` and `usePaginationFragment` to be compatible with `fromData`.
55

66
# 4.5.1
77

packages/rescript-relay/__tests__/Test_paginationInNode-tests.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const ReactTestUtils = require("react-dom/test-utils");
44
const React = require("react");
55
const queryMock = require("./queryMock");
66

7-
const { test_pagination } = require("./Test_paginationInNode.bs");
7+
const { test_pagination, test_pagination_from_data } = require("./Test_paginationInNode.bs");
88

99
describe("Pagination nested in node", () => {
1010
test("paginating works", async () => {
@@ -180,4 +180,9 @@ describe("Pagination nested in node", () => {
180180
await t.screen.findByText("User Second has 3 friends");
181181
expect(t.screen.queryByText("User First has 2 friends")).toBeFalsy();
182182
});
183+
184+
test("usePagination supports `fromData`", async () => {
185+
t.render(test_pagination_from_data());
186+
await t.screen.findByText("Test Data has 2 friends");
187+
});
183188
});

packages/rescript-relay/__tests__/Test_paginationInNode.res

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ module Test = {
113113
}
114114
}
115115

116+
module FromDataPaginationComponent = {
117+
@react.component
118+
let make = (~user) => {
119+
let {data} = Fragment.usePagination(user)
120+
let friendCount = data.friendsConnection->Fragment.getConnectionNodes->Array.length
121+
<div> {React.string(`Test Data has ${friendCount->Int.toString} friends`)} </div>
122+
}
123+
}
124+
116125
@live
117126
let test_pagination = () => {
118127
let network = RescriptRelay.Network.makePromiseBased(~fetchFunction=RelayEnv.fetchQuery)
@@ -126,3 +135,39 @@ let test_pagination = () => {
126135
<Test />
127136
</TestProviders.Wrapper>
128137
}
138+
139+
@live
140+
let test_pagination_from_data = () => {
141+
let environment = RescriptRelay_Test.createMockEnvironment()
142+
<TestProviders.Wrapper environment>
143+
<FromDataPaginationComponent
144+
user={Fragment.Test.fromData({
145+
id: "test-data-user-1",
146+
friendsConnection: {
147+
edges: Some([
148+
Some({
149+
node: Some({
150+
id: "friend-1",
151+
fragmentRefs: UserFragment.Test.fromData({
152+
id: "friend-1",
153+
firstName: "Friend One",
154+
friendsConnection: {totalCount: 0},
155+
}),
156+
}),
157+
}),
158+
Some({
159+
node: Some({
160+
id: "friend-2",
161+
fragmentRefs: UserFragment.Test.fromData({
162+
id: "friend-2",
163+
firstName: "Friend Two",
164+
friendsConnection: {totalCount: 0},
165+
}),
166+
}),
167+
}),
168+
]),
169+
},
170+
})}
171+
/>
172+
</TestProviders.Wrapper>
173+
}

packages/rescript-relay/src/RescriptRelay_Fragment.res

Lines changed: 74 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,35 @@ let internal_makeRefetchableFnOpts = (~fetchPolicy=?, ~onComplete=?, ()) => {
8787
onComplete: ?(onComplete->RescriptRelay_Internal.internal_nullableToOptionalExnHandler),
8888
}
8989

90+
module InternalNoopDisposable = {
91+
type t = {dispose: unit => unit}
92+
external toOpaqueDisposable: t => Disposable.t = "%identity"
93+
let noop = {dispose: () => ()}->toOpaqueDisposable
94+
}
95+
96+
let internal_noopRefetch = (
97+
~variables as _,
98+
~fetchPolicy as _=?,
99+
~onComplete=?,
100+
) => {
101+
switch onComplete {
102+
| None => ()
103+
| Some(complete) => complete(None)
104+
}
105+
InternalNoopDisposable.noop
106+
}
107+
90108
type paginationLoadMoreOptions = {onComplete?: Nullable.t<JsExn.t> => unit}
91109
type paginationLoadMoreFn = (~count: int, ~onComplete: option<JsExn.t> => unit=?) => Disposable.t
110+
111+
let internal_noopLoadMore = (~count as _, ~onComplete=?) => {
112+
switch onComplete {
113+
| None => ()
114+
| Some(complete) => complete(None)
115+
}
116+
InternalNoopDisposable.noop
117+
}
118+
92119
type paginationFragmentReturnRaw<'fragment, 'refetchVariables> = {
93120
data: 'fragment,
94121
loadNext: (int, paginationLoadMoreOptions) => Disposable.t,
@@ -127,40 +154,53 @@ let usePaginationFragment = (
127154
~convertFragment: 'fragment => 'fragment,
128155
~convertRefetchVariables: 'refetchVariables => 'refetchVariables,
129156
) => {
130-
let p = usePaginationFragment_(node, fRef)
131-
let data = RescriptRelay_Internal.internal_useConvertedValue(convertFragment, p.data)
132-
{
133-
data,
134-
loadNext: React.useMemo1(() =>
135-
(~count, ~onComplete=?) => {
136-
p.loadNext(
137-
count,
138-
{onComplete: ?(onComplete->RescriptRelay_Internal.internal_nullableToOptionalExnHandler)},
139-
)
140-
}
141-
, [p.loadNext]),
142-
loadPrevious: React.useMemo1(() =>
143-
(~count, ~onComplete=?) => {
144-
p.loadPrevious(
145-
count,
146-
{onComplete: ?(onComplete->RescriptRelay_Internal.internal_nullableToOptionalExnHandler)},
147-
)
148-
}
149-
, [p.loadPrevious]),
150-
hasNext: p.hasNext,
151-
hasPrevious: p.hasPrevious,
152-
isLoadingNext: p.isLoadingNext,
153-
isLoadingPrevious: p.isLoadingPrevious,
154-
refetch: React.useMemo1(() =>
155-
(~variables, ~fetchPolicy=?, ~onComplete=?) => {
156-
p.refetch(
157-
RescriptRelay_Internal.internal_cleanObjectFromUndefinedRaw(
158-
variables->convertRefetchVariables,
159-
),
160-
internal_makeRefetchableFnOpts(~onComplete?, ~fetchPolicy?, ()),
161-
)
162-
}
163-
, [p.refetch]),
157+
switch RescriptRelay_TestFragmentRef.getDataForNode(node, fRef) {
158+
| Some(data) => {
159+
data,
160+
loadNext: internal_noopLoadMore,
161+
loadPrevious: internal_noopLoadMore,
162+
hasNext: false,
163+
hasPrevious: false,
164+
isLoadingNext: false,
165+
isLoadingPrevious: false,
166+
refetch: internal_noopRefetch,
167+
}
168+
| None =>
169+
let p = usePaginationFragment_(node, fRef)
170+
let data = RescriptRelay_Internal.internal_useConvertedValue(convertFragment, p.data)
171+
{
172+
data,
173+
loadNext: React.useMemo1(() =>
174+
(~count, ~onComplete=?) => {
175+
p.loadNext(
176+
count,
177+
{onComplete: ?(onComplete->RescriptRelay_Internal.internal_nullableToOptionalExnHandler)},
178+
)
179+
}
180+
, [p.loadNext]),
181+
loadPrevious: React.useMemo1(() =>
182+
(~count, ~onComplete=?) => {
183+
p.loadPrevious(
184+
count,
185+
{onComplete: ?(onComplete->RescriptRelay_Internal.internal_nullableToOptionalExnHandler)},
186+
)
187+
}
188+
, [p.loadPrevious]),
189+
hasNext: p.hasNext,
190+
hasPrevious: p.hasPrevious,
191+
isLoadingNext: p.isLoadingNext,
192+
isLoadingPrevious: p.isLoadingPrevious,
193+
refetch: React.useMemo1(() =>
194+
(~variables, ~fetchPolicy=?, ~onComplete=?) => {
195+
p.refetch(
196+
RescriptRelay_Internal.internal_cleanObjectFromUndefinedRaw(
197+
variables->convertRefetchVariables,
198+
),
199+
internal_makeRefetchableFnOpts(~onComplete?, ~fetchPolicy?, ()),
200+
)
201+
}
202+
, [p.refetch]),
203+
}
164204
}
165205
}
166206

@@ -250,23 +290,6 @@ external useRefetchableFragment_: (
250290
'fragmentRef,
251291
) => ('fragment, ('refetchVariables, refetchableFnOpts) => Disposable.t) = "useRefetchableFragment"
252292

253-
let internal_noopRefetch = (
254-
~variables as _,
255-
~fetchPolicy as _=?,
256-
~onComplete=?,
257-
) => {
258-
module Disposable = {
259-
type t = {dispose: unit => unit}
260-
external toOpaqueDisposable: t => Disposable.t = "%identity"
261-
let noop = { dispose : () => ()}->toOpaqueDisposable
262-
}
263-
switch onComplete {
264-
| None => ()
265-
| Some(complete) => complete(None)
266-
}
267-
Disposable.noop
268-
}
269-
270293
/**React hook for using a fragment that you want to refetch. Returns \
271294
a tuple of `(fragmentData, refetchFn)`.\n\n\
272295
### Refetching and variables\n\

0 commit comments

Comments
 (0)