Hello everyone I’m just new to Elixir, generally new to FP. So basically I try to learn Elixir and afterwards jump to building a graphql api server.
I read and follow the book “Craft GraphQL APIs in Elixir with Absinthe” and stuck at the testing of a graphql query.
Here’s what the book said:
test "menuItems field returns menu items" do conn = build_conn() conn = get conn, "/api", query: @query assert json_response(conn, 200) == %{ "data" => %{ "menuItems" => [ //omitted ] } } end
Here’s what the I attempt , I noticed that in my local machine when I make a query it uses POST method rather than GET method so I change it to post instead:
test "courses field returns courses" do conn = build_conn() conn = post conn, "api/graphiql", query: @query assert json_response(conn, 200) === %{ "data" => %{ "courses" => [ %{"name" => "BSIT"}, %{"name" => "BSCS"}, %{"name" => "BSBA"} ] } } end
Then when I run the test it fails because the query returns empty result.
Assertion with === failed code: assert json_response(conn, 200) === %{"data" => %{"courses" => [%{"name" => "BSIT"}, %{"name" => "BSCS"}, %{"name" => "BSBA"}]}} left: %{"data" => %{"courses" => []}} right: %{"data" => %{"courses" => [%{"name" => "BSIT"}, %{"name" => "BSCS"}, %{"name" => "BSBA"}]}} stacktrace: test/voting_system_web/query/course_test.exs:15: (test)
Then I try testing the query through curl and the query works and returns a result.
curl -X POST -H "Content-Type: application/json" --data '{ "query": "{ courses { name } }"}' http://localhost:4000/api/graphiql {"data":{"courses":[{"name":"BSIT"},{"name":"BSCS"},{"name":"BSBA"}]}}%
I’m sorry if this is a noob question but I want to learn so maybe anyone can help me. thank you.