Skip to content

Commit

Permalink
UrqlUseSubscription (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiraarghy authored and parkerziegler committed Jun 20, 2019
1 parent 98f0200 commit ba3007b
Show file tree
Hide file tree
Showing 21 changed files with 343 additions and 419 deletions.
4 changes: 4 additions & 0 deletions __tests__/__snapshots__/UrqlClient_test.bs.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Client {
"operations$": [Function],
"reexecuteOperation": [Function],
"results$": [Function],
"suspense": false,
"url": "https://localhost:3000",
}
`;
Expand All @@ -42,6 +43,7 @@ Client {
"operations$": [Function],
"reexecuteOperation": [Function],
"results$": [Function],
"suspense": false,
"url": "https://localhost:3000",
}
`;
Expand All @@ -67,6 +69,7 @@ Client {
"operations$": [Function],
"reexecuteOperation": [Function],
"results$": [Function],
"suspense": false,
"url": "https://localhost:3000",
}
`;
Expand All @@ -88,6 +91,7 @@ Client {
"operations$": [Function],
"reexecuteOperation": [Function],
"results$": [Function],
"suspense": false,
"url": "https://localhost:3000",
}
`;
5 changes: 3 additions & 2 deletions examples/5-subscription/bsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "5-subscription",
"reason": {
"react-jsx": 2
"react-jsx": 3
},
"bsc-flags": ["-bs-super-errors"],
"sources": [
{
"dir": "src/app",
Expand All @@ -12,7 +13,7 @@
],
"package-specs": [
{
"module": "commonjs",
"module": "es6",
"in-source": true
}
],
Expand Down
5 changes: 3 additions & 2 deletions examples/5-subscription/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@
"scripts": {
"start": "bsb -make-world -w",
"clean": "bsb -clean-world",
"build": "bsb -make-world",
"webpack": "webpack-dev-server --hot",
"server": "node src/server/index.js",
"start:demo": "run-p server webpack"
},
"dependencies": {
"bs-css": "^8.0.2",
"reason-react": ">=0.3.4",
"reason-react": "0.7.0",
"reason-urql": "link:../../"
},
"devDependencies": {
"apollo-server-express": "^2.4.8",
"bs-platform": "^5.0.3",
"bs-platform": "^5.0.4",
"cors": "^2.8.5",
"express": "^4.16.4",
"faker": "^4.1.0",
Expand Down
8 changes: 0 additions & 8 deletions examples/5-subscription/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@
margin: 0;
}
</style>
<link
href="https://fonts.googleapis.com/css?family=PT+Serif+Caption"
rel="stylesheet"
/>
<link
href="https://fonts.googleapis.com/css?family=Marck+Script"
rel="stylesheet"
/>
</head>
<body>
<div id="root"></div>
Expand Down
46 changes: 11 additions & 35 deletions examples/5-subscription/src/app/App.re
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
let component = ReasonReact.statelessComponent("App");

module Styles = {
open Css;

let page =
style([
display(flexBox),
flexDirection(column),
position(absolute),
top(px(0)),
bottom(px(0)),
left(px(0)),
right(px(0)),
padding(px(20)),
]);

let side =
style([
display(flexBox),
flexDirection(column),
alignItems(center),
flexBasis(pct(50.)),
firstChild([justifyContent(flexEnd)]),
]);
};

let make = _children => {
...component,
render: _self =>
<div className=Styles.page>
<section className=Styles.side> <Logo /> </section>
<section className=Styles.side> <Messages /> </section>
</div>,
};
[@react.component]
let make = () =>
<div className=AppStyles.page>
<svg
viewBox="0 0 1000 1000"
height="1000"
width="1000"
style={ReactDOMRe.Style.make(~border="1px solid blue", ())}>
<Circles />
</svg>
</div>;
15 changes: 15 additions & 0 deletions examples/5-subscription/src/app/AppStyles.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
open Css;

let page =
style([
display(flexBox),
flexDirection(column),
alignItems(center),
justifyContent(center),
position(absolute),
top(px(0)),
bottom(px(0)),
left(px(0)),
right(px(0)),
padding(px(20)),
]);
58 changes: 58 additions & 0 deletions examples/5-subscription/src/app/Circles.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
open ReasonUrql;
open Hooks;

module SubscribeRandomInt = [%graphql
{|
subscription subscribeNumbers {
newNumber @bsDecoder(fn: "string_of_int")
}
|}
];

let handler = (~prevSubscriptions, ~subscription) => {
switch (prevSubscriptions) {
| Some(subs) => Array.append(subs, [|subscription|])
| None => [|subscription|]
};
};

[@bs.scope "Math"] [@bs.val] external random: unit => float = "random";
[@bs.scope "Math"] [@bs.val] external floor: float => int = "floor";
[@bs.send] external toString: (int, int) => string = "toString";

let getRandomInt = (max: int) => {
floor(random() *. float_of_int(max));
};

let getRandomHex = () => {
let encode = random() *. float_of_int(16777215) |> floor;
let hex = encode->toString(16);
{j|#$hex|j};
};

[@react.component]
let make = () => {
let {response} =
useSubscriptionWithHandler(~request=SubscribeRandomInt.make(), ~handler);

switch (response) {
| Fetching => <text> "Loading"->React.string </text>
| Data(d) =>
Array.mapi(
(index, datum) =>
<circle
cx={
datum##newNumber;
}
cy={index === 0 ? datum##newNumber : d[index - 1]##newNumber}
stroke={getRandomHex()}
fill="none"
r={getRandomInt(30) |> string_of_int}
/>,
d,
)
|> React.array
| Error(_e) => <text> "Error"->React.string </text>
| NotFound => <text> "Not Found"->React.string </text>
};
};
61 changes: 0 additions & 61 deletions examples/5-subscription/src/app/Logo.re

This file was deleted.

83 changes: 0 additions & 83 deletions examples/5-subscription/src/app/Messages.re

This file was deleted.

6 changes: 3 additions & 3 deletions examples/5-subscription/src/server/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}

setInterval(() =>
setInterval(() => {
pubsub.publish("newNumber", {
newNumber: getRandomInt(1000)
})
);
});
}, 2000);
3 changes: 3 additions & 0 deletions examples/5-subscription/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module.exports = {
publicPath: "/public/",
filename: "index.js"
},
resolve: {
modules: [path.resolve(__dirname, "../../node_modules"), "node_modules"]
},
devServer: {
open: true,
contentBase: path.resolve(__dirname, "public")
Expand Down
Loading

0 comments on commit ba3007b

Please sign in to comment.