Skip to content

Commit dfd9b70

Browse files
authored
Fix refetch empty array (#601)
* add failing test that illustrates the problem * fix * changelog * add one more test
1 parent 3cd40b6 commit dfd9b70

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

CHANGELOG.md

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

3+
- Fix issue with empty arrays and refetch variables. https://github.com/zth/rescript-relay/pull/601
4+
35
# 4.2.1
46

57
- Actually apply fix for lowering the glibc requirement from the PR in 3.5.1, to the release. This was not applied to the actual release until now.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const {
2+
internal_removeUndefinedAndConvertNullsRaw,
3+
} = require("../src/RescriptRelay_Internal.bs");
4+
5+
describe("internal_removeUndefinedAndConvertNullsRaw", () => {
6+
it("keeps empty arrays while converting Some(None) to null", () => {
7+
const input = {
8+
after: { BS_PRIVATE_NESTED_SOME_NONE: 0 },
9+
forSaleStatuses: [],
10+
nullArray: [null],
11+
first: 12,
12+
omitted: undefined,
13+
};
14+
15+
expect(internal_removeUndefinedAndConvertNullsRaw(input)).toEqual({
16+
after: null,
17+
forSaleStatuses: [],
18+
nullArray: [null],
19+
first: 12,
20+
});
21+
});
22+
23+
it("drops None values and preserves non-null data", () => {
24+
const input = {
25+
noneValue: undefined,
26+
zero: 0,
27+
empty: "",
28+
nope: false,
29+
obj: { bar: 1 },
30+
};
31+
32+
expect(internal_removeUndefinedAndConvertNullsRaw(input)).toEqual({
33+
zero: 0,
34+
empty: "",
35+
nope: false,
36+
obj: { bar: 1 },
37+
});
38+
});
39+
40+
});

packages/rescript-relay/src/RescriptRelay_Internal.res

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,19 @@ let internal_cleanObjectFromUndefinedRaw = record =>
1717
| Some(v) => v
1818
}
1919

20+
let internal_isSomeNone: 'any => bool = %raw(`value => value != null && value.BS_PRIVATE_NESTED_SOME_NONE === 0`)
21+
2022
let internal_removeUndefinedAndConvertNullsRaw = record =>
2123
internal_keepMapFieldsRaw(record, ((key, value)) => {
22-
switch (value, value == Some(None)) {
23-
| (_, true) => Some((key, Nullable.null))
24-
| (Some(value), _) => Some((key, Nullable.make(value)))
25-
| (None, _) => None
24+
let rawValue = value
25+
switch value {
26+
| Some(value) =>
27+
if internal_isSomeNone(rawValue) {
28+
Some((key, Nullable.null))
29+
} else {
30+
Some((key, Nullable.make(value)))
31+
}
32+
| None => None
2633
}
2734
})
2835

0 commit comments

Comments
 (0)