简体   繁体   English

Angular Apollo GraphQL 手表查询与订阅

[英]Angular Apollo GraphQL watchQuery vs subscription

I am new to GraphQL and need some help...我是 GraphQL 的新手,需要一些帮助...

As I see there is a plenty of components in GraphQL such as Query, Mutation and Subscription.正如我所见,GraphQL 中有很多组件,例如查询、变异和订阅。 In Angular Apollo Client I have two methods to deal with Query: apollo.query and apollo.watchQuery.在 Angular Apollo Client 中我有两种处理 Query 的方法:apollo.query 和 apollo.watchQuery。 As I understand query method gets the data only once when watchQuery constantly watches for updates.据我了解,当 watchQuery 不断监视更新时,查询方法仅获取一次数据。 And if so what is the difference between watchQuery and GraphQl subscription?如果是这样,watchQuery 和 GraphQl 订阅有什么区别?

As stated in the oficial apollo docs : "GraphQL subscriptions are a way to push data from the server to the clients that choose to listen to real time messages from the server. Subscriptions are similar to queries in that they specify a set of fields to be delivered to the client, but instead of immediately returning a single answer, a result is sent every time a particular event happens on the server."正如官方apollo 文档中所述:“GraphQL 订阅是一种将数据从服务器推送到选择监听来自服务器的实时消息的客户端的方式。订阅类似于查询,因为它们指定了一组字段交付给客户端,但不是立即返回单个答案,而是每次在服务器上发生特定事件时发送结果。”

So the subscription is just listening for some push alterations from the backend, and instead, the watchQuery is a query that whenever that query will be refetched or the data related to that query is changed from anywhere else, this method will keep on emitting the updated data.所以订阅只是监听来自后端的一些推送更改,相反,watchQuery 是一个查询,只要该查询将被重新获取或与该查询相关的数据从其他任何地方发生更改,此方法将继续发出更新的数据。

I dont know if you are confusing watchQuery with "live query", but live queries are not supported yet.我不知道您是否将 watchQuery 与“实时查询”混淆,但尚不支持实时查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM