fireproofsocks

fireproofsocks

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.11.0 but I don’t see where I should call Absinthe.run/3 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!

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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}
    else
      _ -> %{}
    end
  end

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.

Last Post!

fireproofsocks

fireproofsocks

Thanks!

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement