简体   繁体   English

GraphQL-subscriptions:如何在订阅解析器中获取已发布的对象

[英]GraphQL-subscriptions: How to get published object in subscription resolver

I am using an express server with GraphQL subscriptions and subscriptions-transport-ws. 我正在使用具有GraphQL订阅和subscriptions-transport-ws的快递服务器。

I have set up the subscription with a given channel: 我已使用给定频道设置了订阅:

 ... const subscriptionManager = new SubscriptionManager({ schema: executableSchema, pubsub: pubsub, setupFunctions: { testRunChanged: (options, args) => { return { testRunChangedChannel: { filter: (testRun) => { return testRun.id === args.testRunId; } }, }; }, }, }); ... 

After a mutation is received a process is started on the server where the database entry of the test run is updated when finished. 接收到突变后,服务器上将启动一个过程,该过程将在完成后更新测试运行的数据库条目。 Now when the update promise for the database action passes the client should be informed. 现在,当数据库操作的更新承诺通过时,应该通知客户端。

Using the publish functionality from pubsub the subscription manager gets the information about the updated test run: 使用pubsub中的发布功能,订阅管理器将获取有关更新的测试运行的信息:

 ... RunningTestDbService.setToFinished(testRun).then(updatedTestRun => { pubsub.publish("testRunChangedChannel", updatedTestRun); }) ... 

After the subscription manager filters the subscriptions depending on the published testRun and the subscribed testRunId the subscription resolver function is called. 订阅管理器根据已发布的testRun和已订阅的testRunId过滤订阅后,将调用订阅解析器功能。 To update the client i have to fetch the updated test run again. 要更新客户端,我必须再次获取更新的测试运行。

How can i get the published test run object inside of the subscription resolver function? 如何在订阅解析器函数内部获取已发布的测试运行对象?

The subscription and the resolver look like this: 订阅和解析器如下所示:

 ... `testRunChanged(testRunId: ID!): TestRun!` ... Subscription: { testRunChanged(_, { testRunId }) { // need to fetch the test run from database again return TestRunDbService.getTestRunWith(testRunId); }, }, ... 

The object used in publish method as payload is then root parameter of your subscription resolver method - so in this case this is _ in your testRunChanged resolver function. 这样,在publish方法中用作有效负载的对象就是您的订阅resolver方法的root参数-因此,在这种情况下,您的testRunChanged解析程序函数中的testRunChanged_ You should simply do return _ . 您应该简单地return _

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

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