@@ -2,7 +2,11 @@ import { useEffect, useState } from 'react'
22import { WebSocketConn } from 'starpc'
33import { useWebSocket } from './useWebSocket.js'
44import { 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'
610import { pushable } from 'it-pushable'
711
812import './App.css'
@@ -11,6 +15,15 @@ import { buildAppSchema } from './schema.js'
1115const serverAddr = 'ws://localhost:8093/demo.ws'
1216const schema = buildAppSchema ( )
1317
18+ const AppDemoQuery = `{
19+ counter
20+ names
21+ allPeople {
22+ name
23+ height
24+ }
25+ }`
26+
1427function 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