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) opts = if Application.get_env(:towerops, :env) == :prod do [context: context, analyze_complexity: true, max_complexity: 500] else [context: context] end Absinthe.Plug.put_options(conn, opts) 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