towerops/lib/towerops_web/graphql/context.ex

31 lines
774 B
Elixir

defmodule ToweropsWeb.GraphQL.Context do
@moduledoc """
Plug that builds the Absinthe context from the authenticated API connection.
Reads the organization_id and user set by the ApiAuth plug and passes them
into the Absinthe context for use by resolvers.
"""
@behaviour Plug
def init(opts), do: opts
def call(conn, _opts) do
context = build_context(conn)
Absinthe.Plug.put_options(conn, context: context)
end
defp build_context(conn) do
context = %{}
context =
case conn.assigns[:current_organization_id] do
nil -> context
org_id -> Map.put(context, :organization_id, org_id)
end
case conn.assigns[:current_user] do
nil -> context
user -> Map.put(context, :user, user)
end
end
end