dongwooklee96

dongwooklee96

I want the date to be the key and I want to return the resources in an array, should I use an action to make this possible in Ash?

Hello,

...
attributes do
    uuid_primary_key :id

    attribute :title, :string do
      allow_nil? false
    end

    attribute :date_of_record, :date do
      allow_nil? false
    end

    attribute :date_of_visited, :date
    attribute :pain_of_scale, :integer, constraints: [min: 1, max: 5]
    attribute :memo, :string
    attribute :doctor_opinion, :string

    create_timestamp :created_at do
      private? false
 end
...

I have the above resources defined and I want to group by date_of_visited and get back an array of resources with date as key value. How can I do this?
Additionally, we’d like to see pagination applied as well.

expected:

{
  "2024-01-02": [{resource}, {resource}, ....],
  "2024-03-02": [{resource}, {resource}, ...]
}

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

Yes, sorry about that :slight_smile: I was at ElixirConf EU, so didn’t have as much time to expand on my forum post answers.

Given this type:

defmodule Dentallog.Types.GroupByDateDiary do
  use Ash.Type.NewType,
    subtype_of: :map

  use AshGraphql.Type

  @impl AshGraphql.Type
  def graphql_type(_), do: :group_by_date_diary
end

you could then use it in your action

action :group_by_date_of_visited, Dentallog.Types.GroupByDateDiary do
    run fn input, _context ->
      {:ok, group_by_date_of_visited()}
    end
end

Also Liked

chef

chef

You’ll probably want to combine a generic action with an optional paginated read action. You can pass in your pagination parameters into the read action and then transform those results into the desired format.

zachdaniel

zachdaniel

Creator of Ash

You can use Ash.Type.NewType to create a new type of map, and then you can use def graphql_type(_), do: :your_type

dongwooklee96

dongwooklee96

It works! :grinning:
You’re right, I did think you’d be busy with ElixirConf EU 2024.
Thanks as always for your hard work.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43806 214
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39523 209
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New

We're in Beta

About us Mission Statement