Skip to content

Commit b16737e

Browse files
committed
fix: auto-update query result
Signed-off-by: Christian Stewart <[email protected]>
1 parent 5b9074d commit b16737e

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/App.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@
88
.card {
99
padding: 2em;
1010
}
11+
12+
.result {
13+
text-align: left;
14+
padding-left: 2rem;
15+
}

src/App.tsx

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { useEffect, useState } from 'react'
22
import { WebSocketConn } from 'starpc'
33
import { useWebSocket } from './useWebSocket.js'
44
import { RgraphqlDemoClient } from '../app/service/service_srpc.pb.js'
5-
import { RGQLClientMessage, Client as RGraphQLClient } from 'rgraphql'
5+
import {
6+
JSONDecoder,
7+
RGQLClientMessage,
8+
Client as RGraphQLClient,
9+
} from 'rgraphql'
610
import { pushable } from 'it-pushable'
711

812
import './App.css'
@@ -11,6 +15,15 @@ import { buildAppSchema } from './schema.js'
1115
const serverAddr = 'ws://localhost:8093/demo.ws'
1216
const schema = buildAppSchema()
1317

18+
const AppDemoQuery = `{
19+
counter
20+
names
21+
allPeople {
22+
name
23+
height
24+
}
25+
}`
26+
1427
function App() {
1528
const [count, setCount] = useState(0)
1629
const { ws, getStatusMessage } = useWebSocket(serverAddr)
@@ -66,6 +79,30 @@ function App() {
6679
}
6780
}, [ws])
6881

82+
// rpc
83+
const [displayResult, setDisplayResult] = useState<string | undefined>(
84+
undefined,
85+
)
86+
useEffect(() => {
87+
if (!rgqlClient) return
88+
const query = rgqlClient.parseQuery(AppDemoQuery)
89+
const handler = new JSONDecoder(
90+
rgqlClient.getQueryTree().getRoot(),
91+
query.getQuery(),
92+
(val: unknown) => {
93+
if (val) {
94+
console.log({ val })
95+
setDisplayResult(JSON.stringify(val, null, '\t'))
96+
}
97+
},
98+
)
99+
query.attachHandler(handler)
100+
return () => {
101+
query.detachHandler(handler)
102+
query.dispose()
103+
}
104+
}, [rgqlClient])
105+
69106
if (!ws) {
70107
return (
71108
<div>
@@ -88,6 +125,9 @@ function App() {
88125
<>
89126
<h1>Vite + React + starpc</h1>
90127
<h3>Connected to {serverAddr}</h3>
128+
<div className="result">
129+
<pre>result: {displayResult}</pre>
130+
</div>
91131
<div className="card">
92132
<button onClick={() => setCount((count) => count + 1)}>
93133
count is {count}

0 commit comments

Comments
 (0)