crisdegraciadev

crisdegraciadev

Help on testing with Absinthe

Hey guys,

I’m relatively new to Elixir world. Currently I’m working on a GraphQL API with Absinthe. All is going right, I have done some logic, some tests to the business logic layer, etc.

Now I’m trying to test my web layer (resolvers), but every time I’m stuck getting this response every time I try to make a request from ExUnit.

%{
  "errors" => [
    %{
      "locations" => [%{"column" => 1, "line" => 1}],
      "message" => "Parsing failed at `--plug_con`"
    }
  ]
}

The app works just fine from Postman, here it’s my test file

defmodule Test.Web.Resolver.Session do
  alias Mix.Tasks.Phx.Routes
  use Test.Setup.ConnCase, async: true

  @login_mutation """
  query GetUser($id: ID!){
    user(id: $id) {
        id,
        email,
        firstName,
        posts {
            id,
            title
        }
    }
  }
  """

  setup %{conn: conn} do
    :ok
  end

  test "prueba", %{conn: conn} do
    conn =
      post(conn, "/api/graphql", %{
        "mutation" => @login_mutation,
        "variables" => %{id: 1}
      })

    IO.inspect(conn)
    IO.inspect(json_response(conn, 200))
  end
end

Any idea why is this happening?

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hi @crisdegraciadev welcome! That is a very strange error, I’ve never seen it before.

There are definitely a few things that are strange about your test though.

  1. You have something called @login_mutation that is a graphql query, not a mutation. It also doesn’t look like it does a login
  2. you are using the "mutation" key for the document, but the document key in the POST body should always be "query" whether it’s a mutation doc or otherwise

Are you able to put your code up somewhere that I can see this happen?

Also Liked

dimitarvp

dimitarvp

It helps future readers if you show how you solved it.

paulstatezny

paulstatezny

I just ran into this as well.

Just to clarify, the problem here was @benwilson512 's (2):

you are using the "mutation" key for the document, but the document key in the POST body should always be "query" whether it’s a mutation doc or otherwise

So this:

post(conn, "/api/graphql", %{
  "mutation" => @login_mutation,
  "variables" => %{id: 1}
})

Should have been this:

post(conn, "/api/graphql", %{
  "query" => @login_mutation,
  "variables" => %{id: 1}
})

Where Next?

Popular in Questions Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
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
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement