Home

Awesome

fastify-hasura.png

fastify-hasura

NPM version Coverage Status

A Fastify plugin to have fun with Hasura.

Features

Install

  1. Install fastify-hasura with:
yarn add fastify-hasura  # or npm i --save fastify-hasura
  1. Register the plugin:
fastify.register(require('fastify-hasura'), {
  endpoint: 'yourHasuraGraphqlEndpoint',
  admin_secret: 'yourAdminSecret'
})

Usage

Example request on Hasura Graphql Endpoint

const userId = 'yourUserUUID'

const fetchUser = `#graphql
  query fetchUser($id: uuid!) {
    user: user_by_pk(id: $id) {
      password
    }
  }
`
const { user } = await fastify.hasura.graphql(fetchUser, {
  id: userId
})

Example registering event and action:

// Register new_user hasura event
fastify.hasura.registerEvent('new_user', (request, reply) => {
  const user = request.event.getNewData()
  console.log(user)
})

// Register login hasura action
fastify.hasura.registerAction('login', async (request, reply) => {
  const data = request.action.getData('id', 'type', 'user')
  console.log(data)

  const response = await yourAsyncCustomBusinessLogic(data)
  reply.send(response)
})

Note: Requests for events and actions are decorated with hasura-parser. So, you can easily retrieve data in routes with request.event and request.action.

Options

All options:

fastify.register(require('fastify-hasura'), {
  endpoint: 'yourHasuraGraphqlEndpoint',
  admin_secret: 'yourAdminSecret',
  api_secret: 'yourApiSecret',
  routes_prefix: '/hasura'
})

More documentation

Contributions

If you would like to make any contribution you are welcome to do so.

License

Licensed under MIT