Skip to main content

Live Queries

A “live query” monitors the query a user provides and gives the client an updated version whenever the query would return a different result.

“实时查询(live query)”会持续监听客户端提交的查询,每当查询结果发生变化时,自动将最新结果推送给客户端。

Status当前状态​

Live queries are planned for a future PostGraphile V5 release. For now, use standard GraphQL subscriptions for real-time updates. The Real-time features page covers the subscription options currently available.

实时查询功能已列入 PostGraphile V5 后续版本的规划中。现阶段若需实现实时数据更新,请使用标准的 GraphQL 订阅(Subscription)。关于当前可用的订阅方案,请参阅实时特性页面。

Subscriptions should generally be preferred over live queries通常建议优先使用订阅而非实时查询

Subscriptions work on an event basis and are very scalable for that reason - look into pubsub scalability. They're also expressive, and can tell you things that your data might not - for example "Benjie is typing..." might not be a state that you store permanently to the database, but is is a reasonable event.

订阅基于事件驱动机制运行,因此具备极佳的可扩展性(可参阅 pubsub 的横向扩展能力)。同时,订阅的表达能力也更强,能够传递数据库状态本身未必持久化的信息——例如“Benjie 正在输入……”这类临时状态通常不会永久存入数据库,却非常适合作为实时事件进行推送。

Live queries on the other hand require a reactive datastore (or polling) to try and keep a particular query up to date whenever something under the query might cause it to change. It's very hard to achieve especially when combining multiple data sources, and is often a lot "noisier" on the network. There is not yet a GraphQL standard for live queries.

相比之下,实时查询需要依赖响应式数据存储(或轮询机制),才能在底层数据出现任何可能影响查询结果的变更时维持结果同步。当涉及跨多个数据源的组合查询时,这一点极难实现,而且往往会在网络上产生大量冗余通信。此外,GraphQL 目前尚未制定实时查询的官方标准。

The PostGraphile V4 implementation of live queries was highly experimental and had significant issues. Grafast should allow a much more powerful, performant, and stable implementation, but it has not been built yet. If you want live queries in V5, please sponsor the feature or reach out to work on it together.

PostGraphile V4 中的实时查询实现仍处于高度实验阶段,且存在不少明显缺陷。借助 Grafast,未来有望打造出更强大、更高性能且更稳定的实现,但该功能目前尚未开发完成。如果你在 V5 中迫切需要实时查询能力,欢迎赞助该特性的研发或联系我们共同参与共建。