rafbgarcia

rafbgarcia

How to pass Repo for a package to use?

I’m trying to do this:

Main app has the database.

A package will hold a GraphQL API and its resolvers that use that database.

How can I accomplish that?

Thanks!

Marked As Solved

nbap

nbap

Hi @rafbgarcia,
you may specify the Repo on a configuration key in your app config file.

e.g

In the main app config.exs

config :your_package,
  repo: MainApp.Repo

In your package

defmodule YourPackage do
  def repo() do
    Application.get_env(:your_package, :repo)     
  end
end

And reference the repo anywhere in your package like this

YourPackage.repo

I guess that will do but of course, you can tweak a little to adapt it to your needs.

Also Liked

mgwidmann

mgwidmann

What you should do is have your Ecto schemas and GraphQL resolvers in the same package with the Ecto Repo, but don’t add it to your supervision tree. In order to do that you’ll have to configure the :otp_app part so your Ecto schema/GraphQL package’s repo. This would mean your non-main package would just define schemas, resolvers and a repo but wouldn’t start them or do anything (they’re just plain modules then). Be warned though, compilation will fail if you do not configure it! (i.e. in tests or other areas) This would look something like:

defmodule API.Repo do
  @otp_app Application.get_env(:api, :otp_app)
  use Ecto.Repo, otp_app: @otp_app, adapter: Ecto.Adapters.Postgres
  use Absinthe.Ecto, otp_app: @otp_app
end

Then in your main app do the following in config:

config :api, otp_app: :main_app

# Config for Ecto repos & database connection here
config :main_app, ecto_repos: [API.Repo]
config :main_app, API.Repo,
  # ...

Finally in your main app supervision tree:

def start(_type, _args) do
  children = [
    API.Repo,
  ]

  # ...
end
rafbgarcia

rafbgarcia

I managed to get around that using a FakeRepo lol.

defmodule Enderecify.FakeRepo do
  use Ecto.Repo,
    otp_app: :enderecify_api,
    adapter: Ecto.Adapters.Postgres
end

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
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
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
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
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

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
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
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement