/hasura-ws

Minimal javascript graphql websocket client for hasura

Primary LanguageJavaScript

@hasura-ws

Minimal javascript graphql websocket client for hasura

Packages

hasura-ws suite is composed of 4 blocks:

The WebSocket hasura client

Pre-compile queries and add usefull methods to reduce graphql boilerplate

Generate boilerplate crud queries for your data model

React bindings

Example

import { initClient } from '@hasura-ws/browser'
import { initAll, isPending, hasError } from '@hasura-ws/hooks'

const { client, model, prepare } = initAll(initClient({
  address: 'ws://localhost:8080/v1alpha1/graphql',
  debug: true, // show in the console detailed logs (verbose filter must be on)
}))

getJWTToken() // Assuming you have a way to get your token
  .then(token => client.connect({ token, role: 'user' }))

const userModel = model('user')(`
  email
`)

const MySubscribeComponent = ({ id }) => {
  const user = userModel.useSubscribe(id)
  if (isPending(user)) return 'Loading...'
  if (hasError(user)) return 'Oops !'
  if (!user) return 'User not found !'

  return <div>{user.email}</div>
}