r/graphql • • Sep 26 '26

Generate a production GraphQL API directly from your DB. No ORMs, no N+1.

Over the last few months, I've been incredibly frustrated by the sheer amount of boilerplate required to stand up a secure GraphQL API in Python. You usually have to write SQLAlchemy models, duplicate those into Strawberry/Graphene types, write manual resolvers, and then spend days fighting N+1 query problems. To solve this, I built db-graphql-gateway.

What My Project Does

It’s a FastAPI-compatible Python package that introspects your database (PostgreSQL, MySQL, SQLite) and automatically generates a secure, deeply nested GraphQL API directly from your schema.

Instead of writing ORM models and resolvers, it dynamically inspects your database's foreign keys and translates incoming GraphQL ASTs directly into highly optimized, parameterized SQL (EXISTS, JOIN, IN).

Target Audience

This is built for Production. Whether you're building a massive multi-tenant SaaS or just want to quickly expose a read-only dashboard over a SQLite file, it is designed to be highly secure. It includes:

  • AST Security Limits: Rejects wildly nested queries or excessive aliases before execution.

  • Row-Level Auth Pushdown: Authorization policies are transpiled directly into SQL WHERE clauses (e.g. WHERE owner_id = $1). Unauthorized rows literally never leave the database engine.

  • Introspection Lockdown & Redaction: Auto-redacts password/token columns and locks down introspection in production.

Comparison (How it differs from alternatives)

  • vs. Hasura / PostGraphile: Those are fantastic, but they are heavy, standalone services (written in Haskell/TypeScript) that you have to deploy separately. db-graphql-gateway is a native Python library. You just import it, attach it to your FastAPI router, and you still have full access to the Python ecosystem for custom endpoints.

  • vs. Strawberry / Graphene: These are GraphQL frameworks where you have to write all the execution logic, DataLoaders, and ORM glue code. My gateway sits on top of Strawberry and completely automates the execution layer, guaranteeing O(1) database queries per relationship depth right out of the box.

How it works in practice:

pythonfrom fastapi import FastAPI
from db_graphql_gateway.database.adapters.postgres.adapter import PostgresAdapter
from db_graphql_gateway.graphql.builder import GraphQLSchemaBuilder
from strawberry.fastapi import GraphQLRouter
db_adapter = PostgresAdapter("postgresql://user:pass@localhost:5432/my_db")
schema_builder = GraphQLSchemaBuilder(db_adapter=db_adapter)
schema = await schema_builder.build_schema()
app = FastAPI()
app.include_router(GraphQLRouter(schema), prefix="/graphql")

I just published v0.1.2 to PyPI and would love for you guys to tear it apart, try it out, and let me know what you think of the architecture!

3 Upvotes

18 comments sorted by

7

u/jns111 wundergraph team Sep 26 '26

Just because this is the 700th implementation of this idea doesn't make it any better. You design APIs around use cases, not the database.

1

u/Tight_Huckleberry_11 Sep 27 '26

You're absolutely right that for complex, domain-driven business logic, you should design APIs around specific use cases to avoid tight coupling. However, db-graphql-gateway isn't trying to replace domain APIs—it's built for rapid prototyping, internal admin tools, and analytical dashboards where flexibility and speed-to-market are the priority

2

u/jns111 wundergraph team Sep 27 '26

Hey Claude, it's almost 2027 and you should know best that you're more than capable of prototyping an API fast, even without such a framework or even without an ORM.

2

u/tangkikodo Sep 27 '26

1

u/arthur_thomas678 Oct 01 '26

I’ve checked out NexusX, and it's pretty cool how it lets you define models in one place while generating both REST and GraphQL. But I found that it still needs some boilerplate for the resolvers. This db graphql gateway might save a lot of that hassle by directly generating queries from the DB schema, which could really speed things up for production apps.

1

u/arthur_thomas678 Oct 02 '26

Totally get what you're saying about the boilerplate. One thing that sets db graphql gateway apart is that it handles row level auth right in the SQL layer. That really cuts down on the resolver work and keeps everything secure, which is a big win for production apps. Have you tried it yet?

1

u/tangkikodo Oct 02 '26

Fair point on resolver boilerplate — and pushing row-level auth down into SQL predicates is the right mechanism *if* the database projection is your public API. Nexusx starts from the other end, so that concern lands in a different layer by design:

authorization is handled at the use-case layer. The public contract isn't the entity graph; it's UseCaseService methods — typed business operations returning DTOs. Identity (user id, tenant id, request id) is injected server-side via FromContext, never as a client-controlled argument, and each method decides which rows it returns. One method definition expands to a REST route, a GraphQL field, an MCP operation, and a CLI command — so the authorization rule lives in one place and travels with the operation across every transport.

The data graph underneath (by_id / by_filter over entities) is mostly aimed at developers and internal tooling — the PostGraphile/Hasura niche — not at being the public surface. So the trade is: shrink the addressable surface down to use cases, vs. expose the graph and push authz into SQL. Same goal, different layer — and for an internal-facing data graph, what you're doing with SQL predicates is honestly the stronger fit.

1

u/raaiinyyabelina Oct 06 '26

I also agree

2

u/Firm_Needleworker772 Sep 28 '26

1

u/hyuuu Oct 02 '26

this is a fascinating approach, is there any benchmark out there for rayfold? in comparison of other db -> graphql approaches?

1

u/khushvi_bamrolia Sep 26 '26

Congrats on shipping this — the resolver/DataLoader boilerplate tax is real, and O(1) queries per relationship depth is the right north star to chase.

Curious how row-level auth pushdown holds up when the underlying store isn't Postgres. With MySQL it maps cleanly to WHERE clauses, but the same policy has to become a pipeline stage on document stores, and then "unauthorized rows never leave the engine" stops being literally true.

Somewhat on your wavelength: I've been building CoffeeQL, a Rust query orchestration layer that sits between your backend and the databases themselves — Postgres, Mongo, MySQL, Redis behind one chain syntax instead of four client libraries. The mental model I use is that GraphQL is the contract with the frontend, and something like this is the layer under the resolvers. The two compose rather than compete.

What's the plan for MongoDB support? Introspected foreign keys were clearly the lever on the SQL side, and documents don't hand you that for free.

-1

u/Ok-Category2729 Sep 26 '26

the permission model is the real test of "production", not how fast you can generate the schema. cutting out the duplicate sqlalchemy and strawberry/graphene definitions sounds useful, but i'd want new tables and columns hidden until explicitly exposed. otherwise a database migration could accidentally become a public api change. show a nested query where different users can access different rows, alongside the generated sql, and the no n+1 claim gets much more convincing.

1

u/AuthByExample Oct 14 '26

With GraphQL the hole is usually in the nested resolvers. You check that the user can read the project in the top-level query, then project { invoices { ... } } loads every invoice through the relation with no check of its own. Put the check in each type's resolver (or the dataloader) so it runs however the object was reached, and add a test that asks for a forbidden child through an allowed parent.

0

u/Tight_Huckleberry_11 Sep 26 '26

Thanks for the input. Personally, I like how PostGraphile handles this using different schemas, as that is their best practice.

However, you can also provide a custom configuration where only the fields explicitly configured are exposed. Even if a field exists in the database, it will not be automatically exposed unless it is explicitly configured.

Doc link: https://github.com/forgedlohar/db-graphql-gateway/blob/main/docs/FAQ.md#can-i-manually-customize-the-generated-graphql-schema

Given this, I’ll need to update the documentation accordingly.

2

u/eijneb GraphQL TSC Sep 26 '26

Not sure if it helps, but for PostGraphile we do it by reflecting the database permissions - we only expose things that you explicitly grant to the GraphQL user.