tme_317

tme_317

Setting boundaries between resolvers/controllers and contexts

Still struggling a bit with patterns for context boundaries and hoping for some advice.

I frequently have code like this in my Absinthe resolvers for mutations (although the pattern would be exactly the same if it were a standard REST controller):

  def password_reset(_, params, _) do
    with {:l1, {:ok, _recaptcha_response}} <- {:l1, Recaptcha.verify(params.recaptcha)},
         {:l2, {:ok, username}} <- {:l2, Accounts.username_or_email_to_username(params.username_or_email)},
         {:l3, %User{} = user} <- {:l3, Accounts.get_user(username)},
         {:l4, {:ok, reset_token}} <- {:l4, Accounts.create_password_reset(username)} do
      Email.send_password_reset_email(user.email, reset_token)
      {:ok, %{errors: []}}
    else
      {:l1, _error} ->
        {:error, "Recaptcha Error"}
        # All other else clauses go here
    end

Of course this works great but I’ve this nagging feeling that most of this logic should exist in Accounts.create_password_reset/1 and only checking the recaptcha should stay in the resolver/controller since Recaptcha is a different app/dep and a web layer concern. It seems to make sense that the resolver/controller be the glue that calls different contexts to keep the contexts from being coupled to each other but calling Accounts 3 times here is wrong.

Maybe the resolver/controller should be like this:

  def password_reset(_, params, _) do
    with {:l1, {:ok, _recaptcha_response}} <- {:l1, Recaptcha.verify(params.recaptcha)},
         {:l2, {:ok, {email, reset_token}}} <- {:l2, Accounts.create_password_reset(params.username_or_email)} do
      Email.send_password_reset_email(email, reset_token)
      {:ok, %{errors: []}}
      # ... else clauses go here

Since this resolver is the only consumer of this context API I can foresee does it even matter or is this all just personal preference? Or am I missing something? Thanks!

Most Liked

xpg

xpg

I don’t have a definitive answer for you. My approach is to start with having the code in the controller, and if I need similar functionality somewhere else, I refactor it into existing context modules or utility modules.

I generally apply this pattern when writing my code: Write the logic where I need it, and move it if it is beneficial (easier to read or allows sharing of logic).
This approach allows me to quickly get the logic written instead of trying to design for future use-cases.

But obviously, this is just my personal preference.

Where Next?

Popular in Questions Top

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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement