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

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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
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
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
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
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

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
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
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
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
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
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New

We're in Beta

About us Mission Statement