Skip to content

Commit

Permalink
fix nullability with @catch
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed Jan 5, 2025
1 parent f249f97 commit 1bbc4ed
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 18 deletions.
18 changes: 18 additions & 0 deletions packages/rescript-relay/__tests__/Test_catch-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe("Catch", () => {
loggedInUser: {
id: "user-1",
createdAt: date.toISOString(),
isOnline: null,
},
},
});
Expand All @@ -30,6 +31,7 @@ describe("Catch", () => {
loggedInUser: {
id: "user-1",
createdAt: null,
isOnline: null,
},
},
graphqlErrors: [{ path: ["loggedInUser", "createdAt"] }],
Expand All @@ -39,6 +41,22 @@ describe("Catch", () => {
await t.screen.findByText("Error!");
});

test("logged in user prop - success but null", async () => {
queryMock.mockQuery({
name: "TestCatchLoggedInUserPropQuery",
data: {
loggedInUser: {
id: "user-1",
createdAt: date.toISOString(),
isOnline: null,
},
},
});

t.render(test_catch("TestLoggedInUserProp"));
await t.screen.findByText("Got isOnline: null");
});

test("logged in user prop from fragment - success", async () => {
queryMock.mockQuery({
name: "TestCatchLoggedInUserPropQuery",
Expand Down
41 changes: 29 additions & 12 deletions packages/rescript-relay/__tests__/Test_catch.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module QueryLoggedInUserProp = %relay(`
query TestCatchLoggedInUserPropQuery {
loggedInUser {
createdAt @catch
isOnline @catch
...TestCatchUser_user
}
}
Expand All @@ -18,15 +19,31 @@ module TestLoggedInUserProp = {
let make = () => {
let query = QueryLoggedInUserProp.use(~variables=())

switch query.loggedInUser.createdAt {
| Ok({value: createdAt}) =>
<div>
{React.string(
"Got createdAt: " ++ createdAt->Js.Date.toISOString->Js.String2.slice(~from=0, ~to_=10),
)}
</div>
| Error(_) => <div> {React.string("Error!")} </div>
}
<>
{switch query.loggedInUser.createdAt {
| Ok({value: createdAt}) =>
<div>
{React.string(
"Got createdAt: " ++ createdAt->Js.Date.toISOString->Js.String2.slice(~from=0, ~to_=10),
)}
</div>
| Error(_) => <div> {React.string("Error!")} </div>
}}
{switch query.loggedInUser.isOnline {
| Ok({value: isOnline}) =>
<div>
{React.string(
"Got isOnline: " ++
switch isOnline {
| Some(true) => "true"
| Some(false) => "false"
| None => "null"
},
)}
</div>
| Error(_) => <div> {React.string("Error isOnline!")} </div>
}}
</>
}
}

Expand Down Expand Up @@ -65,7 +82,7 @@ module TestMember = {
let query = QueryMember.use(~variables=())

switch query.member {
| Ok({value: User({id, createdAt})}) =>
| Ok({value: Some(User({id, createdAt}))}) =>
<div>
{React.string(
"Got user id: " ++
Expand Down Expand Up @@ -102,7 +119,7 @@ module TestMemberNested = {
let query = QueryMemberNested.use(~variables=())

switch query.member {
| Some(User({id, memberOfSingular: Ok({value: User({createdAt})})})) =>
| Some(User({id, memberOfSingular: Ok({value: Some(User({createdAt}))})})) =>
<div>
{React.string(
"Got user id: " ++
Expand Down Expand Up @@ -146,7 +163,7 @@ module TestMembers = {
members
->Js.Array2.map(r =>
switch r {
| Ok({value: User({id, createdAt})}) =>
| Ok({value: Some(User({id, createdAt}))}) =>
`User: ${id} - ${createdAt->Js.Date.toISOString->Js.String2.slice(~from=0, ~to_=10)}`
| _ => "Error!"
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1bbc4ed

Please sign in to comment.