wolfiton

wolfiton

Craft graphql api with Absinthe book- Subscriptions chapter 6 place orders

Hi everyone,

So following the absinthe book I arrived at the subscription chapter with the ordering system powered by embedded schemas.

My problem is that cast/4 is deprecated but I couldn’t find any example of cast_embed/3

My Order schema looks like this:

defmodule PlateSlate.Ordering.Order do
  use Ecto.Schema
  import Ecto.Changeset
  alias PlateSlate.Ordering.Order

  schema "orders" do
    field :customer_number, :integer, read_after_writes: true
    field :ordered_at, :utc_datetime, read_after_writes: true
    field :state, :string, read_after_writes: true
    embeds_many :items, PlateSlate.Ordering.Item
    timestamps()
  end

  @doc false
  def changeset(%Order{} = order, attrs) do
    order
    |> cast(attrs, [:customer_number, :items, :ordered_at, :state])
    |> cast_embed(:items)
  end
end

My Item schema looks like this:

defmodule PlateSlate.Ordering.Item do
  use Ecto.Schema
  import Ecto.Changeset

  embedded_schema do
    field :price, :decimal
    field :name, :string
    field :quantity, :integer
  end

  def changeset(item, attrs) do
    item
    |> cast(attrs, [:price, :name, :quantity])
    |> validate_required([:price, :name, :quantity])
  end
end

Error trace:

mix test test/plate_slate/ordering_test.exs                                                                   1 ✘  at 17:23:35 


  1) test orders create_order/1 with valid data creates a order (PlateSlate.OrderingTest)
     test/plate_slate/ordering_test.exs:13
     ** (RuntimeError) casting embeds with cast/4 for :items field is not supported, use cast_embed/3 instead
     code: assert {:ok, %Order{} = order} = Ordering.create_order(attrs)
     stacktrace:
       (ecto) lib/ecto/changeset.ex:545: Ecto.Changeset.type!/2
       (ecto) lib/ecto/changeset.ex:519: Ecto.Changeset.process_param/7
       (elixir) lib/enum.ex:1948: Enum."-reduce/3-lists^foldl/2-0-"/3
       (ecto) lib/ecto/changeset.ex:504: Ecto.Changeset.cast/6
       (plate_slate) lib/plate_slate/ordering/order.ex:17: PlateSlate.Ordering.Order.changeset/2
       (plate_slate) lib/plate_slate/ordering.ex:56: PlateSlate.Ordering.create_order/1
       test/plate_slate/ordering_test.exs:24: (test)



Finished in 0.1 seconds
1 test, 1 failure

Also, I don’t have any embeds in my test file, so the error is definitely coming from my schemas, just wondering what is the correct way to define cast_embed/3

Because I think that how it is now it should be correct.
Thanks in advance

Most Liked

fuelen

fuelen

Hello!
Just remove :items from the list of attributes on cast call.

Adzz

Adzz

I think the error makes sense if you know what it’s saying.

Items is an embed, and the error is telling you you tried to cast an embedded field in cast/4, which is not allowed.

Even though you did a cast_embed after, it hits this error first.

Where Next?

Popular in Chat/Questions Top

maz
I’m getting this error: ## ** (Ecto.Query.CompileError) Tuples can only be used in comparisons with literal tuples of the same size fro...
New
loganhelms
A while back, I read a great book by Luis Atencio titled, Functional Programming in JavaScript. In section 7.3, he discusses memoization ...
New
mchean
I was wondering if anyone is using some learning tools that they can recommend? I’ve been looking at two specifically so would also be ...
New
ericdouglas
I think that would be really interesting to have official books created by the community about all kinds of development we can do with El...
New
Fl4m3Ph03n1x
GenStage and Flow resources? I have been hearing about GenStage and Flow, a tool built upon it, for quite some time. I understand it allo...
New
marciol
Hey, I have very restricted resources and time so I’m trying to understand the best way to learn Liveview in terms of cost/time. The Pra...
New
yachnytskyi
Hello everyone. I am gonna start with Elixir/Phoenix, thinking to use Stephen Grider as a start point, then elixir school and other sourc...
New
dogweather
Can anyone recommend books/courses/videos that use real-world Elixir? E.g.: Idiomatic error handling design, whether it’s {ok/error, .....
New
SavagePixie
I was wondering if there are any beginner-friendly, exercise-based resources for learning Elixir out there. I’m looking for something lik...
New
shansiddiqui94
Greetings Elixir Developers, My name is Daniel, and I am taking my first step to learn all about the Elixir language. Currently I have a...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement