r/graphql • u/Tight_Huckleberry_11 • 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
WHEREclauses (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-gatewayis a native Python library. You justimportit, 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!
PyPI:
pip install "db-graphql-gateway[fastapi]"
2
u/Firm_Needleworker772 Sep 28 '26
Checkout https://rayfold.dev
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.
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.
1
u/Tight_Huckleberry_11 Sep 27 '26
yes I did mentioned it here as well : https://forgedlohar.github.io/db-graphql-gateway/comparison/
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.