图形质量使用嵌套的订阅功能 [英] GraphQL & Using a nested subscribe function

查看:30
本文介绍了图形质量使用嵌套的订阅功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个graphql订阅服务器.如果我编写查询,则将解析程序嵌套在另一个之中是没有问题的,因此查询看起来像这样:

I am writing a graphql subscriptions server. If I write a query it is no problem to have resolvers nested one within the other, so the query would look something like this:

query {
  messages {
    privateMessage {
      id
      message
      userId
    }
  }
}

因此,首先执行 messages 解析器,然后执行 privateMessage 解析器.我想知道是否可以为订阅实现相同的结构,所以它看起来像这样:

So first the messages resolver is executed, then the privateMessage resolver is executed. I would like to know if the same structure is achievable for subscriptions so it would look like this:

subscription {
  messages {
    privateMessage {
      id
      message
      userId
    }
  }
}

这是我当前拥有的根订阅模式:

This is the current root subscription schema I have:

const RootSubscriptions = new GraphQLObjectType({
  name: 'RootSubscriptions',
  fields: {
    privateMessage: {
      type: PrivateMessage.type,
      resolve: PrivateMessage.resolve,
      subscribe: PrivateMessage.subscribe,
    },
    flaggedMessage: {
      type: FlaggedMessage.type,
      resolve: FlaggedMessage.resolve,
      subscribe: FlaggedMessage.subscribe,
    },
    teamMessage: {
      type: TeamMessage.type,
      resolve: TeamMessage.resolve,
      subscribe: TeamMessage.subscribe,
    },
  },
})

我希望它看起来像这样:

I would like it to look like this:

const RootSubscriptions = new GraphQLObjectType({
  name: 'RootSubscriptions',
  fields: {
    messages: {
      type: new GraphQLObjectType({
        name: 'MessagesSubType',
        fields: {
          privateMessage: {
            type: PrivateMessage.type,
            resolve: PrivateMessage.resolve,
            subscribe: PrivateMessage.subscribe,
          },
          flaggedMessage: {
            type: FlaggedMessage.type,
            resolve: FlaggedMessage.resolve,
            subscribe: FlaggedMessage.subscribe,
          },
          teamMessage: {
            type: TeamMessage.type,
            resolve: TeamMessage.resolve,
            subscribe: TeamMessage.subscribe,
          },
        }
      })
    }
  },
})

编辑结束

问题是我让 messages 订阅函数运行,但没有让 privateMessage 订阅函数运行.很想知道是否有可能以及如何实现它.由于我是使用node.js编写的,因此我希望在js中获得一个示例,但是任何指向解决方案的指针都将有所帮助.预先感谢!

Problem is that I get the messages subscribe function to run but not the privateMessage subscribe function to run. Would love to know if it is possible and how to achieve it. Since I'm writing it with node.js, I would appreciate an example in js, but any pointer to a solution would be helpful. Thanks in advance!

推荐答案

基于我对graphQl订阅工作方式的理解,嵌套订阅可能会带来一些运气.我没有找到支持该文档的支持文档,但是在我自己的实验中,我没有发现此文档有效.在这种情况下,我建议您有一个消息根订阅,该订阅需要使用推荐类型的参数,在这种情况下,该枚举可以是私有消息,已标记消息或团队消息.您可以使用切换案例来确定订阅并做出相应的反应.

Based on my understanding of how graphQl subscriptions work, you may have little luck with nested subscriptions. I've not found supporting documentation to support this, but in my own experimentations, I've not found this to work. In this case, I would advice that you have a messages root subscription that expects parameters of the type of sucription, in this case an enum of either private, flagged or team message. You can use switch case to determine the subscription and react accordingly.

这篇关于图形质量使用嵌套的订阅功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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