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

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement