您如何映射Apollo中GraphQl订阅的PubSub值?
我正在阿波罗制作GraphQL后端,我想使用订阅。我遵循了阿波罗的文档,并且使用graphql-subscriptions
获得了基本订阅。此软件包还通过fifilter 函数进行过滤。但是,我不想发出通过pubsub
发布的所有数据,因为其中一些数据仅用于过滤目的。
前任。
// GraphQL Schema
type Subscription {
mySubscription(filter: MySubscriptionFilter!): ID!
}
// Publishing the event
pubsub.publish("MY_SUBSCRIPTION", { id: "2092330", username: "asdf", roles: [ "USER", "MODERATOR" ] });
// Handling the event for a subscription
const resolvers = {
Subscriptions: {
mySubscription: {
subscribe: withFilter(
() => pubsub.asyncIterator("MY_SUBSCRIPTION"),
(payload, variables) => {
return customFiltering(payload, variables);
}
)
}
}
}
这将返回具有类型的对象:{id,用户名,角色}
。但是,用户名和角色字段仅用于过滤。我最终需要返回一个类型{mySubScription:id}
的对象,因为这就是我的GraphQl Schema所说的。
有办法做这样的事情吗?
// Handling the event for a subscription
const resolvers = {
Subscriptions: {
mySubscription: {
subscribe: withFilter(
() => pubsub.asyncIterator("MY_SUBSCRIPTION"),
(payload, variables) => {
return customFiltering(payload, variables);
}
).map(x => {
return { mySubsription: x.id }
}) // Map function where x is the payload from the pubsub
}
}
}
I'm making a GraphQL backend in Apollo, and I'd like to use subscriptions. I followed Apollo's docs, and I've gotten basic subscriptions working using graphql-subscriptions
. This package also comes with filtering via the built-in withFilter
function. However, I don't want to emit all of the data published via PubSub
because some of this data is used for filtering purposes only.
Ex.
// GraphQL Schema
type Subscription {
mySubscription(filter: MySubscriptionFilter!): ID!
}
// Publishing the event
pubsub.publish("MY_SUBSCRIPTION", { id: "2092330", username: "asdf", roles: [ "USER", "MODERATOR" ] });
// Handling the event for a subscription
const resolvers = {
Subscriptions: {
mySubscription: {
subscribe: withFilter(
() => pubsub.asyncIterator("MY_SUBSCRIPTION"),
(payload, variables) => {
return customFiltering(payload, variables);
}
)
}
}
}
This returns an object with the type: { id, username, roles }
. However, the username and roles fields are only used for filtering. I ultimately need to return an object of type { mySubscription: id }
, because that's what my GraphQL schema says.
Is there a way to do something like this?
// Handling the event for a subscription
const resolvers = {
Subscriptions: {
mySubscription: {
subscribe: withFilter(
() => pubsub.asyncIterator("MY_SUBSCRIPTION"),
(payload, variables) => {
return customFiltering(payload, variables);
}
).map(x => {
return { mySubsription: x.id }
}) // Map function where x is the payload from the pubsub
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哎呀,看来我忽略了订阅中的解决函数。
来自
graphql-subscriptions
”
您也可以通过在订阅中添加解决方法来操纵已发布的有效载荷:
Whoops, it looks like I overlooked the resolve function in a subscription.
From the
graphql-subscriptions
github pagePayload Manipulation
You can also manipulate the published payload, by adding resolve methods to your subscription: