wolfiton

wolfiton OP

Hi everyone,

Can’t find any error map in wormwood

My test works but I can’t test an error

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

      IO.inspect(menu_items)

      assert length(menu_items) == 0
    end
  end

This all I have

{:ok, %{data: %{"MenuItems" => []}}}

Am I missing something?

Thanks

First 10 of 19 Posts Switch mode

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Why should that return an error? 0 results isn’t an error. Additionally, a graphql query will only return an error if there is a syntax / validation issue with the query itself, or if you return an error from the resolver. If you aren’t doing that, it won’t error.

wolfiton

wolfiton OP

Thank you

I was trying to rewrite this test in Wormwood:

@query """
  {
    menuItems(matching: 123) {
      name
    }
  }
  """
  test "menuItems field returns errors when using a bad value" do
    response = get(build_conn(), "/api", query: @query)
    assert %{"errors" => [
      %{"message" => message}
    ]} = json_response(response, 400)
    assert message == "Argument \"matching\" has invalid value 123."
  end

And i don’t see any possibilities at the moment to do so.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

You didn’t pass in the same value. You passed in "123" (a string), but the wormwood example uses 123 (an integer).

wolfiton

wolfiton OP

You are right. Now I will match it with the error I am getting. Thanks

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

FYI this line doesn’t do anything. You’re just binding a value to a variable, which cannot fail.

wolfiton

wolfiton OP

So now after rewriting this test

describe "GetMenuItems.gql == error" do
    test "Should return error Menu items (error)" do
      result = query_gql(variables: %{"matching" => 123})
      IO.inspect(result)

      assert {:ok, %{"errors" => [%{"message" => message}]}} = result

      assert message = [%{"message" => "Argument \"matching\" has invalid value $matching."}]
    end
  end

I don’t get a pass

I was trying to pattern match on the error message like in the previous tests but no luck.

Stack trace:

mix test test/plate_slate_web/schema/query/menu_items_test.exs

...{:ok,
 %{
   errors: [
     %{
       locations: [%{column: 0, line: 4}],
       message: "Argument \"matching\" has invalid value $matching."
     }
   ]
 }}


  1) test GetMenuItems.gql == error Should return error Menu items (error) (PlateSlateWeb.Schema.Query.MenuItemsTest)
     test/plate_slate_web/schema/query/menu_items_test.exs:31
     match (=) failed
     code:  assert {:ok, %{"errors" => [%{"message" => message}]}} = result
     right: {:ok,
             %{
               errors: [
                 %{
                   locations: [%{column: 0, line: 4}],
                   message: "Argument \"matching\" has invalid value $matching."
                 }
               ]
             }}
     stacktrace:
       test/plate_slate_web/schema/query/menu_items_test.exs:35: (test)



Finished in 1.1 seconds
4 tests, 1 failure

Randomized with seed 419754
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I’m happy to keep helping you, but there are some Elixir basics here you seem to be struggling with that are making this a lot harder than it needs to be. Look carefully at the error message. Look at the exact data types between what you have what you are asserting on the left, and what the value of result is on the right. In particular, pay attention to what values are strings and what values are atoms.

Based on the content of your error messages, it looks like you’re working through the Absinthe book. This is a great way to learn absinthe. However, since you’re very new to this, I don’t think it’s a good idea to try to BOTH work through a new book AND utilize a testing library that isn’t used in the book. There are subtle but important differences between how wormwood tests work and how regular ExUnit test work, and those differences will only increase as you go further into the book and start handling things like authentication headers. I would ditch wormwood for now, and revisit it later after you have a solid grasp of how Absinthe and Elixir testing works.

wolfiton

wolfiton OP

I appreciate the recommendation but when i used the tests in the book I run only into errors and erratas like this one

  • Reported in: P1.0 (31-Mar-19)
  • Fixed: 28-May-19, awaiting book release

#84808
PDF page: 36

@query “”"
{
menuItems(matching: 123) {
name
}
}
“”"
test “menuItems field returns errors when using a bad value” do
response = get(build_conn(), “/api”, query: @query)

assert %{
“errors” => [
%{“message” => message}
]
} = json_response(response, 400)

assert message == “Argument "matching" has invalid value 123.”
end

$ mix test test/plate_slate_web/schema/query/menu_items_test.exs
..

  1. test menuItems field returns errors when using a bad value (PlateSlateWeb.Schema.Query.MenuItemsTest)
    test/plate_slate_web/schema/query/menu_items_test.exs:76
    ** (RuntimeError) expected response with status 400, got: 200, with body:
    {“errors”:[{“message”:“Argument "matching" has invalid value 123.”,“locations”:[{“line”:2,“column”:0}]}]}
    code: } = json_response(response, 400)
    stacktrace:
    (phoenix) lib/phoenix/test/conn_test.ex:373: Phoenix.ConnTest.response/2
    (phoenix) lib/phoenix/test/conn_test.ex:419: Phoenix.ConnTest.json_response/2
    test/plate_slate_web/schema/query/menu_items_test.exs:83: (test)

Finished in 0.2 seconds
3 tests, 1 failure–Conrad Taylor

Ben Wilson says: This happened because you upgraded absinthe_plug which has slightly different behaviour with respect to status codes. Using the version specified in the book lock file will ensure that the book code works.

I don’t ewnat to complain i just searched for a solution for myself.

Thank you a lot for all the suport and patience in guiding me with this.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

The answer I supply in the errata is the correct one to solve your problem. Did you try using the exact mix.lock file from the book code?

wolfiton

wolfiton OP

I wanted to use the latest, because that is what i will use in production and wormwood helped me to use the latest until now.

Where Next? Top

Trending in Chat/Questions Top

krisleech
Hey folks, I’ve been using Elixir for over a couple of years (phx, ecto, broadway, oban). For this kind of work you do not tend, in my e...
New
mhindujadheerajsudan
Hi, I’m Dheeraj Sudan from the UK. I’m a software developer and also run a business with my wife Meenu Hinduja. I’m interested in getting...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement