Link Search Menu Expand Document

Build Realtime GraphQL Rails Apps

HasuraHandler is a Rails framework that makes building microservices for Hasura easy. HasuraHandler also simplifies adding Hasura to an existing Rails app.

Learn about Hasura Get Started


Gem Version tests Maintainability Test Coverage

Summary

HasuraHandler is a Rails engine, by default it will add 2 routes where it is mounted; one for actions, one for events. Actions allow you to return information to a GraphQL query/mutation, such as verifying a usename/password and generating a login token. Events allow you to receive information and react to it accordingly, such as sending an updated record to a search backend/provider for indexing.

What can Hasura do?

When using Hasura as part of a Rails app, Hasura can act as several pieces of infrastructure:

  • A GraphQL API gateway that supports subscriptions over websockets.
  • An internal event-based webhook service that delivers records to endpoints when they’re insertted/updated/deleted.
  • As a data access layer where services use it as a master data store.
  • An internal webhook scheduling service that can deliver one-off or recurring events to endpoints.

Since Hasura can be used in so many ways, it can be added to an application stack incrementally for existing Rails apps. For new Rails apps, considering ways to use this functionality can radically alter the way you think about building and architecting your app.

Quick Start

  1. Add the gem to your project
    gem 'hasura_handler', '~> 0.1.0'
    
  2. Create config/initializers/hasura_handler.rb:
    HasuraHandler.setup do |config|
      config.auth_key = ENV['HASURA_SERVICE_KEY'] || Rails.application.credentials.hasura_service_key
    end
    
  3. Create the following directories:
    • app/actions
    • app/reactions
  4. Add routes in config/routes.rb:
    mount HasuraHandler::Engine => '/hasura'
    
  5. Create event trigger in Hasura: TODO

  6. Create event reaction in app/reactions/welcome_user.rb:
     class WelcomeUser < HasuraHandler::EventHandler
       match_by trigger: 'user_inserted'
    
       def run
         Rails.logger.info('A new user signed up!')
       end
     end