You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{readFileSync}from'fs'import{dirname,join}from'path'import{PassThrough}from'stream'import{fileURLToPath}from'url'importServerfrom'@hydre/shimio/server'importServefrom'@hydre/shimio-graphql/serve'importgraphqlfrom'graphql'importKoafrom'koa'constdirectory=dirname(fileURLToPath(import.meta.url))constWAIT=150constfile=readFileSync(join(directory,'schema.gql'),'utf-8')constserver=Server({koa: newKoa(),// context here is an empty object created by Shimio serveron_upgrade: ({ context })=>{context.through=newPassThrough({objectMode: true})returntrue},on_socket: Serve({context: async({// the raw upgraded socket
socket,// the raw connection context (in allow_upgrade)
context,// the raw first http request
request
})=>({// put whatever you want here// this is to build a per-operation context// each batch of queries will have a clean context}),schema: graphql.buildSchema(file),query : {me(){return{name: 'pepeg'}},ping(){return'ping pong chin chan'},},mutation: {sendMessage({ message },{ through }){through.write({onMessage: message})return'message sent!'},},subscription: {async*onMessage(_,{ through }){forawait(constchunkofthrough){awaitnewPromise(resolve=>setTimeout(resolve,WAIT))yieldchunk}},},}),})awaitserver.listen(3000)console.log('running on :3000')
Client example
importdebugfrom'debug'importcasualfrom'casual'import{inspect}from'util'importClientfrom'@hydre/shimio/client'importQueryfrom'@hydre/shimio-graphql/query'// || ===========================================// || When running in nodejs you need to provide a ws polyfill// || Browsers have it by default// ||importwsfrom'ws'globalThis.WebSocket=ws// ||// || ===========================================// see options in @hydre/shimioconstclient=Client({host: 'ws://0.0.0.0:3000'})constquery=Query(client)constEND=2000awaitclient.connect()const{
listen,// an asyncIterator yielding updates
stop,// close the channel
once // promise of the first result only}=awaitquery(/* GraphQL */` query pang { ping } mutation hello { first: sendMessage(message: "howdy") then: sendMessage(message: "pls sir show vagana") } subscription hey_listen { onMessage }`)setTimeout(()=>{stop()// unsubscribe from operation},END)forawait(constchunkoflisten())console.log('received',inspect(chunk,false,Infinity,true))client.disconnect()