Andrew-Bekhiet

Andrew-Bekhiet

set_attribute(:line, arg(:line)) sets to nil when argument is not provided

When using change set_attribute(:line, arg(:line)) and using input: {} in the graphql api, the line attribute is set to nil and persisted as null in the database, which erases existing field data
I expected that it would stay the same as the documentation says:

Use arg(:argument_name) to use the value of the given argument. If the argument is not supplied then nothing happens.

I have to pass it this way to convert from geo_json to line_string, which uses AshGeo to map to PostGIS line string type

The only workaround I found is this

change fn changeset, _context ->
  if Map.has_key?(changeset.arguments, :line) do
    line = Changeset.get_argument(changeset, :line)
    changeset |> Changeset.change_attribute(:line, line)
  else
    changeset
  end
end

But I think there might be a better way to do it, or I might be missing something

Here is the model code:

defmodule MyProject.Geography.Street do
  @moduledoc false
  alias Ash.Changeset
  alias MyProject.Geography

  use Ash.Resource,
    domain: MyProject.Geography,
    data_layer: AshPostgres.DataLayer,
    extensions: [AshGraphql.Resource]

  graphql do
    type :streets
    ...

    mutations do
      create :create_street, :create
      update :update_street, :update
      ...
    end
  end
  ...

  actions do
    defaults [:read, ...]

    update :update do
      accept :*
      primary? true

      argument :line, :geo_json
      
      change set_attribute(:line, arg(:line))
    end
  end

  attributes do
    uuid_v7_primary_key :id
    attribute :name, :string, allow_nil?: false, public?: true
    attribute :line, :line_string, public?: true
    ...
  end
end

defmodule MyProject.Type.LineString do
  use AshGeo.Geometry,
    storage_type: :"geography(LineString,4326)",
    geo_types: :line_string

  def graphql_input_type(_), do: :json
  def graphql_type(_), do: :json
end

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

Ah, I’ve fixed the docs. It just doesn’t work that way, and a custom change is likely the best way. You can do change set_attribute(:attribute, arg(:value), set_when_nil?: false) to ignore nil values, but that would prevent actually setting to nil.

Where Next?

Popular in Questions Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement