I’m new to developing with Ash and I want to use AshJsonApi to interact with the application. I managed to configure multitenancy and run tests from IEX, and both actions and queries work correctly.
I’m trying to invoke the same operations from the REST client, but I keep encountering the error ‘(Ash.Error.Invalid.TenantRequired) Queries against the Helpdesk.Support.Ticket resource require a tenant to be specified.’
I tried creating a plug to set the tenant using Ash.PlugHelpers.set_tenant and a preparation, but I still can’t make any progress.
The resource:
defmodule Helpdesk.Support.Ticket do
use Ash.Resource,
domain: Helpdesk.Support,
data_layer: AshPostgres.DataLayer,
extensions: [AshJsonApi.Resource]
alias Helpdesk.Support.Ticket
json_api do
type “ticket”
end
postgres do
table “tickets”
repo Helpdesk.Repo
end
actions do
defaults [:read, :destroy]
read :list_tickets do
get? true
end
read :ticket_by_tenant do
argument :tenant, :string, allow_nil?: false
get? true
end
create :open do
accept [:subject]
end
update :update do
accept [:subject]
end
update :close do
accept []
change set_attribute(:status, :close)
end
end
Tell Ash that this Resource must belong to a tenant
multitenancy do
# Every tenant will ha
strategy :context
end
preparations do
prepare Helpdesk.Support.TenantPreparation
end
attributes do
uuid_primary_key :id
attribute :subject, :string, allow_nil?: false, public?: true
attribute :status, :atom do
constraints one_of: [:open, :close]
default :open
allow_nil? false
end
timestamps()
end
end
The domain:
defmodule Helpdesk.Support do
use Ash.Domain, extensions: [AshJsonApi.Domain]
alias Helpdesk.Support.Ticket
resources do
resource Ticket do
define :open_ticket, action: :open
define :list_tickets, action: :read
define :update_ticket, action: :update
define :destroy_ticket, action: :destroy
define :close_ticket, action: :close
define :get_ticket_by_tenant, args: [:tenant], action: :ticket_by_tenant
end
end
json_api do
routes do
# in the domain base_route
acts like a scope
base_route “/tickets”, Helpdesk.Support.Ticket do
get :read
index :read
post :open
get :list_tickets
get :ticket_by_tenant, route: “get_ticket_by_tenant/:tenant”
end
end
end
end
Thanks