Skip to content
This repository has been archived by the owner on Nov 20, 2020. It is now read-only.

Make compatible with GraphQL PPX 1.0 beta #117

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion bsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
}
],
"suffix": ".bs.js",
"bs-dependencies": ["reason-react", "reason-apollo"],
"bs-dependencies": ["reason-react"],
"refmt": 3
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
"@apollo/react-hooks": "^3.0.0",
"@commitlint/cli": "^8.2.0",
"@commitlint/config-conventional": "^8.2.0",
"bs-platform": "^7.0.1",
"bs-platform": "^7.2.0",
"bsdoc": "6.0.1-alpha",
"husky": "^3.0.5",
"lint-staged": "^9.4.0",
"reason-apollo": "^0.18.0",
"reason-react": ">=0.7.0"
},
"peerDependencies": {
Expand Down
43 changes: 31 additions & 12 deletions src/ApolloHooks.re → src/ApolloClient.re
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
module Mutation = ApolloHooksMutation;
module Query = ApolloHooksQuery;
module Provider = ApolloHooksProvider;
module Subscription = ApolloHooksSubscription;
module Mutation = ApolloClient_Mutation;
module Query = ApolloClient_Query;
module Provider = ApolloClient_Provider;
module Subscription = ApolloClient_Subscription;
module Client = ApolloClient_Client;
module Link = ApolloClient_ApolloLink;
module InMemoryCache = ApolloClient_InMemoryCache;

/**
This is probably the one hook you'll use the most. A quick demo:

{[
open ApolloHooks;
open ApolloClient;

module Query = [%graphql {|
query MyQuery {
Expand Down Expand Up @@ -85,12 +88,13 @@ module Subscription = ApolloHooksSubscription;
That covers the most common cases of usage. If you want to see more complex usages check out the examples folder.
*/
let useQuery = Query.useQuery;
let useQueryLegacy = Query.useQueryLegacy;

/**
Second most used! Here's a quick demo:

{[
open ApolloHooks;
open ApolloClient;

module Mutation = [%graphql {|
mutation MyMutation($input: MyMutationInput!) {
Expand Down Expand Up @@ -124,7 +128,7 @@ let useQuery = Query.useQuery;
Or if you only care about calling [mutate]

{[
open ApolloHooks;
open ApolloClient;

module Mutation = [%graphql {|
mutation MyMutation {
Expand All @@ -151,13 +155,28 @@ let useQuery = Query.useQuery;
]}
*/
let useMutation = Mutation.useMutation;
let useMutationLegacy = Mutation.useMutationLegacy;
let useMutationWithVariables = Mutation.useMutationWithVariables;

let toReadQueryOptions = result => {
"query": Client.gql(. result##query),
"variables": Js.Nullable.fromOption(Some(result##variables)),
};

/** useSubscription bindings */
let useSubscription = Subscription.useSubscription;

/** Helper to generate the shape of a query for [refetchQueries] mutation param. Take a look in examples/persons/src/EditPerson.re for a more complete demo of usage. */
let toQueryObj = result =>
ApolloClient.{
query: ApolloClient.gql(. result##query),
variables: result##variables,
};
let toQueryObj:
(string, 'raw_t_variables) => Client.queryObj('raw_t_variables) =
(theQuery, variables) =>
Client.{query: Client.gql(. theQuery), variables};

let toOpaqueQueryObj: (string, 'raw_t_variables) => Client.opaqueQueryObj =
(theQuery, variables) =>
Client.toOpaqueQueryObj(
Client.{query: Client.gql(. theQuery), variables},
);

[@bs.module "@apollo/client"]
external useApolloClient: unit => ApolloClient_Client.t = "useApolloClient";
148 changes: 148 additions & 0 deletions src/ApolloClient_ApolloLink.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
module Types = ApolloClient_Types;

type t = Types.apolloLink;
type fetch;
type request;
type context;

type linkOptions = {
uri: string,
includeExtensions: Js.Nullable.t(bool),
fetch: Js.Nullable.t(fetch),
headers: Js.Nullable.t(Js.Json.t),
credentials: Js.Nullable.t(string),
fetchOptions: Js.Nullable.t(Js.Json.t),
};

type fetchResult('raw_t) = {
errors: Js.Nullable.t(Js.Array.t(Types.graphqlError)),
data: Js.Nullable.t('raw_t),
extensions: Js.Json.t,
context: Js.Json.t,
};

type nextLink('raw_t, 'raw_t_variables) =
Types.operation('raw_t_variables) =>
Types.Observable.t(fetchResult('raw_t));

type makeCb('raw_t, 'raw_t_variables) =
(Types.operation('raw_t_variables), nextLink('raw_t, 'raw_t_variables)) =>
Types.Observable.t(fetchResult('raw_t));
external make: makeCb('raw_t, 'raw_t_variables) => t;

/* Bind the method `from`, used to compose links together */
[@bs.module "@apollo/client"] external from: array(t) => t = "from";

/* Bind the method split. Based on a test send left or right */
[@bs.module "@apollo/client"]
external split: (Types.splitTest => bool, t, t) => t = "split";

module RetryLink = {
// serverError is an Error instance with some extra props
// see: https://github.com/apollographql/apollo-client/blob/master/src/link/utils/throwServerError.ts
type serverError = {
statusCode: int,
response: Js.Json.t,
result: Js.Json.t,
};
type delay = {
initial: int,
max: int,
jitter: bool,
};
type attempts('raw_t_variables) = {
max: int,
retryIf:
(Js.Nullable.t(serverError), Types.operation('raw_t_variables)) => bool,
};
type options('raw_t_variables) = {
delay,
attempts: attempts('raw_t_variables),
};

[@bs.module "apollo-link-retry"] [@bs.new]
external make: options('raw_t_variables) => t = "RetryLink";
};

module HttpLink = {
[@bs.module "@apollo/client"] [@bs.new]
external jsMake: linkOptions => t = "HttpLink";
let make =
(
~uri,
~includeExtensions=?,
~fetch=?,
~headers=?,
~credentials=?,
~fetchOptions=?,
(),
) => {
jsMake({
uri,
includeExtensions: Js.Nullable.fromOption(includeExtensions),
fetch: Js.Nullable.fromOption(fetch),
headers: Js.Nullable.fromOption(headers),
credentials: Js.Nullable.fromOption(credentials),
fetchOptions: Js.Nullable.fromOption(fetchOptions),
});
};
};

module LinkContext = {
/* Bind the setContext method */
[@bs.module "apollo-link-context"]
external setContext: ([@bs.uncurry] ((request, context) => Js.Json.t)) => t =
"setContext";

[@bs.module "apollo-link-context"]
external setContextPromise:
((request, context) => Js.Promise.t(Js.Json.t)) => t =
"setContext";
};

module LinkError = {
/* Bind the onError method */
[@bs.module "apollo-link-error"]
external onError:
(Types.errorResponse('raw_t, 'raw_t_variables) => unit) => t =
"onError";
};

/* bind apollo-link-ws */
[@bs.module "apollo-link-ws"] [@bs.new]
external webSocketLink: Types.webSocketLink => t = "WebSocketLink";

// module UploadLink = {
// type uploadLinkOptions = {
// uri: Js.Nullable.t(string),
// fetch: Js.Nullable.t(fetch),
// fetchOptions: Js.Nullable.t(Js.t({.})),
// credentials: Js.Nullable.t(string),
// headers: Js.Nullable.t(Js.Json.t),
// includeExtensions: Js.Nullable.t(bool),
// };

// /* Bind createUploadLink function from apollo upload link */
// [@bs.module "apollo-upload-client"]
// external jsMake: uploadLinkOptions => t = "createUploadLink";
// let make =
// (
// ~uri=?,
// ~fetch=?,
// ~fetchOptions=?,
// ~credentials=?,
// ~headers=?,
// ~includeExtensions=?,
// (),
// ) =>
// jsMake(
// Js.Nullable.{
// uri: fromOption(uri),
// fetch: fromOption(fetch),
// fetchOptions: fromOption(fetchOptions),
// credentials: fromOption(credentials),
// headers: fromOption(headers),
// includeExtensions: fromOption(includeExtensions),
// },
// );
// };
Loading