🌐 Web Development

Hasura

Hasura

Hasura is an open-source engine that instantly generates a GraphQL API on top of a PostgreSQL database. It enables CRUD operations, real-time subscriptions, and access control without code, and as a BaaS (Backend as a Service) it suits rapid prototyping.

πŸ“– Detailed Explanation

Hasura is an open-source GraphQL engine released in 2018. When you connect an existing PostgreSQL database, it analyzes the table structure and automatically generates a GraphQL API. Query, Mutation, and Subscription all work without writing any backend code, and it is especially useful for apps that need real-time data synchronization.

Its core feature is the authorization system. Row-level security can be defined declaratively at the GraphQL layer, so a rule such as "users can only see their own data" is set for each table. It integrates with JWT or webhook-based authentication, and receives session variables through x-hasura-* headers to use in authorization rules.

Actions and Remote Schemas overcome Hasura's limitations. Actions expose custom business logic (payment processing, email sending, etc.) as GraphQL mutations, and Remote Schemas merge external GraphQL servers into the Hasura schema. This makes it possible to provide a single GraphQL endpoint even in a microservice architecture.

Event Triggers detect database changes (INSERT, UPDATE, DELETE) and call webhooks. This lets you trigger asynchronous tasks such as sending notifications, updating search indexes, and invalidating caches when data changes. Hasura Cloud is a managed service that provides monitoring, caching, and rate limiting.

πŸ’» Code Example

# Hasuraκ°€ μžλ™ μƒμ„±ν•˜λŠ” GraphQL API μ˜ˆμ‹œ

# μ‚¬μš©μž 쑰회 (μžλ™ μƒμ„±λœ Query)
query GetUsers {
  users(
    where: { is_active: { _eq: true } }
    order_by: { created_at: desc }
    limit: 10
  ) {
    id
    name
    email
    posts {
      id
      title
    }
  }
}

# μ‚¬μš©μž 생성 (μžλ™ μƒμ„±λœ Mutation)
mutation CreateUser {
  insert_users_one(object: {
    name: "κΉ€μ² μˆ˜",
    email: "kim@example.com"
  }) {
    id
    created_at
  }
}

# μ‹€μ‹œκ°„ ꡬ독 (WebSocket 기반)
subscription OnNewMessage {
  messages(
    where: { room_id: { _eq: "room-123" } }
    order_by: { created_at: desc }
    limit: 1
  ) {
    id
    content
    user {
      name
    }
    created_at
  }
}

# ---- κΆŒν•œ κ·œμΉ™ μ˜ˆμ‹œ (Hasura Console 메타데이터) ----
# users ν…Œμ΄λΈ”μ˜ select κΆŒν•œ
# "μžμ‹ μ˜ λ°μ΄ν„°λ§Œ 쑰회 κ°€λŠ₯"
{
  "role": "user",
  "permission": {
    "filter": {
      "id": { "_eq": "X-Hasura-User-Id" }
    },
    "columns": ["id", "name", "email", "created_at"],
    "allow_aggregations": false
  }
}

# ---- Actions μ •μ˜ (μ»€μŠ€ν…€ mutation) ----
# REST APIλ₯Ό GraphQL mutation으둜 λž˜ν•‘
type Mutation {
  sendEmail(to: String!, subject: String!, body: String!): EmailResult
}

# handler: https://my-api.com/send-email

# ---- JavaScript ν΄λΌμ΄μ–ΈνŠΈ μ‚¬μš© μ˜ˆμ‹œ ----
import { createClient } from 'graphql-ws';
import { GraphQLClient } from 'graphql-request';

const client = new GraphQLClient('https://my-hasura.hasura.app/v1/graphql', {
  headers: {
    'Authorization': `Bearer ${token}`,
    'x-hasura-role': 'user',
  },
});

// μ‹€μ‹œκ°„ ꡬ독
const wsClient = createClient({
  url: 'wss://my-hasura.hasura.app/v1/graphql',
  connectionParams: {
    headers: { 'Authorization': `Bearer ${token}` },
  },
});

πŸ—£οΈ Examples of Practical Conversations

πŸ‘¨β€πŸ’» Startup CTO
"We need to build an MVP quickly, but we lack backend development resources. Implementing the GraphQL API ourselves would probably take a long time."
πŸ‘©β€πŸ’» Developer
"Try Hasura. Once you design just the PostgreSQL schema, the GraphQL API is generated right away. Setting up authorization takes only a few clicks in the console, and real-time subscriptions are supported out of the box, so you can easily implement chat or notification features. You can add custom logic with Actions."
πŸ‘¨β€πŸ’Ό Technical interviewer
"What should I watch out for when using Hasura?"
πŸ‘¨β€πŸ’» Candidate
"First, complex business logic should be separated into Actions or a standalone service. Second, you need to configure authorization rules carefully to prevent data exposure. Third, N+1 query problems can be solved with PostgreSQL indexes and Hasura's batching, and it's best to predefine complex aggregations as Views."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

⚠️ Cautions

πŸ”— Related Terms

πŸ“š Learn More