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?
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:
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.