Accessing request data (~Plug.Conn) in Absinthe/GraphQL

It’s bit a hot minute since I worked with Absinthe and GraphQL…

I’m wondering where you can access request data, e.g. the session ID or API key? I think I’m in the right place looking at The Context and Authentication — absinthe v1.7.0 but I don’t see where I should call to set the context variables. When I have the document, the conn is not available (?); in the auth plugin, the document isn’t available.

Could someone point me in the right direction?
Thanks in advance!

1 Like

Hey @fireproofsocks you’re very close to the right answer! The Context and Plugs section there shows how to set the Absinthe Context from plug, and pass in values from the conn.

Specifically, the function here:

  def build_context(conn) do
    with ["Bearer " <> token] <- get_req_header(conn, "authorization"),
    {:ok, current_user} <- authorize(token) do
      %{current_user: current_user}
      _ -> %{}

could be amended to add any other value that you want. Say for example you want the client IP address just do:

%{current_user: current_user, client_ip: conn.remote_ip}

What might be tempting to do but I recommend avoiding is to just pass the whole conn into the context. Extract what you actually need for your logic, set that in the context, and go from there.