简体   繁体   English

GraphQL 将输入 arguments 传递给订阅

[英]GraphQL passing input arguments to subscriptions

I'm trying to pass an input argument like ID to a GQL subscription.我正在尝试将像 ID 这样的输入参数传递给 GQL 订阅。 I'm working with:我正在与:

const { PubSub } =require ('apollo-server');

This is the apollo subscription reference page这是阿波罗订阅参考页面

Basically I'm trying with this easy subscription typedef:基本上我正在尝试使用这个简单的订阅 typedef:

 type Subscription{
  subscribe_device(id: String!): device}

And this is my subscription resolver:这是我的订阅解析器:

Subscription:{
    subscribe_device:(parent, args, context, info) =>{
        console.log(args)
        subscribe: () => pubsub.asyncIterator('stream')
    }
}

Actually without input arguments it works pretty good but in that case it seems to have problems with the asyncIterator实际上没有输入 arguments 它工作得很好,但在这种情况下,它似乎与 asyncIterator 有问题

Without input arguments:无输入 arguments:

Subscription:{
    subscribe_device:{
        subscribe: () => pubsub.asyncIterator('stream'),
    }
}

Someone know how to manage that issue?有人知道如何处理这个问题吗? How is it possible to input some arguments in subscriptions?如何在订阅中输入一些 arguments? I need it to create dynamically different "stream"我需要它来创建动态不同的“流”

Thank you very much for your help非常感谢您的帮助

I think what you are looking for is additional middleware (which is in the documentation ) where you can do stuff between getting something and returning something我认为您正在寻找的是额外的中间件(在文档中),您可以在其中做一些事情之间获取一些东西和返回一些东西

In addition, if you want to use the withFilters functionality, you can do a general calculation for every possible scenario and filter the desired outcome for a variables input and check with the payload .此外,如果您想使用withFilters功能,您可以对每个可能的场景进行一般计算,并为variables输入过滤所需的结果并检查payload For example only those who ask for a specific result will get notified of the changes over socket.例如,只有那些要求特定结果的人才会收到有关套接字更改的通知。

If you need to describe to channel based on arguments, you just use the args to create the channel name based on it.如果您需要基于 arguments 对频道进行描述,您只需使用args来创建基于它的频道名称。 For example:例如:

Subscription:{
  subscribeDevice: {
    subscribe: (_parent, args, _context, _info) => { 
      const { someId } = args;
      return pubsub.asyncIterator([`stream_${someId}`])
    }
  }
}

This approach is working for me using apollo-server@2.19.0 .这种方法对我apollo-server@2.19.0

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

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