Skip to content

Commit

Permalink
Add common hook type (Fix #80) (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
gugahoa authored and parkerziegler committed Jun 28, 2019
1 parent e2c4be2 commit d434fbd
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 59 deletions.
5 changes: 3 additions & 2 deletions examples/2-query/src/Monster.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
open ReasonUrql;
open Hooks;
open MonsterStyles;

type maximum = {maximum: int};
Expand Down Expand Up @@ -34,9 +35,9 @@ let make = (~pokemon: string) => {
/* We set up the query here as we need access to the pokemon
value passed in from GetAll */
let request = GetPokemon.make(~name=pokemon, ());
let (result, _) = Hooks.useQuery(~request, ());
let ({response}, _) = useQuery(~request, ());

switch (result.response) {
switch (response) {
| Data(data) =>
switch (data##pokemon) {
| Some(pokemon) =>
Expand Down
6 changes: 6 additions & 0 deletions src/ReasonUrql.re
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ module Error = UrqlCombinedError;
module Exchanges = UrqlClient.UrqlExchanges;

module Hooks = {
type hookResponse('ret) = Types.hookResponse('ret) = {
fetching: bool,
data: option('ret),
error: option(UrqlCombinedError.t),
response: Types.response('ret)
};
include UrqlUseMutation;
include UrqlUseQuery;
include UrqlUseSubscription;
Expand Down
7 changes: 7 additions & 0 deletions src/UrqlTypes.re
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,10 @@ type handler('acc) =

type parsedHandler('acc, 'response) =
(~prevSubscriptions: option('acc), ~subscription: 'response) => 'acc;

type hookResponse('ret) = {
fetching: bool,
data: option('ret),
error: option(UrqlCombinedError.t),
response: response('ret)
}
15 changes: 5 additions & 10 deletions src/hooks/UrqlUseMutation.re
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
open UrqlTypes;

[@bs.deriving abstract]
type useMutationResponseJs = {
fetching: bool,
Expand All @@ -7,15 +9,8 @@ type useMutationResponseJs = {
error: UrqlCombinedError.t,
};

type useMutationResponse('response) = {
fetching: bool,
data: option('response),
error: option(UrqlCombinedError.t),
response: UrqlTypes.response('response),
};

type executeMutation =
option(Js.Json.t) => Js.Promise.t(UrqlTypes.operationResult);
option(Js.Json.t) => Js.Promise.t(operationResult);

[@bs.module "urql"]
external useMutationJs: string => (useMutationResponseJs, executeMutation) =
Expand All @@ -29,7 +24,7 @@ let useMutationResponseToRecord =

let response =
switch (fetching, data, error) {
| (true, _, _) => UrqlTypes.Fetching
| (true, _, _) => Fetching
| (false, Some(data), _) => Data(data)
| (false, _, Some(error)) => Error(error)
| (false, None, None) => NotFound
Expand All @@ -38,7 +33,7 @@ let useMutationResponseToRecord =
{fetching, data, error, response};
};

let useMutation = (~request: UrqlTypes.request('response)) => {
let useMutation = (~request: request('response)) => {
let (useMutationResponseJs, executeMutation) =
useMutationJs(request##query);
let useMutationResponse =
Expand Down
11 changes: 3 additions & 8 deletions src/hooks/UrqlUseMutation.rei
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
type useMutationResponse('response) = {
fetching: bool,
data: option('response),
error: option(UrqlCombinedError.t),
response: UrqlTypes.response('response),
};
open UrqlTypes;

let useMutation:
(~request: UrqlTypes.request('response)) =>
(~request: request('response)) =>
(
useMutationResponse('response),
hookResponse('response),
unit => Js.Promise.t(UrqlTypes.operationResult),
);
16 changes: 6 additions & 10 deletions src/hooks/UrqlUseQuery.re
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
open UrqlTypes;

[@bs.deriving abstract]
type useQueryStateJs = {
fetching: bool,
Expand All @@ -12,23 +14,17 @@ type useQueryArgs = {
query: string,
variables: Js.Json.t,
[@bs.optional]
requestPolicy: UrqlTypes.requestPolicy,
requestPolicy: requestPolicy,
[@bs.optional]
pause: bool,
};

type partialOperationContextFn =
option(UrqlTypes.partialOperationContext) => unit;
option(partialOperationContext) => unit;
type useQueryResponseJs = (useQueryStateJs, partialOperationContextFn);

type useQueryState('response) = {
fetching: bool,
data: option('response),
error: option(UrqlCombinedError.t),
response: UrqlTypes.response('response),
};
type useQueryResponse('response) = (
useQueryState('response),
hookResponse('response),
partialOperationContextFn,
);

Expand All @@ -39,7 +35,7 @@ let useQueryResponseToRecord = (parse, result) => {

let response =
switch (fetching, data, error) {
| (true, _, _) => UrqlTypes.Fetching
| (true, _, _) => Fetching
| (false, Some(data), _) => Data(data)
| (false, _, Some(error)) => Error(error)
| (false, None, None) => NotFound
Expand Down
15 changes: 5 additions & 10 deletions src/hooks/UrqlUseQuery.rei
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
type partialOperationContextFn =
option(UrqlTypes.partialOperationContext) => unit;
open UrqlTypes;

type useQueryState('response) = {
fetching: bool,
data: option('response),
error: option(UrqlCombinedError.t),
response: UrqlTypes.response('response),
};
type partialOperationContextFn =
option(partialOperationContext) => unit;

type useQueryResponse('response) = (
useQueryState('response),
hookResponse('response),
partialOperationContextFn,
);

Expand All @@ -21,7 +16,7 @@ let useQuery:
"query": string,
"variables": Js.Json.t,
},
~requestPolicy: UrqlTypes.requestPolicy=?,
~requestPolicy: requestPolicy=?,
~pause: bool=?,
unit
) =>
Expand Down
17 changes: 6 additions & 11 deletions src/hooks/UrqlUseSubscription.re
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
open UrqlTypes;

type handler('acc, 'resp, 'ret) =
| Handler((option('acc), 'resp) => 'acc): handler('acc, 'resp, 'acc)
| NoHandler: handler(_, 'resp, 'resp);
Expand All @@ -18,13 +20,6 @@ type useSubscriptionResponseJs('ret) = {
error: UrqlCombinedError.t,
};

type useSubscriptionResponse('ret) = {
fetching: bool,
data: option('ret),
error: option(UrqlCombinedError.t),
response: UrqlTypes.response('ret),
};

[@bs.module "urql"]
external useSubscriptionJs:
(useSubscriptionArgs, option((option('acc), Js.Json.t) => 'acc)) =>
Expand All @@ -38,7 +33,7 @@ let useSubscriptionResponseToRecord = (parse, result) => {

let response =
switch (fetching, data, error) {
| (true, None, _) => UrqlTypes.Fetching
| (true, None, _) => Fetching
| (true, Some(data), _) => Data(data)
| (false, Some(data), _) => Data(data)
| (false, _, Some(error)) => Error(error)
Expand All @@ -54,9 +49,9 @@ let useSubscription =
type resp,
type ret,
~handler: handler(acc, resp, ret),
~request: UrqlTypes.request(resp),
~request: request(resp),
)
: useSubscriptionResponse(ret) => {
: hookResponse(ret) => {
let parse = request##parse;
let args =
useSubscriptionArgs(
Expand All @@ -65,7 +60,7 @@ let useSubscription =
(),
);

let state: useSubscriptionResponse(ret) =
let state: hookResponse(ret) =
switch (handler) {
| NoHandler =>
useSubscriptionJs(args, None)[0]
Expand Down
11 changes: 3 additions & 8 deletions src/hooks/UrqlUseSubscription.rei
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
open UrqlTypes;

type handler('acc, 'resp, 'ret) =
| Handler((option('acc), 'resp) => 'acc): handler('acc, 'resp, 'acc)
| NoHandler: handler(_, 'resp, 'resp);

type useSubscriptionResponse('ret) = {
fetching: bool,
data: option('ret),
error: option(UrqlCombinedError.t),
response: UrqlTypes.response('ret),
};

let useSubscription:
(
~handler: handler('acc, 'resp, 'ret),
~request: UrqlTypes.request('resp)
) =>
useSubscriptionResponse('ret);
hookResponse('ret);

0 comments on commit d434fbd

Please sign in to comment.