I’m not sure if this is the correct way to test an API call in with Absinthe. The code works, but I’m not sure if this is what I should be doing for each query?
@account %{email: "hey@you.com", password: "herewego"}
setup do
{:ok, account} = AccountResolver.create(@account, %{})
{:ok, token} = AccountResolver.login(@account, %{})
{:ok, %{token: token.token, id: account.id}}
end
test "Logged in user should be able to see their email", info do
queryDoc = %{
"operationName" => "account",
"query" => "query account { account (id: #{info.id}) { email } }",
"variables" => "{}"
}
conn = info.conn
|> put_req_header("authorization", "Bearer #{info.token}")
|> Map.put(:host, "localhost:4001")
|> Map.put(:body_params, queryDoc)
|> post("/api")
assert conn.state == :sent
assert conn.status == 200
assert String.contains?(conn.resp_body, "hey@you.com")
end
And in a second, not related question, I am transforming changeset errors into a string to be passed back to Absinthe on error:
defmodule Graphqlapi.ChangesetErrors do
def handle_changeset_errors(errors) do
Enum.map(errors, fn {field, detail} ->
"#{field} " <> render_detail(detail)
end)
|> Enum.join
end
def render_detail({message, values}) do
Enum.reduce values, message, fn {k, v}, acc ->
String.replace(acc, "%{#{k}}", to_string(v))
end
end
def render_detail(message) do
message
end
end
So that in my graphql resolver I have this:
def create(params, _info) do
case Auth.register(params) do
{:ok, account} -> {:ok, account}
{:error, changeset} -> {:error, ChangesetErrors.handle_changeset_errors(changeset.errors)}
end
end
With the idea being I don’t have to write error code twice. Is that a good plan or a bad one?