Last active
February 3, 2021 21:25
-
-
Save nireno/a2ca4a7e7f17c78e4b032ac99dbe1819 to your computer and use it in GitHub Desktop.
Unwrap/Map Apollo Client response promise into a Future
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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