open-index/open-npm huggingface dataset:
npm package: @hasura-ws/core
Description: Shared internal core for hasura-ws
Author: kigiri
Latest version: 1.1.0

# `@hasura-ws/core`

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

### Initialize the client

```jsx
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:
```jsx
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)`

```jsx
// 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)`

```jsx
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.
npm package: @hasura-ws/node
Description: Node driver for hasura-ws
Author: kigiri
Latest version: 1.1.0

# `@hasura-ws/node`

Wrapper for `@hasura-ws/core` on node-js

see [`@hasura-ws/core`](../core) for details on the client

> `@hasura-ws/node` use `ws` as a websocket client
npm package: @hasura-ws/prepare
Description: Query builder for hasura-ws
Author: kigiri
Latest version: 1.1.0

# `@hasura-ws/prepare`

Prepare a query, returning an async function to execute the query.

> Pre-stringify and share subscribes

## `initPrepare`

```js
import { initClient } from '@hasura-ws/browser'
import { initPrepare } from '@hasura-ws/prepare'

const client = initClient({
  address: 'ws://localhost:8080/v1alpha1/graphql', // (either ws:// or wss://)
  token: 'eyJhbGciOiJIUzI...w5c', // or a valid JWT token
})

const prepare = initPrepare(client) // pass the client to prepare
```

## `prepare`

```js
// simple query
const getUserEmail = prepare(`query {
  user {
    email
  }
}`)

const users = await getUserEmail()
console.log(users[0].email)

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

// by default the function return only the first ressource:
const users = await getUserEmail({ id: 1 })
console.log(users[0].email)

// you can get the untouched client result with `.all`:
const data = await getUserEmail.all({ id: 1 })
console.log(data.user[0].email)

// If your query is limited to 1 result, you can access it with `.one`:
const user = await getUserEmail.one({ id: 1 })
console.log(user.email)

// mutation with variables
const updateUser = prepare(`
mutation update_user($id: Int, $changes: user_set_input) {
  update_user(where: {id: {_eq: $id}}, _set: $changes) {
    affected_rows
  }
}`)

// by default it only returns the results of the first mutation,
// but like in queries you have `.all` if you do multiple mutations at once.
const result = await updateUser({ id: 1, changes: { email: 'jean@email.com' } })

console.log(result.affected_rows)

// subscription with variables
const userSubscribe = prepare(`
subscription subscribeToUserById($id: Int!) {
  user (where: {id: {_eq: $id}}) {
    email
  }
}`)

// a subscribe return the returned values of client.subscribe
const { execution, unsubscribe } = userSubscribe(
  user => console.log(user[0].email), // callback
  { id: 1 }, // variables
)

// like queries you can use `.one`, here, you get the first user directly
userSubscribe.one(({ email }) => console.log(email), { id: 1 })

// and you get `.all` for having the raw result from the client
userSubscribe.all(data => console.log(data.user[0].email), { id: 1 })

// you can also specify how you want prepare to map your data with `.map`

const myUserSubscribe = userSubscribe.map(data => data.user[0].email)
myUserSubscribe(email => console.log('my email', email))

// the mapper function always takes the raw results from the client.

```