-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Build schema registry like Hasura #23
Comments
If each tenant has its own custom schema, then the system needs to handle dynamic schema registration, storage, and retrieval per tenant. The challenge here is to isolate each tenant's schema while allowing efficient switching between schemas without restarting the server. In a multi-tenant setup like this, the typical approach involves:
Let's build a complete solution using Express and Apollo Server that meets the following requirements:
Implementation StrategyWe will build an Express server with the following features:
ImplementationStep 1: Install Necessary Packagesnpm install express apollo-server-express graphql graphql-tools uuid Step 2: Create the Server File (
|
Hasura does not create separate ports for each tenant. Instead, Hasura manages multi-tenancy on a single port using metadata configurations and role-based access control (RBAC) for schema isolation and access control. How Hasura Handles Multi-Tenancy
Managing Tenants with Hasura
How to Implement Multi-Tenancy in HasuraIf you're considering using Hasura for a multi-tenant setup, you can implement it in the following ways:
Example: Row-Level Security in Hasura
Final ThoughtsHasura does not require separate ports for each tenant because it leverages single-tenant schema configurations and role-based permissions to ensure data isolation and secure access. This simplifies management and deployment while providing flexibility to define complex multi-tenant access control mechanisms. If you want more advanced multi-tenancy strategies, such as using a separate database for each tenant or managing different sets of remote schemas, you can configure Hasura accordingly, but the basic endpoint remains the same. |
To implement a Hasura-like multi-tenant solution using Node.js, Express, and Apollo Server, we can create a basic server that manages tenants dynamically using a single endpoint and provides tenant-specific schemas and permissions. Overview of the ImplementationThis solution involves:
Full Code ImplementationLet's create a server that implements these features using Node.js, Express, and Apollo Server. Step 1: Set Up the Project
Step 2: Create the Server File (
|
To build an endpoint similar to Hasura, where you can dynamically manage GraphQL schemas, including adding or removing tables, fields, or remote schemas at runtime, you can create a custom server using Node.js, Express, and Apollo Server. Additionally, you will need to create endpoints to handle metadata changes and dynamically build or update your GraphQL schema.
Below is an example of a simple implementation that provides:
Step-by-Step Guide to Building a Hasura-like Dynamic GraphQL Endpoint
Step 1: Set Up the Server
First, let's initialize a basic Node.js server using
Express
andApollo Server
. We will also includegraphql-tools
to help with building and merging schemas dynamically.server.js
):Explanation:
Schema Management:
schemas
) is used to keep track of all the dynamically added schemas.mergeSchemas
from@graphql-tools/schema
is used to combine multiple schemas into one unified schema that the Apollo Server uses.Dynamic Schema Registration:
POST /add-schema
endpoint allows new schemas to be added dynamically by sending a request with the new schema's type definitions (typeDefs
) and resolvers (resolvers
).mergedSchema
in the Apollo Server.Dynamic Schema Removal:
POST /remove-schema
endpoint allows you to remove a schema by specifying its index in theschemas
array. This is for demonstration purposes and can be replaced with a more sophisticated schema management strategy.Apollo Server:
/graphql
for GraphQL queries.Step 2: Dynamic Schema Management
Use the following curl command to add a new schema to the server:
This command will add a new schema with a
Book
type and abooks
query.Once added, you can query the newly added schema by using the GraphQL playground or sending a query directly:
Step 3: Enhancements and Considerations
Final Thoughts
The above implementation gives you a basic structure for dynamically registering and managing GraphQL schemas, similar to Hasura's functionality. For production, consider adding more sophisticated metadata management, schema validation, and caching strategies to enhance the server's robustness and scalability.
The text was updated successfully, but these errors were encountered: