Skip to content

Instantly share code, notes, and snippets.

@nireno
Last active February 3, 2021 21:25
Show Gist options
  • Save nireno/a2ca4a7e7f17c78e4b032ac99dbe1819 to your computer and use it in GitHub Desktop.
Save nireno/a2ca4a7e7f17c78e4b032ac99dbe1819 to your computer and use it in GitHub Desktop.
Unwrap/Map Apollo Client response promise into a Future
open! Utils;
type t('a) =
Js.Promise.t(
Belt.Result.t(
ApolloClient__React_Types.FetchResult.t__ok('a),
ApolloClient__ApolloClient.ApolloError.t,
),
);
let toFutureData:
(
t('a),
Js.Promise.error => 'b,
ApolloClient__ApolloClient.ApolloError.t => 'b
) =>
Future.t(Belt.Result.t('a, 'b)) =
(queryResult, promiseErrorTransformer, resultErrorTransformer) => {
FutureExt.fromPromiseResult(
queryResult,
promiseErrorTransformer,
resultErrorTransformer,
)
->Future.map(apolloResult => {
switch (apolloResult) {
| Ok({error: Some(apolloError)}) =>
Error(resultErrorTransformer(apolloError))
| Error(response) => Error(response)
| Ok({data}) => Ok(data)
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment