wolfiton

wolfiton

How to use get_in to retrieve Item name from [%{"id" => "1849", "name" => "Reuben"}]

Hi everyone,

How can i retrieve the name from a structure like this?

 %{"id" => "1570", "name" => "Croque Monsieur"}

My test looks like this

describe "GetMenuItems.gql == matching" do
    test "Should return all Menu items (1 of them)" do
      result = query_gql(variables: %{"matching" => "reu"})
      assert {:ok, %{data: %{"MenuItems" => menu_items}}} = result
      IO.inspect(menu_items)

      assert length(menu_items) == 1

      assert menu_items == [%{"name" => "Reuben", "id" => 1471}]
    end
  end

What i tried:

name = get_in(menu_items, [:name])

name = get_in(result, [:data, "MenuItems", "name"])

So can someone explain to me the correct way to do this?

Thanks

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

"name" is a string, :name is an atom.

On a larger note though, I really don’t recommend using get_in like this to test API responses. The reason is that if things don’t go as you expect, either because you have a bug in your test code, or because your API isn’t returning what you expect, the error messages don’t tell you anything.

I always test my GraphQL apis using a combination of = and == eg:

assert {:ok, %{data: %{"MenuItems" => menu_items}}} = result
assert menu_items == [%{"name" => "Reuben", "id" => 1471}]

This is so much better than using get_in because if result doesn’t match, the error message will tell you what result actually is. Then if the name isn’t "Reuben", once again the failure message will show you the whole thing.

Eiji

Eiji

If you have a list of menu items then you need to use Access.at/1.

ityonemo

ityonemo

Access.at is zero indexed.

ityonemo

ityonemo

Maps are special in pattern matching; you can ignore id as well by doing this:

%{"name"=>"foo"} = my_map

The difference being Ben’s match enforces the presence of an id field but not its value, and mine enforces neither.

Aduril

Aduril

As @Eiji mentions a list would require Access.at/1

The other way would be to use get_in with a function as key (see
Kernel — Elixir v1.19.3 )

If you always expect exactly one result at this point you can also do it like this:

Hope I helped you :slight_smile:

Where Next?

Popular in Chat/Questions Top

xgilarb
Hi there, I’m interested in using Elixir because of the rumors about the reliability of the Phoenix framework, and surprisingly, Elixir’...
New
Scoty
Hey, I am currently reading Programming Elixir and I am doing one of the exercises where you should write a solution to the “I’m thinkin...
New
shansiddiqui94
Hello all, I recently did my first app in Phoenix and Liveview, many thanks to all the users who assisted me. I found that the tutorial ...
New
Yoga
Or at least, in the works? All I can find are bits and pieces but not a single project from start to finish. Including things like i18n,...
New
New
Allyedge
Hey, I want to learn Elixir OTP and I wanted to know if there are any good resources that teach it. I found some web pages, but none of t...
New
maqbool
what books/Resources do you recommend to learn about distributed system(theory)?
New
Nopp
Hey guys and girls, i am completely “new” to programming, recently played a bit with Python, Ruby and PureBasic, but i want to try somet...
New
AstonJ
It finally feels like I’ve got some time to catch up on my reading - though I am sure lots of people will be wondering the same: what are...
New
Fl4m3Ph03n1x
Background Hey guys, recently I bought a book on TDD that I am reading. The books is really nice and has some really juicy things on arch...
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement