gdub01
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?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
Hey there, I’m glad you’re using this library!
Let’s take each of your questions in turn.
Testing
The overall approach here is a very common way to do integration level testing. You may also find that the functions you build to handle the resolvers need unit testing if they’re more complex, but often complexity there gets extracted into service type functions that you’d want to unit test anyway.
A couple of things however would make the testing code you have a bit more idiomatic. Most of these things are phoenix conn test related, there isn’t much absinthe specific about this method of testing.
|> post("/api", query_doc)instead of directly setting:body_paramsString.contains?. This is particularly important because GraphQL does not use HTTP status codes to indicate errors that may happen on a given field. So for example suppose youraccountfield returned an error"No account found for email hey@you.com". Your tests would actually pass right now, but clearly there’d be an error.There’s also a few minor things about the testing here that are a bit confusing. Where does the
connpart ofinfo.conncome from? What content type header is being set? Why is the:hostvalue being set? Are you using phoenix or just bare plug.Error handling
It’s definitely common to want to handle changeset errors in a generic way, and definitely noto something you want to have to call explicitly in your resolvers over and over again.
The best solution at the moment is to build a wrapper function that handles this possible return value from the resolver function its wrapping. Here’s an example:
Now all that you have to do is wrap resolvers where you want changesets to be handled in a
handle_errorscall and you’re good to go. Having to still manually placehandle_errorsthroughout your schema is a bit of an annoyance as well, and so we’re working to finalize a middleware pattern that will let you apply this pattern in an even more generic way. Until then, wrapper functions are the way to go.gdub01
Thanks for the answers @benwilson512! They were both very helpful =)
I’ll use that wrapper function and I’m going to have another go at that test to clean up the naming problems and confusing code. Thanks for pointing those things out as I wasn’t sure how far off I was from the right track.
gdub01
Okay - I’ve revised the test. Here’s what it looks like now if anyone is interested. Thanks again @benwilson512
cultofmetatron
this solution worked well for me based on brian’s answer
tosbourn
I just wanted to say thanks to @gdub01 for asking about this and the answers that came from it. I am incredibly new to Elixir, GraphQL, and Absinthe and this helped a lot.
I’ve written up what I did in order to test both queries and mutations. Testing Absinthe with ExUnit.
The main difference is that the query param you send needs to look slightly different, these are the two helpers I came up with based on what was shared here before;
law
This is cool
I havn’t written any integration tests at the phoenix level for my graphql endpoint so this’ll be helpful when I get there.
Self Plug: I test my queries one level down using Kronky
So like
benwilson512
I highly recommend making use of variables within test, at least for any complicated input. It means you don’t have to worry about escaping stuff within the query string.
aptinio
First of all, thank you @benwilson512 and the Absinthe team for your work!
What do you think about bypassing
connusingAbsinthe.runlike so?:benwilson512
It’s an option, but I’m not sure it really brings anything to the table. If you want something more like a unit test I’d focus on unit testing whatever business logic function gets called within specific resolvers. For integration level tests this leaves out stuff that can be important like authorization, or any custom phases specified on the plug.
aptinio
Good points. Thank you so much!