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.”

Where Next?

Popular in Questions Top

New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
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
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement