celsobonutti

celsobonutti

Loading filtered relation with Dataloader

I have three models, which are: professors, users and reviews.
In the model files, they are declared as

  schema "reviews" do
    ...
    belongs_to :user, ProfessorEscroto.Users.User
    belongs_to :professor, ProfessorEscroto.Professors.Professor
  end
  schema "professors" do
    ...
    has_many :reviews, ProfessorEscroto.Reviews.Review
    ...
  end
  schema "users" do
    ...
    has_many :reviews, ProfessorEscroto.Reviews.Review
  end

What I want to do is have an field on my Professor GraphQL schema, called myReview, which can get the review the user created to that professor. I could do it but it was making a SQL query for each professor in the response, so I wanted to load it with Dataloader, as I’m doing with every other field.

I could achieve this with the following field in my schema file:

 node object :professor do
    ...
    field :my_review, list_of(:review), resolve: dataloader(Datasource, :reviews, args: %{user: true})
  end

and this in my dataloader file:

  def data(current_user) do
    Dataloader.Ecto.new(Repo,
      query: &query/2,
      default_params: %{current_user: current_user}
    )
  end

  def query(Review, %{current_user: current_user, user: true}) do
    from(r in Review, where: r.user_id == ^current_user.id)
  end

…except that the response is an array. I tried to remove the list_of() from the field in the schema file and modify dataloader to

def query(Review, %{current_user: current_user, user: true}) do
    from(r in Review, where: r.user_id == ^current_user.id)
  end

but it won’t work in any way.

Does anybody have an idea on how to do this?

This is my repo btw, if it helps: https://github.com/CelsoBonutti/professor_escroto

Thank you very much!

Marked As Solved

1player

1player

Bear with me, I use dataloader on my app but it’s wrapped in a few helper functions, so I might be incorrect.

In any case, the dataloader library supports a tuple {:one, queryable or association} as its second argument, to return a single result (Repo.one) instead of a list (Repo.all)

I would try changing the resolve function of my_review to: dataloader(Datasource, {:one, :reviews}, args: %{user: true})

More details on https://hexdocs.pm/dataloader/1.0.7/Dataloader.Ecto.html

Where Next?

Popular in Questions Top

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
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
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 record...
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
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
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
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
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
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
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement