您如何映射Apollo中GraphQl订阅的PubSub值?

发布于 2025-02-07 17:01:11 字数 1408 浏览 1 评论 0原文

我正在阿波罗制作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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相对绾红妆 2025-02-14 17:01:12

哎呀,看来我忽略了订阅中的解决函数。

来自graphql-subscriptions

您也可以通过在订阅中添加解决方法来操纵已发布的有效载荷:

const SOMETHING_UPDATED = 'something_updated';

export const resolvers = {
  Subscription: {
    somethingChanged: {
      resolve: (payload, args, context, info) => {
        // Manipulate and return the new value
        return payload.somethingChanged;
      },
      subscribe: () => pubsub.asyncIterator(SOMETHING_UPDATED),
    },
  },
}

Whoops, it looks like I overlooked the resolve function in a subscription.

From the graphql-subscriptions github page

Payload Manipulation

You can also manipulate the published payload, by adding resolve methods to your subscription:

const SOMETHING_UPDATED = 'something_updated';

export const resolvers = {
  Subscription: {
    somethingChanged: {
      resolve: (payload, args, context, info) => {
        // Manipulate and return the new value
        return payload.somethingChanged;
      },
      subscribe: () => pubsub.asyncIterator(SOMETHING_UPDATED),
    },
  },
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文