jeroen11dijk
AEAsh Elixir
•Created by jeroen11dijk on 6/4/2025 in #support
destroy action based on filters
I currently have a read action and I pass the result to the default destroy action, is it possible to have a destroy action which includes the read part? It seems like destroy only works when passing along the objects
So basically I want to destroy the results of
read :replannable_routes_by_date do
prepare build(load: [stops: [orders: [:parcels]]])
argument :depot_id, :uuid, allow_nil?: false
filter expr(depot_id == ^arg(:depot_id))
argument :date, :date, allow_nil?: false
prepare fn %{arguments: %{date: date}} = query, _ ->
{start_utc, end_utc} = TimezoneHelpers.date_range_for_timezone(date)
Ash.Query.filter(
query,
expr(projected_departure_time >= ^start_utc and projected_departure_time <= ^end_utc)
)
end
filter expr(replan_locked == false)
end
read :replannable_routes_by_date do
prepare build(load: [stops: [orders: [:parcels]]])
argument :depot_id, :uuid, allow_nil?: false
filter expr(depot_id == ^arg(:depot_id))
argument :date, :date, allow_nil?: false
prepare fn %{arguments: %{date: date}} = query, _ ->
{start_utc, end_utc} = TimezoneHelpers.date_range_for_timezone(date)
Ash.Query.filter(
query,
expr(projected_departure_time >= ^start_utc and projected_departure_time <= ^end_utc)
)
end
filter expr(replan_locked == false)
end
9 replies
AEAsh Elixir
•Created by jeroen11dijk on 5/26/2025 in #support
Error: `key :manual not found in: nil` with create_query using graphql
So I got this simple resource:
And using graphql I query
And this gives me the following stacktrace:
defmodule Zelo.Planner.PlanResult do
@moduledoc false
use Ash.Resource,
domain: Zelo.Planner,
authorizers: [Ash.Policy.Authorizer],
extensions: [
AshJason.Resource,
AshGraphql.Resource,
AshJsonApi.Resource
]
code_interface do
domain Cvs.Planner
define :create, action: :create
define :run_planner, action: :run_planner
end
actions do
create :create do
primary? true
accept :*
argument :routes, {:array, :map}, allow_nil?: false
change manage_relationship(:routes, type: :direct_control)
end
create :run_planner do
manual Zelo.Planner.ManualPlanCreate
end
end
policies do
policy always() do
authorize_if always()
end
end
graphql do
type :plan_result
mutations do
create :run_planner, :run_planner
end
end
json_api do
type "plan_result"
end
attributes do
uuid_primary_key :id
end
relationships do
has_many :routes, Zelo.Planner.RouteResult do
public? true
end
end
end
defmodule Zelo.Planner.ManualPlanCreate do
@moduledoc false
use Ash.Resource.ManualCreate
alias Ash.Changeset
alias Zelo.Planner.PlanResult
@impl true
def create(changeset, opts, context) do
{:ok,
%PlanResult{
id: "clappen",
routes: [
%{
vehicle_id: "vehicle-123",
distance: 1234,
duration: 1234,
polyline: "SDFGSDG",
projected_departure_time: DateTime.utc_now(),
projected_end_time: DateTime.utc_now()
}
]
}}
end
end
defmodule Zelo.Planner.PlanResult do
@moduledoc false
use Ash.Resource,
domain: Zelo.Planner,
authorizers: [Ash.Policy.Authorizer],
extensions: [
AshJason.Resource,
AshGraphql.Resource,
AshJsonApi.Resource
]
code_interface do
domain Cvs.Planner
define :create, action: :create
define :run_planner, action: :run_planner
end
actions do
create :create do
primary? true
accept :*
argument :routes, {:array, :map}, allow_nil?: false
change manage_relationship(:routes, type: :direct_control)
end
create :run_planner do
manual Zelo.Planner.ManualPlanCreate
end
end
policies do
policy always() do
authorize_if always()
end
end
graphql do
type :plan_result
mutations do
create :run_planner, :run_planner
end
end
json_api do
type "plan_result"
end
attributes do
uuid_primary_key :id
end
relationships do
has_many :routes, Zelo.Planner.RouteResult do
public? true
end
end
end
defmodule Zelo.Planner.ManualPlanCreate do
@moduledoc false
use Ash.Resource.ManualCreate
alias Ash.Changeset
alias Zelo.Planner.PlanResult
@impl true
def create(changeset, opts, context) do
{:ok,
%PlanResult{
id: "clappen",
routes: [
%{
vehicle_id: "vehicle-123",
distance: 1234,
duration: 1234,
polyline: "SDFGSDG",
projected_departure_time: DateTime.utc_now(),
projected_end_time: DateTime.utc_now()
}
]
}}
end
end
mutation {
runPlanner {
result {
id
routes {
vehicleId
}
}
}
}
mutation {
runPlanner {
result {
id
routes {
vehicleId
}
}
}
}
13 replies