smahi

smahi

How to use ex_machina in umbrella apps

I have two apps restaurant_app and graphql_api_app in an umbrella.
I have ex_machina added as dependency to restaurant_app and properly configured there.
In graphql_api_app I want to use the same Factories from restaurant_app for fixtures.
How can i properly setup and configure my tests in graphql_api_app

Most Liked

cjbottaro

cjbottaro

Who is Dave Lucia? I figured he would be the creator of ex_machina or something, but I check the GitHub page and he’s not on the list of contributors.

I also was not really a fan of using ex_machina, and tried to encourage people to just create records using plain Ecto functions instead… but I recently gave it another shot and it’s a bit easier to create associated records, so I’m giving it a shot again.

So every app in the umbrella just becomes a normal dir under lib in a normal mix project. We have several top level modules; not everything is nested under one module. I found that to be fine so far.

Then in runtime.exs we have sections like this…

if System.get_env("START_FOO") || System.get_env("START_ALL") do
  config :my_app, Foo, start: true
end

if System.get_env("START_BAR") || System.get_env("START_ALL") do
  config :my_app, Bar, start: true
end

And finally in lib/my_app/application.ex

...
children = [
  # things you always want started
]
|> add_if_started(Foo)
|> add_if_started(Bar)
...

defp add_if_started(children, mod) do
  start = Application.get_env(:my_app, mod, [])
  |> Keyword.get(:start, false)

  if start do
    children ++ [module]
  else
    children
  end
end

Then if you just want to startup foo, you can do:

START_FOO=1 mix run

Our original umbrella had over a dozen apps in it, but we rewrote it this way because we had lots of circular dependency problems. Our mix projects were filled with xref exceptions.

Also, it’s much easier to navigate this way. Much less config files and mix project files, etc.

cjbottaro

cjbottaro

I mean, I find it kind of strange to just show a random tweet with zero context of someone saying they don’t like it. I just assumed it was supposed to be someone that I know.

No explanation or anything. Could have at least said something like, “While this question is specifically about how to use ex_machina, you should be aware that a lot of the community doesn’t recommend it.”

thojanssens1

thojanssens1

cjbottaro

cjbottaro

Back when we used umbrella apps, we had like a “base” app that all other apps depended on, which would have common or shared factories. Then each app would depend on that base app and also have their own factories.

We recently collapsed our umbrella app into a single OTP app, and we just use environment variables to determine what processes to start. It much more simple and gets you out of dependency hell, but still lets you scale “services” independently.

cjbottaro

cjbottaro

I don’t use Twitter; it never occurred to me to click on the link. I thought it was just a standalone statement and clicking would just show me the same statement, but on Twitter’s website instead.

But now that I see, yeah, I agree with that (just using Ecto functions) and was my recommendation for a while, but my team still opted to use ex_machina for some reason. And I thought there was something easier about building assocs with it, but looking at the blurb, it doesn’t look any easier at all.

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
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

We're in Beta

About us Mission Statement