exadra37
exadra37
AEAsh Elixir
Created by exadra37 on 2/10/2023 in #support
Any example of an ASH app running distributed?
I am looking for an open source project or a blog article that shows an app built with ASH that runs in distributed mode. If you know about one then please drop the link to it.
14 replies
AEAsh Elixir
Created by exadra37 on 2/10/2023 in #support
How can I delete a record by id?
Currently I can destroy a ticket with:
Helpdesk.Api.destroy_support_ticket ticket
Helpdesk.Api.destroy_support_ticket ticket
But I would like to be able to do it by ID:
Helpdesk.Api.destroy_support_ticket "5f63e748-68e0-4b7d-9030-47c38eb8e86e"
Helpdesk.Api.destroy_support_ticket "5f63e748-68e0-4b7d-9030-47c38eb8e86e"
My code:
defmodule Helpdesk.Support.Ticket do
# @link https://ash-hq.org/docs/guides/ash/latest/tutorials/get-started

# This turns this module into a resource
use Ash.Resource,
data_layer: Ash.DataLayer.Ets

actions do
# Add a set of simple actions. You'll customize these later.
defaults [:create, :read, :update, :destroy]

### ommitted code..

# @link https://ash-hq.org/docs/guides/ash/latest/code-interface
code_interface do
define :open_support_ticket, args: [:subject]
define :read_support_ticket, action: :read, get_by: [:id]
define :close_support_ticket, args: []
define :destroy_support_ticket, action: :destroy
end
end
defmodule Helpdesk.Support.Ticket do
# @link https://ash-hq.org/docs/guides/ash/latest/tutorials/get-started

# This turns this module into a resource
use Ash.Resource,
data_layer: Ash.DataLayer.Ets

actions do
# Add a set of simple actions. You'll customize these later.
defaults [:create, :read, :update, :destroy]

### ommitted code..

# @link https://ash-hq.org/docs/guides/ash/latest/code-interface
code_interface do
define :open_support_ticket, args: [:subject]
define :read_support_ticket, action: :read, get_by: [:id]
define :close_support_ticket, args: []
define :destroy_support_ticket, action: :destroy
end
end
4 replies
AEAsh Elixir
Created by exadra37 on 2/10/2023 in #support
How can we get a record by id?
I was able to figure out that i can get one with:
Helpdesk.Support.get! Helpdesk.Support.Ticket, ticket.id
Helpdesk.Support.get! Helpdesk.Support.Ticket, ticket.id
but I would like to get it with:
Helpdesk.Api.read_support_ticket ticket.id
Helpdesk.Api.read_support_ticket ticket.id
This doesn't work:
{:error,
%Ash.Error.Invalid{
errors: [
%Ash.Error.Invalid.MultipleResults{
count: 2,
at_least?: true,
changeset: nil,
query: nil,
error_context: [],
vars: [],
path: [],
stacktrace: #Stacktrace<>,
class: :invalid
}
],
stacktraces?: true,
changeset: nil,
query: nil,
error_context: [],
vars: [],
path: [],
stacktrace: #Stacktrace<>,
class: :invalid
}}
{:error,
%Ash.Error.Invalid{
errors: [
%Ash.Error.Invalid.MultipleResults{
count: 2,
at_least?: true,
changeset: nil,
query: nil,
error_context: [],
vars: [],
path: [],
stacktrace: #Stacktrace<>,
class: :invalid
}
],
stacktraces?: true,
changeset: nil,
query: nil,
error_context: [],
vars: [],
path: [],
stacktrace: #Stacktrace<>,
class: :invalid
}}
My code:
defmodule Helpdesk.Api do

require Ash.CodeInterface

Ash.CodeInterface.define_interface(Helpdesk.Support, Helpdesk.Support.Ticket)
end

defmodule Helpdesk.Support.Ticket do

use Ash.Resource,
data_layer: Ash.DataLayer.Ets

actions do
defaults [:create, :read, :update, :destroy]

create :open_support_ticket do
accept [:subject]
end

read :read_support_ticket do
# primary? true
argument :id, :uuid do
allow_nil? false
end
# filter [id: :id]
end

## ommitted code ...

code_interface do
define :open_support_ticket, args: [:subject]
define :read_support_ticket, action: :read_support_ticket, args: [:id], get?: true
define :close_support_ticket, args: []
end
end
defmodule Helpdesk.Api do

require Ash.CodeInterface

Ash.CodeInterface.define_interface(Helpdesk.Support, Helpdesk.Support.Ticket)
end

defmodule Helpdesk.Support.Ticket do

use Ash.Resource,
data_layer: Ash.DataLayer.Ets

actions do
defaults [:create, :read, :update, :destroy]

create :open_support_ticket do
accept [:subject]
end

read :read_support_ticket do
# primary? true
argument :id, :uuid do
allow_nil? false
end
# filter [id: :id]
end

## ommitted code ...

code_interface do
define :open_support_ticket, args: [:subject]
define :read_support_ticket, action: :read_support_ticket, args: [:id], get?: true
define :close_support_ticket, args: []
end
end
21 replies
AEAsh Elixir
Created by exadra37 on 2/10/2023 in #support
How to use define_interface example in docs it's not clear
Show where you found the issue I have followed the Get Started guide to build the Helpdesk app and then followed the Whats next section where we have a link to learn how to use Code interfaces. While I perfectly understood how to use the define_for , because it builds on top of the Helpdesk app, I wasn't able to grasp how to use the define_interface, because the example for it doesn't build on top of the Helpdesk app. Share the problematic documentation define_for and define_interface Notice how we included a specific Api module using define_for above. Without this, no functions will be defined in the resource. This is because you might want to define the interface for multiple resources in a single module. While we encourage the use of define_for Api , it is not the only way to do it. You could also do something like this:
defmodule MyApp.MyApi.Interface do
require Ash.CodeInterface

Ash.CodeInterface.define_interface(MyApp.MyApi, MyApp.Resource1)
Ash.CodeInterface.define_interface(MyApp.MyApi, MyApp.Resource2)
end
defmodule MyApp.MyApi.Interface do
require Ash.CodeInterface

Ash.CodeInterface.define_interface(MyApp.MyApi, MyApp.Resource1)
Ash.CodeInterface.define_interface(MyApp.MyApi, MyApp.Resource2)
end
And then call functions on MyApp.MyApi.Interface instead. How can I use the above code example in the context of the Helpdesk app?
45 replies