用途:在 Elixir/Phoenix 项目中使用 Absinthe 实现实时 GraphQL 订阅,覆盖 Phoenix 通道、PubSub 与订阅模式。
主要能力:在 Endpoint 中接入 Absinthe.Phoenix.Endpoint 并配置 socket;在 UserSocket 中通过 Absinthe.Phoenix.Socket 绑定 schema,并在 connect 中校验 token、用 put_options 注入 context(如 current_user);用 object 定义订阅字段,通过 config 返回 {:ok, topic: ...} 指定主题,通过 trigger 在 mutation 上自动发布;在 resolver 中用 Absinthe.Subscription.publish 手动发布,支持按用户隔离的主题(如 user:<id>:notifications);支持在 config 中做授权判断,未授权返回 {:error, "Unauthorized"}。
使用方式:配置 pubsub_server(如 config :my_app, MyAppWeb.Endpoint, pubsub_server: MyApp.PubSub),在 application.ex 的 children 中加入 {Phoenix.PubSub, name: MyApp.PubSub} 与 {Absinthe.Subscription, MyAppWeb.Endpoint}。
适用场景:需要实时推送的 GraphQL 应用,如帖子创建/更新通知、用户私有消息与通知。
限制与注意:资料未提供版本、依赖安装命令与性能数据;建议按主题限定数据范围、在连接层认证、处理断连清理、限制订阅频率,避免向宽泛主题发布敏感数据、避免无认证订阅、避免主题粒度过细。