Skip to content

Commit 0ab2d7a

Browse files
fix(CombinedError): use record binding to support migration to ReScript (#230)
1 parent 37f8a69 commit 0ab2d7a

File tree

6 files changed

+23
-74
lines changed

6 files changed

+23
-74
lines changed

__tests__/Types_test.re

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,22 @@ describe("Types", () => {
7979
extensions: None,
8080
};
8181

82-
let errorJs = {
83-
"message": "Error returned by GraphQL API",
84-
"graphQLErrors": [|graphQLError|],
85-
"networkError": Js.Nullable.null,
86-
"response": Js.Nullable.null,
87-
};
88-
8982
let error =
9083
CombinedError.{
84+
name: "CombinedError",
9185
message: "Error returned by GraphQL API",
9286
graphQLErrors: [|graphQLError|],
9387
networkError: None,
9488
response: None,
89+
toString: () => "Error returned by GraphQL API",
9590
};
9691

9792
let response =
9893
Types.Hooks.{
9994
operation: mockOperation,
10095
fetching: false,
10196
data: Js.Nullable.return(Js.Json.string("Hello")),
102-
error: Some(errorJs),
97+
error: Some(error),
10398
extensions: None,
10499
stale: false,
105100
};
@@ -131,27 +126,22 @@ describe("Types", () => {
131126
extensions: None,
132127
};
133128

134-
let errorJs = {
135-
"message": "Error returned by GraphQL API",
136-
"graphQLErrors": [|graphQLError|],
137-
"networkError": Js.Nullable.null,
138-
"response": Js.Nullable.null,
139-
};
140-
141129
let error =
142130
CombinedError.{
131+
name: "CombinedError",
143132
message: "Error returned by GraphQL API",
144133
graphQLErrors: [|graphQLError|],
145134
networkError: None,
146135
response: None,
136+
toString: () => "Error returned by GraphQL API",
147137
};
148138

149139
let response =
150140
Types.Hooks.{
151141
operation: mockOperation,
152142
fetching: false,
153143
data: Js.Nullable.undefined,
154-
error: Some(errorJs),
144+
error: Some(error),
155145
extensions: None,
156146
stale: false,
157147
};

src/CombinedError.re

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,8 @@
1-
class type _combinedError =
2-
[@bs]
3-
{
4-
pub networkError: Js.Nullable.t(Js.Exn.t);
5-
pub graphQLErrors: array(GraphQLError.t);
6-
pub response: Js.Nullable.t(Fetch.response);
7-
pub message: string;
8-
};
9-
10-
type combinedErrorJs = Js.t(_combinedError);
11-
[@bs.module "urql"] external combinedError: combinedErrorJs = "CombinedError";
12-
13-
type combinedError = {
14-
networkError: option(Js.Exn.t),
1+
type t = {
2+
name: string,
3+
message: string,
154
graphQLErrors: array(GraphQLError.t),
5+
networkError: option(Js.Exn.t),
166
response: option(Fetch.response),
17-
message: string,
18-
};
19-
20-
let combinedErrorToRecord = (err: combinedErrorJs): combinedError => {
21-
{
22-
networkError: err##networkError->Js.Nullable.toOption,
23-
graphQLErrors: err##graphQLErrors,
24-
response: err##response->Js.Nullable.toOption,
25-
message: err##message,
26-
};
7+
toString: unit => string,
278
};
28-
29-
type t = combinedError;

src/CombinedError.rei

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
1-
class type _combinedError =
2-
[@bs]
3-
{
4-
pub networkError: Js.Nullable.t(Js.Exn.t);
5-
pub graphQLErrors: array(GraphQLError.t);
6-
pub response: Js.Nullable.t(Fetch.response);
7-
pub message: string;
8-
};
9-
10-
type combinedErrorJs = Js.t(_combinedError);
11-
[@bs.module "urql"] external combinedError: combinedErrorJs = "CombinedError";
12-
13-
type combinedError = {
14-
networkError: option(Js.Exn.t),
1+
type t = {
2+
name: string,
3+
message: string,
154
graphQLErrors: array(GraphQLError.t),
5+
networkError: option(Js.Exn.t),
166
response: option(Fetch.response),
17-
message: string,
7+
toString: unit => string,
188
};
19-
20-
let combinedErrorToRecord: combinedErrorJs => combinedError;
21-
22-
type t = combinedError;

src/Types.re

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ type operation = {
8181
type operationResultJs('dataJs) = {
8282
operation,
8383
data: Js.Nullable.t('dataJs),
84-
error: option(CombinedError.combinedErrorJs),
84+
error: option(CombinedError.t),
8585
extensions: option(Js.Dict.t(string)),
8686
stale: option(bool),
8787
};
@@ -102,10 +102,8 @@ type operationResult('data) = {
102102

103103
let operationResultToReason =
104104
(~response: operationResultJs('dataJs), ~parse: 'dataJs => 'data) => {
105-
let {extensions, stale}: operationResultJs('dataJs) = response;
105+
let {error, extensions, stale}: operationResultJs('dataJs) = response;
106106
let data = response.data->Js.Nullable.toOption->Belt.Option.map(parse);
107-
let error =
108-
response.error->Belt.Option.map(CombinedError.combinedErrorToRecord);
109107

110108
let response =
111109
switch (data, error) {
@@ -163,7 +161,7 @@ module Hooks = {
163161
operation,
164162
fetching: bool,
165163
data: Js.Nullable.t('dataJs),
166-
error: option(CombinedError.combinedErrorJs),
164+
error: option(CombinedError.t),
167165
extensions: option(Js.Json.t),
168166
stale: bool,
169167
};
@@ -177,11 +175,9 @@ module Hooks = {
177175
(~response: hookResponseJs(dataJs), ~parse: dataJs => data) =>
178176
hookResponse(data) =
179177
(~response, ~parse) => {
180-
let {operation, fetching, extensions, stale} = response;
178+
let {operation, fetching, error, extensions, stale} = response;
181179

182180
let data = response.data->Js.Nullable.toOption->Belt.Option.map(parse);
183-
let error =
184-
response.error->Belt.Option.map(CombinedError.combinedErrorToRecord);
185181

186182
let response =
187183
switch (fetching, data, error) {

src/Types.rei

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ type operation = {
7171
type operationResultJs('dataJs) = {
7272
operation,
7373
data: Js.Nullable.t('dataJs),
74-
error: option(CombinedError.combinedErrorJs),
74+
error: option(CombinedError.t),
7575
extensions: option(Js.Dict.t(string)),
7676
stale: option(bool),
7777
};
@@ -140,7 +140,7 @@ module Hooks: {
140140
operation,
141141
fetching: bool,
142142
data: Js.Nullable.t('dataJs),
143-
error: option(CombinedError.combinedErrorJs),
143+
error: option(CombinedError.t),
144144
extensions: option(Js.Json.t),
145145
stale: bool,
146146
};

src/hooks/UseSubscription.re

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,9 @@ type useSubscriptionResponse('response) = (
4545

4646
let subscriptionResponseToReason =
4747
(response: Types.Hooks.hookResponseJs('ret)) => {
48-
let Types.Hooks.{operation, fetching, extensions, stale} = response;
48+
let Types.Hooks.{operation, fetching, error, extensions, stale} = response;
4949

5050
let data = response.data->Js.Nullable.toOption;
51-
let error =
52-
response.error->Belt.Option.map(CombinedError.combinedErrorToRecord);
5351

5452
let response =
5553
switch (fetching, data, error) {

0 commit comments

Comments
 (0)