How to read resource without actions

I'm trying to inspect MyApp.Accounts.Token using iex, but I'm getting NoPrimaryAction error. Is it possible to inspect resources without defining read action? Is there a way to convert it to Ecto.Schema, so I can use Ecto.Query?
> MyApp.Accounts.read(MyApp.Accounts.Token)
{:error,
%Ash.Error.Invalid{
errors: [
%Ash.Error.Invalid.NoPrimaryAction{
resource: MyApp.Accounts.Token,
type: :read,
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
}}
> MyApp.Accounts.read(MyApp.Accounts.Token)
{:error,
%Ash.Error.Invalid{
errors: [
%Ash.Error.Invalid.NoPrimaryAction{
resource: MyApp.Accounts.Token,
type: :read,
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
}}
defmodule MyApp.Accounts.Token do
use Ash.Resource,
data_layer: AshPostgres.DataLayer,
extensions: [AshAuthentication.TokenResource]

token do
api MyApp.Accounts
end

postgres do
table "tokens"
repo MyApp.Repo
end
end
defmodule MyApp.Accounts.Token do
use Ash.Resource,
data_layer: AshPostgres.DataLayer,
extensions: [AshAuthentication.TokenResource]

token do
api MyApp.Accounts
end

postgres do
table "tokens"
repo MyApp.Repo
end
end
2 Replies
jart
jart3y ago
Hi there. There are actions generated by AshAuthentication but you can add your own also. Just add
actions do
defaults [:read]
end
actions do
defaults [:read]
end
And you’ll be good to go. In answer to your other question - every Ash resource is an Ecto schema so you can just do MyApp.Repo.all(MyApp.Accounts.Token)
NDaniil
NDaniilOP3y ago
every Ash resource is an Ecto schema
Awesome, this is what I was looking for. I just found an introspection tool to get a list of actions without reading AshAuthentication source code.
iex(6)> Ash.Resource.Info.actions(MyApp.Accounts.Token) |> Enum.filter(& &1.type == :read) |> Enum.map(& &1.name)
[:get_token, :get_confirmation_changes, :revoked?, :read_expired]
iex(6)> Ash.Resource.Info.actions(MyApp.Accounts.Token) |> Enum.filter(& &1.type == :read) |> Enum.map(& &1.name)
[:get_token, :get_confirmation_changes, :revoked?, :read_expired]
My problem can also be solved by adding action: :read
MyApp.Streams.read(MyApp.Streams.Event, action: :read)
MyApp.Streams.read(MyApp.Streams.Event, action: :read)

Did you find this page helpful?