florid-sojourner

florid-sojourner

Ash Resource Change behavior differs between REPL and GraphQL mutation

I get different behavior when using the REPL to do a resource update that includes and after_action when I issue the same change via a GraphQL mutation.

On my resource:

update :complete do
      ... redacted ...
      change after_action(&PhaseComplete.send/3)
    end

The change code:

def secondary_action(changeset, record, _context) do
    flow_phase =
      Ash.get!(MyResource, changeset.data.id,
        load: [:second_resource, third_resource: [:fourth_resource, fifth_resource: [:sixth_resource]]]
      )

    ... redacted ...
    
    {:ok, record}
  end

In the REPL is simply succeeds:

Ash.get!(MyResource, "c86e8eb3-70a7-4537-8628-868d908031fd")

and then

|> MyResource.complete

I get the expected info logs that the secondary action completed.

But when using the GraphQL mutation I get:

[debug] QUERY OK db=0.1ms
rollback []
↳ Ash.Actions.Update.Bulk.run/6, at: lib/ash/actions/update/bulk.ex:243
[error] 3b72b806-0492-42a4-b101-00020723c988: Exception raised while resolving query.

** (KeyError) key :id not found in: %Ash.Changeset.OriginalDataNotAvailable{reason: :atomic_query_update}

We’ve already changed the syntax we were using and solved the problem but wanted to report this so you can either confirm that it should NOT be written the original way shown at the top of this thread, or if it’s a defect, that it can be corrected.

New syntax used:

def send(changeset, record, _context) do
    my_resource =
      record
      |> Ash.load!([:second_resource, third_resource: [:fourth_resource, fifth_resource: [:sixth_resource]]])

    ... redacted ...
    
    {:ok, record}
  end

First Post!

zachdaniel

zachdaniel

Creator of Ash

This…is actually pretty interesting. The problem should exist only with after_action/1 I believe. If you changed it to:

change fn changeset, _ -> 
  Ash.Changeset.after_action(changeset, &PhaseComplete.send/3)
end

does it work?

Last Post!

zachdaniel

zachdaniel

Creator of Ash

Right, so in this case to make it atomic:

defmodule SendPhaseComplete do
  use Ash.Resource.Change

  def change(changeset, _, context) do  
    Ash.Changeset.after_action(changeset, &PhaseComplete.send(&1, &2, context)
  end

  def atomic(changeset, opts, context) do
    {:ok, change(changeset, _, context)}
  end
end

Then you can refer to it as change SendPhaseComplete (giving it a better module name that makes sense for you.

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement