Adding custom business logic with Hasura
Hasura provides instant realtime GraphQL on Postgres along with an authorization engine. This makes it easy to setup a blazing-fast GraphQL API that is secure & scalable, including GraphQL subscriptions that work and scale out of the box!
How would you add custom business logic though?
This blog post covers the different ways of customising and extending Hasura so that you can add your own business logic easily, with your favourite tools & frameworks.
You will notice that Hasura encourages opinions that allow you to leverage the benefits of cloud-native patterns (stateless microservices, serverless functions, event-driven) as you think about adding new or existing business logic to your application.
Often, some business logic is better (or easier) done in the database than in the application layer. For example, complex high-performance aggregations (say a continuous time-series aggregate) or transactional stored-procedures.
Hasura makes it easy to use Postgres views (named-queries) and functions to add new types and fields to the GraphQL API. Hasura also makes it easy to customise and extend types and fields generated in the GraphQL API.
- Views & Functions
- Computed fields
- Customising GraphQL schema and field names (you can use the metadata APIs or the Hasura console UI to do this easily)
Hasura makes it easy to get the best out of your database, while enforcing and reusing a common authorisation system so that you don't have to write code for data-heavy tasks.
Bring your own GraphQL service: Custom business logic
Hasura provides an easy way to add business logic using your own GraphQL API using "remote schemas".
(This method is ideal when you need to bring types to your GraphQL schema that are not derived from the database that Hasura is already pointing to.)
Domain specific business logic often reside in microservices that might speak to other databases, external SaaS APIs or require complex application layer processing and transformations.
Hasura can integrates them directly into a unified GraphQL API and also allows you to configure semantic relationships between the types exposed by different GraphQL service by configuring "remote joins".
Event-driven business logic (write heavy workflows)
As microservices and serverless functions proliferate the “backend stack”, event-driven patterns are becoming the norm to deal with increasing complexity.
Driving business logic with events allows for easier decoupling and fault-tolerance without compromising on scalability.
Hasura Actions provide an easy way to create a custom GraphQL mutation specification and maps them to event handlers which can be microservices APIs or serverless functions (or even Postgres functions!).
Hasura internally takes care of the event plumbing, including event capture, persistence and delivery.