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.

Where Next?

Popular in Chat/Questions Top

sadcad
I love the Phoenix and Elixir docs, but I always tend to learn faster when I watch a video of someone explaining things and then I implem...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
332 30681 154
New
Chawki
hi,i’m new to programming world i had learned front-end( javascript,react.js) and i wanna learn a back-end programming language i thought...
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
miguelsrrobo
hi i was wondering if it is necessary to learn erlang to learn elixir
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
zeroexcuses
Besides https://elixir-lang.org/getting-started/basic-types.html are there any other well recommended “elixir by example” style resources...
New
QueenSvetlana
My bookstore has Introducing Elixir: Getting Started In Functional Programming and the front cover says the book uses Elixir 1.4. The cur...
New
imanuelgittens
Hey all! Just joined the forum and I’m very excited to learn the language. I just finished reading the documentation and I was wondering ...
New
ericmachine88
Hi all, I am currently on this course https://www.ludu.co/course/discover-elixir-phoenix Half a way thru, and struggled a bit.. someti...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
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
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement