SuperAPI AI 工具集

absinthe-subscriptions

通用与其它

查看项目

更新于 10-16 03:36

技能介绍

用途:在 Phoenix 项目中使用 Absinthe 实现 GraphQL 实时订阅,将订阅接入 Phoenix Socket 与 PubSub,并可在 Mutation 后向订阅者推送数据。

主要能力:配置 Endpoint 与 UserSocket;在 Schema 中定义订阅字段,用 config 返回主题,用 trigger 关联 mutation 自动发布;在 resolver 中调用 Absinthe.Subscription.publish 手动发布;支持按用户隔离的主题,如 "user:#{user.id}:notifications",并可在订阅 config 中做授权判断。

使用方式:在 Endpoint 中 use Absinthe.Phoenix.Endpoint 并声明 socket;在 UserSocket 中 use Absinthe.Phoenix.Socket, schema: MyApp.Schema,在 connect 中校验 token 并通过 Absinthe.Phoenix.Socket.put_options 写入 context;订阅字段用 config 指定 topic,用 trigger 绑定 mutation;发布时调用 Absinthe.Subscription.publish(MyAppWeb.Endpoint, data, topic_key: "topic");在 config 中设置 pubsub_server,并在 application 监督树中加入 Phoenix.PubSub 与 Absinthe.Subscription。

适用场景:实时消息、通知、帖子创建或更新等需要服务端推送的 GraphQL 接口;需要按用户或资源维度限定数据范围的场景。

限制与注意:需自行处理 connection 级授权与断线资源清理;主题粒度过细会影响性能,不应向宽泛主题发布敏感数据,也不宜使用无认证的订阅;建议对订阅做速率限制。官方资料未提供具体版本要求、性能数据或收费信息。