Re:Elixir, Phoenix, Absinthe, GraphQL, React, and Apollo
by benwilson-512 on Sunday April 21, 2019 @08:42PM
(#19715514)
Hey folks! Co-Author of Absinthe here, happy to answer any questions about it. The post here is good, although these days we recommend using Dataloader vs Absinthe.Ecto. Dataloader really extends the idea behind Absinthe.Ecto while providing in request caching, pluggable backends, and easier query manipulation.
[ Parent
| on HN ]
Re:Elixir, Phoenix, Absinthe, GraphQL, React, and Apollo
by AlchemistCamp on Monday April 22, 2019 @01:28AM
(#19716752)
I've got an app with back-end oauth-based login. I had a bit of a headache integrating the sessions with Absinthe and finally arrived on this in my Context module:
```
def call(conn, _) do
context = build_context(conn)
Absinthe.Plug.put_options(conn, context: context)
end
def before_send(conn, %Absinthe.Blueprint{} = blueprint) do
if blueprint.execution.context[:logout?] do
Auth.drop_current_user(conn)
else
conn
end
end
defp build_context(conn) do
with ["Bearer " <> token] <- get_req_header(conn, "authorization"),
{:ok, data} <- MyApp.Token.verify(token),
%{} = user <- get_user(data) do
%{current_user: user}
else
_ -> %{}
end
end
# rest of the file
```Then made a Logout middleware that sets logout? to true in the resolution context.
Is digging into the Blueprint as in the code above necessary? Is there a simpler way of solving this?
Re:Elixir, Phoenix, Absinthe, GraphQL, React, and Apollo
by schneidmaster on Sunday April 21, 2019 @08:46PM
(#19715525)
Good to know, thanks! I wasn't familiar with Dataloader, I'll have to check it out :)