How to handle max cost on a per user basis in absinthe

I’m preparing a graphQL API and need to maintain different :max_complexity on a per user basis.

Since I am not using Absinthe.run/3 but :absinthe_plug which only supports a global maximum as I read the documentation, I’m wondering if I can set the max in a different way in a plug via the context (or whatever).

I’m already preparing a plug which does authentication for graphql, so I could extend on that if we can communicate the appropriate value of :max_complexity to absinthe or do I need to throw away absinthe_plug and do it completely manually?

Maybe https://hexdocs.pm/absinthe_plug/Absinthe.Plug.html#put_options/2 would work?

case user do
  %User{admin?: true} -> Absinthe.Plug.put_options(conn, max_complexity: 100)
  _user -> Absinthe.Plug.put_options(conn, max_complexity: 50)
end

It seems to be updating conn.private.absinthe map.

For :max_complexity to work, analyze_complexity: true should be set during plug’s init as well, I think.

Absinthe.Plug.init([max_complexity: @default_max_complexity, analyze_complexity: true] ++ opts)
1 Like

Or

Absinthe.Plug.put_options(conn, raw_options: %{max_complexity: 123})

since these complexity options seem to be stored in conn.private.absinthe.raw_options.

1 Like

Thank you!

I’ll take a closer look this evening.

1 Like