有没有办法从服务器端强制断开 absinthe graphql 订阅?

问题描述

我正在开发的 Phoenix 应用程序会创建对文档更改的订阅,只要该文档是“公开的”。但是,如果有人将他们的文档更改为“私人”,我不希望这些订阅继续接收更新。

我知道如何防止在私人文档上创建新订阅,但我不确定如何从服务器端禁用预先存在的订阅

解决方法

您需要为对应于特定用户的每个套接字分配一个 ID。然后你可以这样做:

MyAppWeb.Endpoint.broadcast(socket_id,"disconnect",%{})

要将 ID 分配给套接字,请找到执行此操作的模块:

use Absinthe.Phoenix.Socket,schema: MyAppWeb.Schema

可能在lib/my_app_web/channels/user_socket.ex

在那个“socket”模块中,你像这样编写你的 ID 回调:

  @impl true
  def id(%{
        assigns: %{
          absinthe: %{
            opts: [
              context: %{current_user: current_user}
            ]
          }
        }
      }),do: socket_id_for_user(current_user)

  def id(_socket),do: nil

  def socket_id_for_user(%{id: id}),do: "user_socket:#{id}"

现在您只需确保此 current_user 分配在您的套接字上。

首先,转到您的路由器并将这条线放入任何需要它的管道中(通常只是 :api 管道,有时是 :browser 管道):

    plug :fetch_current_user

在您的路由器中(或在您喜欢的导入模块中),编写此函数:

  def fetch_current_user(conn,_opts) do
    # I don't know how you do auth so get your user your own way.
    # For me,it usually involves finding their session token in the DB.
    user = get_user_from_conn(conn)

    context = if is_nil(user),do: %{},else: %{current_user: user}

    conn
    |> Absinthe.Plug.assign_context(context)
  end

你可能想在这个函数中做其他事情。 例如,如果您使用 phx_gen_auth,您可能会将 user_token 置于私有分配中。

您现在遇到的主要问题是,如果您通过此套接字发送注销突变,则在发送响应之前您将关闭它。很有趣的问题。

正如这里所讨论的:https://elixirforum.com/t/is-there-a-way-to-force-disconnect-an-absinthe-graphql-subscription-from-the-server-side/41042 我的方法不允许您终止特定的描述。我所描述的是一种终止整个套接字的方法。使用这种方法,如果需要,客户端将不得不创建一个新的“未经身份验证”的套接字。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...