Created
July 30, 2019 10:01
-
-
Save elnygren/1495887287554d2b690c70102cd3469a to your computer and use it in GitHub Desktop.
Using ReasonApollo with a configurable wrapper
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
module type MyGraphQLMutation = { | |
module GQL: ReasonApolloTypes.Config; | |
include (module type of ReasonApolloMutation.Make(GQL)); | |
}; | |
module type MyGraphQLQuery = { | |
module GQL: ReasonApolloTypes.Config; | |
include (module type of ReasonApolloQuery.Make(GQL)); | |
}; | |
module MyQueryWrapper = (Query: MyGraphQLQuery) => { | |
[@react.component] | |
let make = (~children, ~fetchPolicy, /* etc, whatever configurations you prefer */ ) => { | |
// eg. we can do some things better than ReasonApollo | |
let fetchPolicy = switch fetchPolicy { | |
| `network => Some("network-only") | |
| `cache => Some("cache-only") | |
| `cacheFirst => Some("cache-first") | |
| _ => None | |
}; | |
// call Apollo and do some generic error handling etc. | |
<Query ?fetchPolicy> | |
{({result, refetch, fetchMore, /* etc..*/}) => | |
switch (result) { | |
| Loading => <p> "Still loading!"->React.string </p> | |
| Error(_) => <p> "Error!"->React.string </p> | |
| Data(data) => children(~result, ~data, ~fetchMore, ~refetch, /* etc... */) | |
}} | |
</Query>; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment