JSPM

  • Created
  • Published
  • Downloads 183
  • Score
    100M100P100Q77747F
  • License MIT

Shared internal core for hasura-ws

Package Exports

  • @hasura-ws/core
  • @hasura-ws/core/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@hasura-ws/core) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@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.