-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
78 lines (67 loc) · 1.58 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import React, { useState } from "react";
import { useQuery, useMutation, useSubscription } from "graphql-hooks";
import "./App.css";
const RESULT = `query GetResult {
result
}`;
const ADD = `mutation AddValue($num: Int) {
add(num: $num)
}`;
const SUBTRACT = `mutation SubtractValue($num: Int) {
subtract(num: $num)
}`;
const ON_RESULT_CHANGE = `subscription OnResultChange {
onResultChange {
operation
prev
current
}
}`;
export default function App() {
const { isLoading, data, refetch } = useQuery(RESULT);
const [addMutation] = useMutation(ADD);
const [subtractMutation] = useMutation(SUBTRACT);
const [resultState, setResultState] = useState({
operation: "",
prev: "",
current: "",
});
useSubscription(
{
query: ON_RESULT_CHANGE,
},
({ data: { onResultChange }, errors }) => {
if (errors && errors.length > 0) {
console.log(errors[0]);
}
if (onResultChange) {
setResultState(onResultChange);
refetch();
}
}
);
return (
<div className="app">
<div className="count">
<h1>{isLoading ? "Loading…" : data?.result}</h1>
</div>
<div className="buttons">
<button
onClick={() => {
return subtractMutation({ variables: { num: 1 } });
}}
>
-
</button>
<button
onClick={() => {
return addMutation({ variables: { num: 1 } });
}}
>
+
</button>
</div>
<pre>{JSON.stringify(resultState, null, 2)}</pre>
</div>
);
}