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
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
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









