Does PostGraphile Fit Your Project?PostGraphile 适合你的项目吗?
Hopefully you’ve been convinced that PostGraphile serves an awesome GraphQL API, but now let’s take a more critical look at whether or not you should adopt PostGraphile for your project.
相信你已经体会到 PostGraphile 能够提供出色的 GraphQL API,但现在不妨更审慎地评估一下:你的项目究竟适不适合采用 PostGraphile。
PostGraphile’s audience is for people who want to prioritize their product. PostGraphile allows you to define your content model in the database as you normally would, however instead of building the bindings to the database (your API) PostGraphile takes care of it.
PostGraphile 面向的是希望将精力优先投入到产品本身而非繁琐胶水代码的团队。你可以像往常一样直接在数据库中定义数据模型,而原本需要手动编写的数据库对接层(即你的 API),则全部交由 PostGraphile 来处理。
This takes a huge maintenance burden off your shoulders. Now you don’t have to worry about optimizing the API and the database, instead you can focus on just optimizing your database. Scaling a database is well-understood — and you can combine techniques — scaling vertically with larger database servers (more RAM, faster storage), or horizontally with read replicas.
这能为你卸下沉重的维护负担。你不再需要同时在 API 层和数据库层两边奔波调优,只需专注于优化数据库本身即可。数据库扩展已是非常成熟的领域,你可以灵活组合多种策略——既可以通过升级更大规格的数据库服务器(更大内存、更快存储)进行纵向扩展,也可以借助只读副本实现横向扩展。
No Lock-In无技术锁定
PostGraphile does not lock you into using PostGraphile forever — in fact most of
the work you do implementing a PostGraphile API is in your database, which you
can take with you if you chose to move to a different system, so no work is
lost. If you feel comfortable with the cost of building your API, PostGraphile
is simple to switch with a custom solution — you can even export the GraphQL SDL
PostGraphile builds for you so you just need to implement your own resolvers.
And thanks to graphile-export, if your plugins support it, you can export
your GraphQL schema as executable code including the Grafast plan resolvers.
PostGraphile 并不会把你死死绑在它的生态里——事实上,使用 PostGraphile 构建 API 时的大部分工作都是在数据库层面完成的;即使未来决定迁移到其他系统,这些积累也都可以完整带走,不会白费。如果你决定投入成本自建 API,要将 PostGraphile 替换为自定义方案也非常容易:你甚至可以直接导出 PostGraphile 生成的 GraphQL SDL,只需按这份契约自行实现对应的 resolver 即可。此外,借助 graphile-export,只要你使用的插件支持导出,还能直接将 GraphQL schema 连同 Grafast plan resolver 一并导出为可执行代码。
PostGraphile does not ask you to do anything too divergent with your PostgreSQL schema, allowing you to take your schema (and all your data) to whatever solution you build next, and being confident that it was well designed — hand rolled by you! GraphQL itself provides a simple and clear deprecation path if you want to start using different fields. And of course with Graphile Build plugins you can extend (or remove) functionality as you wish.
PostGraphile 不会强迫你对 PostgreSQL schema 做任何标新立异的改动,这意味着你可以放心地将自己亲手精心设计的 schema(以及全部数据)无缝迁移到下一个技术方案中。如果日后需要更换字段设计,GraphQL 本身也提供了简单明了的废弃(deprecation)机制;当然,你还可以随时通过 Graphile Build 插件按需扩展或移除功能。
Further, you can migrate away from PostGraphile bit by bit by placing a GraphQL proxy in front of it and redirecting certain resolvers to your new solution. This enables you to move away from PostGraphile with zero downtime.
更进一步,你还可以逐步迁出 PostGraphile:只需在它前面架设一层 GraphQL 代理,将特定 resolver 的请求逐步转发到新方案即可。这种方式能够实现零停机的平滑迁移。
Ideally, PostGraphile will scale with your company (get in touch if you’re facing scaling issues — we can almost certainly help!), however there is a simple exit path even years into the business. We welcome your contributions to help PostGraphile scale and meet your needs, and are very open to sponsored improvements to the software.
理想情况下,PostGraphile 会伴随你的业务一同成长扩展(如果遇到扩展性难题,欢迎联系我们——我们十有八九能帮上忙!);即便在业务运行多年后,依然保留着顺畅的退出路径。我们也非常欢迎你为 PostGraphile 贡献代码以满足自身规模需求,并且十分乐意接受针对特定功能改进的赞助开发。
Schema Driven APIsSchema 驱动的 API
If you fundamentally disagree with “one-to-one mapping of a SQL schema to an API” (GraphQL or otherwise) this section is for you.
如果你从根本上就不认同“将 SQL schema 一对一映射为 API”(无论是 GraphQL 还是其他形态),那么本节正是为你而写。
First of all, PostGraphile’s out of the box behavior is not necessarily meant
to be the be-all and end-all of your API. PostGraphile was created to allow you
to focus on your product and not the API. If you need to integrate external
systems, there are easy to use ways to extend your schema (e.g.
extendSchema). If you want to
exclude things from your schema, we have a powerful behavior
system you can use to accomplish that using global preferences and
local overrides, or you can simply remove things from the
schema. If you want a custom
manually written API there is a simple transition path (read no lock-in).
首先,PostGraphile 的开箱即用行为本来就不是要成为你整个 API 的全部。设计 PostGraphile 的初衷是让你专注于产品本身,而不必在 API 样板代码上消耗精力。如果你需要集成外部系统,有非常便捷的方式可以扩展 schema(例如 extendSchema);如果你想隐藏 schema 中的部分内容,我们提供了强大的 behavior 系统,支持通过全局偏好设置与局部覆盖规则来控制,也可以直接从 schema 中移除指定内容;而如果你最终仍想要一套完全手写的定制 API,迁移路径也同样顺畅(参见无技术锁定)。
If you still can’t get over the auto-generated nature of PostGraphile consider the following arguments why putting your business logic in PostgreSQL is a good idea:
如果你对 PostGraphile 自动生成 API 的方式仍有顾虑,不妨看看将业务逻辑放在 PostgreSQL 中的几大理由:
-
PostgreSQL already has a powerful user management system with fine grained row-level security. A custom API would mean you have to build your own user management and security, and having to guarantee that every possible route to your database data is vetted by the same permissions logic (which PostgreSQL RLS does for you).
-
PostgreSQL allows you to hide implementation details with views of your data. Simple views can even be auto-updatable. This provides you with the same freedom and flexibility as you might want from a custom API except more performant.
-
PostgreSQL gives you automatic relations with the
REFERENCESconstraint and PostGraphile automatically detects them. With a custom API, you’d need to hardcode these relationships, which can become quite a chore!PostgreSQL 可通过
REFERENCES约束自动建立表间关联,PostGraphile 会自动识别这些 relation。而在自建 API 中,你必须把这些 relation 逐一硬编码,维护起来相当繁琐! -
For what it’s worth, you can write in your favorite scripting language in PostgreSQL, including JavaScript and Ruby.
值得一提的是,你在 PostgreSQL 中还可以使用自己熟悉的脚本语言来编写逻辑,例如 JavaScript 和 Ruby。
-
If you don’t want to write your code inside PostgreSQL, you could also use PostgreSQL’s
NOTIFYfeature to fire events to a listening Ruby or JavaScript microservice (this could include email transactions and event reporting), implement a job queue (Graphile Worker is one approach to this), or add a Graphile Build plugin to wrap or replace a PostGraphile plan resolver.如果不想把代码写在 PostgreSQL 内部,你也可以利用 PostgreSQL 的
NOTIFY机制向正在监听的 Ruby 或 JavaScript 微服务推送事件(例如发送事务性邮件、上报业务事件),或者引入任务队列(Graphile Worker 就是一种常见方案),甚至还可以编写 Graphile Build 插件来包装或替换 PostGraphile 的 plan resolver。 -
Well implemented1 logic in the database can be hundreds or even thousands of times faster than implementing the same logic in the application layer via an ORM or similar abstraction; this performance improvement can eliminate the need for caching and the dreaded cache invalidation problem, at least for a while. In the database, all the data is right there - there’s no need for expensive round-trips over the network, and the serialization/deserialization and data transfer costs are eliminated since no data is transferred to remote clients.
实现得当[^1]的数据库端逻辑,其运行速度往往比在应用层通过 ORM 或类似抽象层实现快出数百甚至数千倍;这种性能优势甚至可以让你省去缓存层,至少在相当长一段时间内远离令人头疼的缓存失效难题。因为所有数据原本就近在数据库本地,无需经历昂贵的网络往返,也避免了序列化、反序列化以及向远端传输中间数据的开销。
Still worried about a certain aspect of a schema driven API? Open an issue, we’re confident we can convince you otherwise 😉
对 schema 驱动的 API 还有某些具体顾虑?欢迎提交 issue 讨论,我们有信心打消你的疑虑 😉
This article was originally written by Caleb Meredith.
本文最初由 Caleb Meredith 撰写。