GraphQL &使用嵌套订阅功能 [英] GraphQL & Using a nested subscribe function

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

问题描述

我正在编写一个 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 订阅工作原理的理解,您可能对嵌套订阅不太走运.我还没有找到支持文档来支持这一点,但在我自己的实验中,我没有发现这行得通.在这种情况下,我建议您有一个消息根订阅,它需要 sucription 类型的参数,在这种情况下是私有、标记或团队消息的枚举.您可以使用 switch case 来确定订阅并做出相应的反应.

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.

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

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