@hasura-ws/core

1.1.0 • Public • Published

@hasura-ws/core

@hasura-ws/browser or @hasura-ws/node (for nodejs only!)

Initialize the client

import { initClient } from '@hasura-ws/browser'

// the client can take 2 parameters
// the required websocket address
// and a jwt token (or the admin secret)
const client = initClient({
  address: 'ws://localhost:8080/v1alpha1/graphql', // (either ws:// or wss://)
  debug: false, // log additional information for sent and recieved messages

  // Crendentials :
  adminSecret: '9311f0d7b5caaa183', // your hasura secret
  token: 'eyJhbGciOiJIUzI...w5c', // or a valid JWT token
})

You can also delay the connection by passing the crendentials later on:

const client = initClient({ address: 'ws://localhost:8080/v1alpha1/graphql' })

// later once you get the user token:
client.connect({ token: 'eyJhbGciOiJIUzI...w5c' })

client.run

This method allow you to run queries and mutations.

Run takes 2 arguments:

  • the query (string)
  • optional variables (object)
// simple usage
const data = await client.run(`query {
  user {
    email
  }
}`)

console.log(data.result.user[0].email)

// with variables
const getUserByIdQuery = `query getUserById($id: Int!) {
  user (where: {id: {_eq: $id}}) {
    email
  }
}`

const data = await client.run(getUserByIdQuery, { id: 1 })
console.log(data.result.user[0].email)

client.subscribe

This method allow you to start subscriptions.

Run takes 3 arguments:

  • the listener callback (function)
  • the subscription query (string)
  • optional variables (object)
const subscribeToUserByIdQuery = `subscription subscribeToUserById($id: Int!) {
  user (where: {id: {_eq: $id}}) {
    email
  }
}`

const { execution, unsubscribe } = client.subscribe(
  data => console.log(data.user[0].email),
  subscribeToUserByIdQuery,
  { id: 1 },
)

// execution is the promise of the subscribe,
//   it resolve when the query is completed.

// unsubscribe is a function to call to stop subscribing

client.ws

This is the internal websocket client.

client.connection

This is a promise of the pending connection, if you want to handle reconnect.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.1.074latest

Version History

VersionDownloads (Last 7 Days)Published
1.1.074
1.0.1-alpha.00
1.0.0-alpha.00
0.5.129
0.5.00
0.4.40
0.4.30
0.4.00
0.3.00
0.2.20
0.2.10
0.2.00
0.1.180
0.1.160
0.1.150
0.1.140
0.1.130
0.1.120
0.1.110
0.1.100
0.1.90
0.1.50
0.1.30
0.1.10
0.1.00
0.0.110
0.0.100
0.0.90
0.0.80
0.0.70
0.0.60
0.0.51
0.0.40
0.0.30
0.0.20

Package Sidebar

Install

npm i @hasura-ws/core

Weekly Downloads

104

Version

1.1.0

License

MIT

Unpacked Size

8.26 kB

Total Files

3

Last publish

Collaborators

  • kigiri
  • lfoussat