I have Absinthe middleware that checks the context for an authenticated user, like so:
defmodule CliftonWeb.Middlewares.CheckAuth do
@behaviour Absinthe.Middleware
require Logger
def call(resolution, _config) do
case resolution.context do
%{current_user: _} ->
resolution
_ ->
resolution
|> Absinthe.Resolution.put_result({:error, "unauthenticated"})
end
end
end
This is then applied in my schema:
defmodule CliftonWeb.Schema do
# Imports and plugin setup etc...
def middleware(middleware, _field, %{identifier: :student_queries} = object) do
[CliftonWeb.Middlewares.CheckAuth | middleware ]
end
def middleware(middleware, _field, object), do: middleware
query do
import_fields :student_queries
# etc...
end
end
However the middleware is never called when I make a query. Is it possible to match on any identifier, or only query
, mutation
etc?
In addition, is this the best way to only apply middlewares to certain groups of queries/mutations?