Skip to content

Instantly share code, notes, and snippets.

@elnygren
Created July 30, 2019 10:01
Show Gist options
  • Save elnygren/1495887287554d2b690c70102cd3469a to your computer and use it in GitHub Desktop.
Save elnygren/1495887287554d2b690c70102cd3469a to your computer and use it in GitHub Desktop.
Using ReasonApollo with a configurable wrapper
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