Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add config #14

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/Mutation.re
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,21 @@ module type Mutation = {
'b,
)
) =>
(((~variables: Yojson.Basic.t, unit) => unit, status), Hooks.t('a, 'b));
(
(
(~config: S.config=?, ~variables: Yojson.Basic.t, unit) => unit,
status,
),
Hooks.t('a, 'b),
);
};

module Make =
(C: BaseConfig, G: MutationConfig)
: (Mutation with type t = G.t) => {
let baseConfig = C.config;

type t = G.t;
let baseUrl = C.baseUrl;
let headers = C.headers;

type status =
| Idle
Expand Down Expand Up @@ -87,7 +93,9 @@ module Make =
},
);

let makeRequest = (~variables) => {
let makeRequest =
(~config: option(S.config)=?, ~variables: Yojson.Basic.t) => {
let mergedConfig = Option.value(config, ~default=baseConfig);
let requestBody =
`Assoc([("query", `String(G.query)), ("variables", variables)])
|> Yojson.Basic.to_string;
Expand All @@ -97,8 +105,11 @@ module Make =
Fetch.(
post(
~body=requestBody,
~headers=[("Content-Type", "application/json"), ...headers],
baseUrl,
~headers=[
("Content-Type", "application/json"),
...mergedConfig.headers,
],
mergedConfig.baseUrl,
)
|> Lwt.map(
fun
Expand All @@ -118,10 +129,10 @@ module Make =
|> ignore;
};

let mutation = (~variables, ()) => {
let mutation = (~config: option(S.config)=?, ~variables, ()) => {
setVariables(_prevVariables => variables);

makeRequest(~variables);
makeRequest(~config?, ~variables);
};

(mutation, state);
Expand Down
8 changes: 7 additions & 1 deletion src/Mutation.rei
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ module type Mutation = {
'b,
)
) =>
(((~variables: Yojson.Basic.t, unit) => unit, status), Hooks.t('a, 'b));
(
(
(~config: S.config=?, ~variables: Yojson.Basic.t, unit) => unit,
status,
),
Hooks.t('a, 'b),
);
};

module Make:
Expand Down
153 changes: 153 additions & 0 deletions src/NewQuery.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
open Revery.UI.React;

type status('a) =
| Idle
| Loading
| Error
| Data('a);

type action('a) =
| Fetch
| Error
| Data('a);

let stringOfAction =
fun
| Fetch => "Fetch"
| Error => "Error"
| Data(_) => "Data";

let reducer = (action, _state): status('a) => {
Printf.printf("dispatched: %s\n%!", stringOfAction(action));
switch (action) {
| Fetch => Loading
| Error => Error
| Data(obj) => Data(obj)
};
};

let initialState: status('a) = Idle;

let use =
(
~definition,
~config: S.config,
~variables: option(Yojson.Basic.t)=?,
(),
) => {
let (parse, query, composeVariables) = definition;
let%hook (state, dispatch) = Hooks.reducer(~initialState, reducer);
let mergedConfig = config;

let query = () => {
let variables =
switch (variables) {
| Some(variables) => variables
| None => `Assoc([])
};

let query =
`Assoc([("query", `String(query)), ("variables", variables)])
|> Yojson.Basic.to_string;

(query, variables);
};

let tap = (~l=?, ~f=ignore, x) => {
Option.iter(l => Printf.printf("%s: ", l), l);
f(x);
Printf.printf("\n%!");
x;
};

let subscribeToStore = query =>
Store.subscribe(
~query,
graphqlJson => {
let data =
try(
graphqlJson
|> tap(~l="graphqlJson", ~f=ignore)
|> Yojson.Basic.from_string
|> tap(~l="fromString", ~f=ignore)
|> Yojson.Basic.Util.member("data")
|> tap(~l="member", ~f=ignore)
|> tap(~l="data", ~f=data =>
Printf.printf("%s", Yojson.Basic.to_string(data))
)
|> parse
) {
| firstException =>
Printf.printf("This Crashed\n%!");
raise(Not_found);
};

dispatch(Data(data));
},
);

let executeRequest = (query, variables) => {
dispatch(Fetch);

Printf.printf("Fetching\n%!");
Fetch.(
post(
~body=query,
~headers=[
("Content-Type", "application/json"),
...mergedConfig.headers,
],
mergedConfig.baseUrl,
)
|> Lwt.map(result => {
Printf.printf("Got result\n%!");

switch (result) {
| Ok({Response.body, _}) =>
let query =
`Assoc([
("query", `String(query)),
("variables", variables),
])
|> Yojson.Basic.to_string;

Printf.printf("%s\n%!", Body.toString(body));
Store.publish(~query, Body.toString(body));
| _ => dispatch(Error)
};
})
)
|> ignore;
};

/* TODO: use OnMountAndIf when Revery has been updated to latest brisk */
let%hook () =
Hooks.effect(
OnMount,
() => {
let (query, variables) = query();
let unsubscribe = subscribeToStore(query);

executeRequest(query, variables);

Some(unsubscribe);
},
);

let%hook () =
Hooks.effect(
If(
(prevVariables, nextVariables) => prevVariables != nextVariables,
variables,
),
() => {
let (query, variables) = query();
let unsubscribe = subscribeToStore(query);
executeRequest(query, variables);

Some(unsubscribe);
},
);

state;
};
88 changes: 64 additions & 24 deletions src/Query.re
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module type Query = {

let use:
(
~config: S.config=?,
~variables: Yojson.Basic.t=?,
unit,
Hooks.t(
Expand All @@ -30,8 +31,7 @@ module type Query = {

module Make = (C: BaseConfig, G: QueryConfig) : (Query with type t = G.t) => {
type t = G.t;
let baseUrl = C.baseUrl;
let headers = C.headers;
let baseConfig = C.config;

type status =
| Idle
Expand All @@ -44,17 +44,31 @@ module Make = (C: BaseConfig, G: QueryConfig) : (Query with type t = G.t) => {
| Error
| Data(G.t);

let reducer = (action, _state): status =>
let stringOfAction =
fun
| Fetch => "Fetch"
| Error => "Error"
| Data(_) => "Data";

let reducer = (action, _state): status => {
Printf.printf("dispatched: %s\n%!", stringOfAction(action));
switch (action) {
| Fetch => Loading
| Error => Error
| Data(obj) => Data(obj)
};
};

let initialState: status = Idle;

let use = (~variables: option(Yojson.Basic.t)=?, ()) => {
let use =
(
~config: option(S.config)=?,
~variables: option(Yojson.Basic.t)=?,
(),
) => {
let%hook (state, dispatch) = Hooks.reducer(~initialState, reducer);
let mergedConfig = Option.value(config, ~default=baseConfig);

let query = () => {
let variables =
Expand All @@ -70,15 +84,34 @@ module Make = (C: BaseConfig, G: QueryConfig) : (Query with type t = G.t) => {
(query, variables);
};

let tap = (~l=?, ~f=ignore, x) => {
Option.iter(l => Printf.printf("%s: ", l), l);
f(x);
Printf.printf("\n%!");
x;
};

let subscribeToStore = query =>
Store.subscribe(
~query,
graphqlJson => {
let data =
graphqlJson
|> Yojson.Basic.from_string
|> Yojson.Basic.Util.member("data")
|> G.parse;
try(
graphqlJson
|> tap(~l="graphqlJson", ~f=ignore)
|> Yojson.Basic.from_string
|> tap(~l="fromString", ~f=ignore)
|> Yojson.Basic.Util.member("data")
|> tap(~l="member", ~f=ignore)
|> tap(~l="data", ~f=data =>
Printf.printf("%s", Yojson.Basic.to_string(data))
)
|> G.parse
) {
| firstException =>
Printf.printf("This Crashed\n%!");
raise(Not_found);
};

dispatch(Data(data));
},
Expand All @@ -87,26 +120,33 @@ module Make = (C: BaseConfig, G: QueryConfig) : (Query with type t = G.t) => {
let executeRequest = (query, variables) => {
dispatch(Fetch);

Printf.printf("Fetching\n%!");
Fetch.(
post(
~body=query,
~headers=[("Content-Type", "application/json"), ...headers],
baseUrl,
~headers=[
("Content-Type", "application/json"),
...mergedConfig.headers,
],
mergedConfig.baseUrl,
)
|> Lwt.map(
fun
| Ok({Response.body, _}) => {
let query =
`Assoc([
("query", `String(G.query)),
("variables", variables),
])
|> Yojson.Basic.to_string;

Store.publish(~query, Body.toString(body));
}
| _ => dispatch(Error),
)
|> Lwt.map(result => {
Printf.printf("Got result\n%!");

switch (result) {
| Ok({Response.body, _}) =>
let query =
`Assoc([
("query", `String(G.query)),
("variables", variables),
])
|> Yojson.Basic.to_string;

Printf.printf("%s\n%!", Body.toString(body));
Store.publish(~query, Body.toString(body));
| _ => dispatch(Error)
};
})
)
|> ignore;
};
Expand Down
1 change: 1 addition & 0 deletions src/Query.rei
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module type Query = {

let use:
(
~config: S.config=?,
~variables: Yojson.Basic.t=?,
unit,
Hooks.t(
Expand Down
2 changes: 2 additions & 0 deletions src/ReveryGraphqlHooks.re
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ module Make = (C: BaseConfig) => {
module Mutation = {
module Make = Mutation.Make(C);
};

let newQuery = NewQuery.use;
};
Loading