GraphQL-subscriptions:如何在订阅解析器中获取已发布的对象 [英] GraphQL-subscriptions: How to get published object in subscription resolver

查看:100
本文介绍了GraphQL-subscriptions:如何在订阅解析器中获取已发布的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有GraphQL订阅和subscriptions-transport-ws的快递服务器.

I am using an express server with GraphQL subscriptions and 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.

使用pubsub中的发布功能,订阅管理器将获取有关更新的测试运行的信息:

Using the publish functionality from pubsub the subscription manager gets the information about the updated test run:

...

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

...

在订阅管理器根据发布的testRun和订阅的testRunId筛选订阅之后,将调用订阅解析器功能.要更新客户端,我必须再次获取更新的测试运行.

After the subscription manager filters the subscriptions depending on the published testRun and the subscribed testRunId the subscription resolver function is called. 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);
  },
},

...

推荐答案

publish方法中用作有效负载的对象是预订resolver方法的root参数-在这种情况下,这是_testRunChanged解析器函数中.您只需执行return _.

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. You should simply do return _.

这篇关于GraphQL-subscriptions:如何在订阅解析器中获取已发布的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆