absinthe-graphql/absinthe_phoenix

How to pass params?

rafbgarcia opened this issue · 1 comments

Hello again, and sorry to bother so much, but couldn't find a solution by looking at the docs and diving into the code.

I'm trying to put the current user in the context but the params in def connect(params, socket) doesn't give me the arguments as I was expecting.

Instead, it returns the params in the query string, and I don't see my argument anywhere in that function.

pedidos

pry(2)> params
%{"vsn" => "2.0.0"}
pry(3)> socket
%Phoenix.Socket{assigns: %{}, channel: nil, channel_pid: nil,
 endpoint: Api.Endpoint, handler: Api.Channels.OrderSocket, id: nil,
 join_ref: nil, joined: false, private: %{}, pubsub_server: Api.PubSub,
 ref: nil, serializer: Phoenix.Transports.V2.WebSocketSerializer,
 topic: nil, transport: Phoenix.Transports.WebSocket,
 transport_name: :websocket, transport_pid: #PID<0.780.0>, vsn: "2.0.0"}

This is the related code:

JS

const operation = `subscription motoboyOrders($authToken: String!) {
  motoboyOrders(authToken: $authToken) {
    name
  }
}`

const absintheSocket = AbsintheSocket.create(
  new PhoenixSocket("ws://localhost:4001/socket")
);

const notifier = AbsintheSocket.send(absintheSocket, {
  operation,
  variables: {authToken: "my-auth-token"}
});

Server:

  subscription do
    field :motoboy_orders, :order do
      arg :auth_token, non_null(:string)

      config fn args, _ ->
        {:ok, topic: args.auth_token}
      end

      trigger :create_order, topic: fn order ->
        order.motoboy.auth_token
      end
    end
  end
  def connect(params, socket) do
    require IEx
    IEx.pry
    socket = Absinthe.Phoenix.Socket.put_opts(socket, context: %{
      current_motoboy: current_motoboy(params)
    })
    {:ok, socket}
  end

  defp current_motoboy(%{"auth_token" => auth_token}) do
    from(m in Motoboy, where: [auth_token: ^auth_token])
    |> first
    |> Repo.one
  end

What am I missing here?

Thanks again ❤️

AbsintheSocket.create(
  new PhoenixSocket("ws://localhost:4001/socket", {params: { token: 'my-token' }})
)