wolfiton

wolfiton

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

Showing Posts 1 to 10

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

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews