简体   繁体   English

如何在GraphQL Apollo客户端中处理异步突变

[英]How to handle async mutations in GraphQL Apollo client

Structure 结构体

This is a question how to best use Apollo Client 2 in our current infrastructure. 这是一个问题,如何在当前的基础架构中最佳地使用Apollo Client 2。 We have one Graphql server (Apollo server 2) which connects to multiple other endpoints. 我们有一台Graphql服务器(Apollo服务器2)连接到其他多个端点。 Mutations are send via RabbitMQ and our Graphql also listens to RabbitMQ which then are pushed to the client via subscriptions. 变异通过RabbitMQ发送,我们的Graphql也监听RabbitMQ,然后通过订阅将其推送到客户端。

Implementation 实作

We have a Apollo server which we send mutations, these always give a null because they are async. 我们有一个Apollo服务器,我们发送突变,因为它们是异步的,所以它们总是给出null Results will be send back via a subscription. 结果将通过订阅发送回。 I have currently implemented it like this. 我目前已经这样实现了。

  • Send mutation. 发送变异。
  • Create a optimisticResponse . 创建一个optimisticResponse
  • Update the state optimistically via writeQuery in the update function. 通过update函数中的writeQuery乐观地更新状态。
  • When the real response comes (which is null) use the optimisticResponse again in the update method. 当实际响应到来时(为null),请在update方法中再次使用optimisticResponse
  • Wait for the subscription to come back with the response. 等待订阅返回响应。
  • Then refresh the the state/component with the actual data. 然后使用实际数据刷新状态/组件。

As you can see.. not the most ideal way, and a lot of complexity in the client. 如您所见..并不是最理想的方式,并且客户端中存在很多复杂性。

Would love to keep the client as dumb as possible and reduce complexity. 很乐意让客户尽量保持沉默,并减少复杂性。 Seems Apollo is mostly designed for sync mutations (which is logical). 似乎Apollo主要是为同步突变而设计的(这是合乎逻辑的)。

What are your thoughts on a better way to implement this? 您对实现此目标的更好方法有何看法?

Mutations can be asynchronous 变异可以是异步的

Mutations are generally not synchronous in Apollo Client. 突变通常在Apollo Client中不同步。 The client can wait for a mutation result as long as it needs to. 客户可以根据需要等待突变结果。 You might not want your GraphQL service to keep the HTTP connection open for that duration and this seems to be the problem you are dealing with here. 您可能不希望您的GraphQL服务在此期间保持HTTP连接打开,这似乎是您在此处要解决的问题。 Your approach responding to mutations goes against how GraphQL is designed to work and that starts to create complexity that - in my opinion - you don't want to have. 您应对变异的方法与GraphQL的设计工作方式背道而驰,并且开始产生您不希望拥有的复杂性-在我看来。

Solve the problem on the implementation level - not API level 在实施级别(而非API级别)解决问题

My idea to make this work better is the following: Follow the GraphQL spec and dogmatic approach. 我想使这项工作更好的想法如下:遵循GraphQL规范和教条主义方法。 Let mutations return mutation results. 让突变返回突变结果。 This will create an API that any developer is familiar working with. 这将创建任何开发人员都熟悉的API。 Instead treat the delivery of these results as the actual problem you want to solve. 而是将这些结果的交付视为您要解决的实际问题。 GraphQL does not specify the transport protocol of the client server communication. GraphQL没有指定客户端服务器通信的传输协议。 If you have websockets running between server and client already throw away HTTP and completely operate on the socket level. 如果您有在服务器和客户端之间运行的websocket,则已经丢弃了HTTP并完全在套接字级别上运行。

Leverage Apollo Client's flexible link system 利用Apollo客户的灵活链接系统

This is where Apollo Client 2 comes in. Apollo Client 2 lets you write your own network links that handle client server communication. 这是Apollo Client 2出现的地方。ApolloClient 2使您可以编写自己的网络链接来处理客户端服务器通信。 If you solve the communication on the link level, developers can use the client as they are used to without any knowledge of the network communication details. 如果您在链接级别解决通信问题,则开发人员可以在不了解网络通信细节的情况下使用客户端。

I hope this helps and you still have the chance to go in this direction. 我希望这会有所帮助,您仍然有机会朝这个方向前进。 I know that this might require changes on the server side but could be totally worth it when your application is frontend heavy (as most applications are these days) 我知道这可能需要在服务器端进行更改,但是当您的应用程序前端很繁重时(这是当今大多数应用程序),这可能是完全值得的

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

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