Hacker News new | past | comments | ask | show | jobs | submit login

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.



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?


Good to know, thanks! I wasn't familiar with Dataloader, I'll have to check it out :)


I can second this, we've been using dataloader in production for almost 2 years now. Fantastic library.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: