MikeyBower93
Balancing Elixir Context Design with Flexible Web API's
Recently I have been using elixir phoenix contexts to structure my software development projects to allow for a clean/cohesive/reusable code base which works really well and makes a lot of sense. However I have trouble reconciling this with creating a flexible and open web API for front ends to use, whether that standard be JSON API, GraphQL etc.
To further understand this problem, let me give you an example, imagine you have a Users context with an exposed function called list_users which might look like following:
defmodule App.Users do
def list_users() do
Repo.all(User)
end
end
and then imagine that you have a Web API controller that uses that code like the following:
defmodule AppWeb.UserController do
def index(conn, _params) do
users = App.Users.list_users()
json(conn, users)
end
end
This is absolutely fine and works great. However for rich client side applications its often the case that you will need a lot of flexibility over the data that is returned, for example in a couple of projects I have worked on we use JSON API and allow for filtering, nested filtering (in our example imagine a user is associated to a company and you want your user listing filtered by a company name), sorting and pagination.
As soon as we get into this realm things start to get tricky in my opinion, because we want to keep the context and the list function isolated without exposing too many details of the inner works (in this circumstance it’s using ecto), however we now need to support multi layers of functionality for the listing of users if we want our Web API to allow for a rich level of functionality. If you try and keep that within the context function you might end up with the following:
defmodule AppWeb.UserController do
def list_users(params) do
filter_params = Map.get(params, :filters)
pagination_params = Map.get(params, :pagination)
sort_params = Map.get(params, :sort)
# ...reduce over filter params and use ecto composability to build the query up
# perhaps do some pattern matching on the sort_params, if non has been passed through
# then do a regular Repo.all, otherwise can run limit and offset etc.
end
end
All of sudden that function has become very general and very abstract in what it can and can’t do. Not to mention that we don’t want the context function to know if its JSON API, GraphQL etc calling it, so we will likely end up formatting the request parameters into a general format that can be understand by the contexts (support for like, greater than, less than etc). This is crucial because if another part of the application such as a background job needs to do call the list_users it won’t be natural to pass parameters to it in the form of a JSON API request etc.
It feels like we have to put a lot of plumbing in to create that separation between the 2 whilst having that rich web API for the client, to the point we have needed to define a query language to pass to the list function (we would also need to create a layer that converts the request params into the form).
This approach starting to seem so overblown that a few months back I ended up writing a library that takes in request parameters in the JSON API format and creates an ecto query that you can execute which leverages ecto named bindings (see GitHub - MikeyBower93/json_api_ecto_builder · GitHub). However after reading into more context design/general design principles it does feel like that is essentially coupling your web API to your database and ecto etc.
I would be interested to see what people have done in these circumstances, how people believe you should approach such design considerations.
Thanks,
Mike.
Most Liked
baldwindavid
I use the following pattern…
# context
def list_users(queries \\ & &1) do
from(User)
|> queries.()
|> Repo.all()
end
# controller
Accounts.list_users(fn query ->
query
|> Accounts.include_user_profile()
|> Accounts.filter_non_executive_users()
|> Accounts.filter_users_by_company(company)
|> Accounts.order_users_by_first_name()
end)
All of the needed queries are publicly exposed from the context. This is very explicit, dependency-free, and dead simple to find the call sites where you might be using a specific query.
I previously experimented with a more dynamic method via a package, but found it to be a little more difficult to maintain and more magic than needed. That dynamic method is wrapped up in the TokenOperator package. There is a good bit of discussion on some of the things you mention in the thread for TokenOperator and another for the QueryBuilder package.
baldwindavid
I’ve tried to avoid importing in a lot of cases for explicitness/searchability, but import is totally an option to slim down those queries.
mbuhot
When you have the requirement for flexible queries, but want to use contexts for well defined update operations, then a CQRS approach might be worth considering.
By having separate code paths for queries and commands, you can optimise each along different dimensions.
You can start with separating the controllers for the index and show actions. Those will be primarily concerned with composing the appropriate ecto query to serve the request efficiently.
The controller for the other actions would be primarily concerned with validating parameters, possibly building a struct that represents the command and passing that into your context module.
Unfortunately CQRS is often introduced in the context of event sourcing, but I think it can be valuable on its own.
Last Post!
edisonywh
Yup like I pointed out in the README, it’s basically a glorified Enum.reduce with some slight difference, some examples being:
- with Enum.reduce your reduction runs in the order user specified
- you get some niceties like validating the options user passed in is valid
- I’d argue that the API is a bit better (your function retains the same shape, rather than having to rearrange everything to fit
Enum.reduce)
The package itself is very small!
For now I’ve ported my app http://slickinbox.com/ to use Condiment and I really like it
Popular in Discussions
Other popular topics
Latest Phoenix Threads
Chat & Discussions>Discussions
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









