Filter expression doesn't work in prepare?

This works:

read :future_works do
    argument :foo, :atom do
        constraints one_of: [:a, :b, :c]
    end

    filter expr(
        date >= now() and
        if is_nil(^arg(:foo)) do true else ^arg(:foo) in allowed_foos end
    )
end

This doesn’t work:

read :future_works do
    argument :foo, :atom do
        constraints one_of: [:a, :b, :c]
    end

    prepare fn query, _ctx ->
       query
       |> Ash.Query.filter(^arg(:foo) in allowed_foos) # gives an error and doesn't compile
       |> Ash.Query.filter(Ash.Query.get_argument(query, :foo) in allowed_foos) # gives an error and doesn't compile
       |> Ash.Query.filter(expr(arg(:foo) in allowed_foos)) # gives an error and doesn't compile
    end
end

Whats the correct way to write this? My point is that I want to re-use this filter for multiple read actions.

I think you need to put the value into a variable and use that in the expression.

read :future_works do
    argument :foo, :atom do
        constraints one_of: [:a, :b, :c]
    end

    prepare fn query, _ctx ->
      foo = Ash.Query.get_argument(query, :foo)

       query
       |> Ash.Query.filter(^foo in allowed_foos) # gives an error and doesn't compile
     end
end

This works if I require Ash.Query first.

Thanks! I don’t understand why it works but it does.