axelson

axelson

Scenic Core Team

I am working on an application of mid to high size and complexity, it is an umbrella application with at least 3 main applications (but you could think of them as Phoenix Contexts if you wish). There is an existing client-facing JSON API, a little pure HTML rendering, some websockets, and an internal API (for another server to interface with us). Currently data-loading and permissions checking happens in an ad-hoc manner. As we are just starting to add GraphQL support with Absinthe I’ve been thinking of using Dataloader throughout the various applications and contexts. Does anyone have familiarity with this type of approach?

I have been fairly deeply impacted by the philosophy espoused by Dan Schafer in this GraphQL talk: https://www.youtube.com/watch?v=etax3aEe2dA

So my goals are to have entities always loaded with respect to the current viewer (usually a user) and for that data loading to live in the context of the specific application.

The main approach that has come to mind so far is to create a %UserRequest{} struct with a %Viewer{} struct embedded. The %Viewer{} would have typically have a normal %User{} associated with it but sometimes the user would be a :super_admin (i.e. me) which would have extra permissions and it might even be :other_internal_system. The %UserRequest{} struct would additionally contain the %Dataloader{} that could be used to fetch entities that the user is allowed to see.

So my context/application data fetching functions would look something like this:

defmodule Blog do
  @spec fetch_post(binary, %UserRequest{}) :: {:ok, {%Post{}, %UserRequest{}}} | {:error, term}
  def fetch_post(id, %UserRequest{loader: loader, viewer: viewer}) do
    ...
  end

  @spec fetch_posts(list(binary), %UserRequest{}) :: {:ok, {list(%Post{}), %UserRequest{}}} | {:error, term}
  def fetch_posts(ids, %UserRequest{loader: loader, viewer: viewer}) do
    ...
  end
end

So the context/application data fetching functions would take in the data to query, along with the %UserRequest{} (which now that I think of it should perhaps be called a %UserRequestContext{} or maybe just %RequestContext{}). And along with fetching and returning the actual data, they would potentially load new data into the %UserRequest{} data structure which is the reason that the %UserRequest{} needs to be returned from the data fetching method as well.

This would allow all of the data fetching methods to have a common format for permission checking and avoid loading the same data multiple times (note that the permission checking will sometimes have to load data specific to the user).

Does this approach make sense? Are there better alternatives that wouldn’t require re-writing so much of the data-fetching logic? Should I instead move the dataloader to the Process dictionary (which is effectively the approach taken by the javascript dataloader)? @jfrolich I’m especially interested in your thoughts since I think your talk (which I’m about to watch) and PR are related to this topic.

Showing Posts 1 to 10

jfrolich

jfrolich

Definitely makes sense to me, in my application the contexts indeed take a user as the last argument for authorization.

It’s not really clear to me how including %UserRequestContext{} as the result of the function solves the dataloading/n+1 problem.

That is indeed exactly the problem I’m trying to solve with the PR. Interested what you think about the talk and the approach taken in the PR. Unfortunately I didn’t have much time to work on this. But help is welcome, I think it’s a pretty important element for more complex GraphQL apps.

PS: using the PR you don’t need to pass the loader, so you can just use the viewer as the last argument. Only in the graphql the deferrable is resolved using the dataloader. The deferrables are as much as possible resolved at the same time, so batching, caching and parallelization is applied.

axelson

axelson OP

Scenic Core Team

It doesn’t really solve all of it. What it solves is the avoidance of re-fetching already fetched data (e.g. when checking permissions across multiple fields). What it gives up is effecient batching because the first time you call a method like Blog.fetch_posts/2 it would call Dataloader.run. Basically I’m trying to work out a concept for fetching data using dataloader’s existing semantics and what I’m coming to realize is that it falls short in a few key areas.

I do think that the approach is very interesting and has the potential to get us to where we want to go but I feel like that the semantics are awkward and feel at odds with the nature of Elixir. Of course part of that is a knee-jerk reaction because I have not actually tried using your PR.

That is interesting and does in fact make the interface much easier to use (passing something like a %UserRequestContext{}) all over the place could definitely feel like needless boilerplate (and possibly error prone). But isn’t the relationship between the deferrable and the dataloader a hidden coupling? How do you ensure that the deferrable is run against the correct dataloader? Or against a dataloader with the correct source?

What if instead of introducing the concept of Deferrable we used a process? Imagine a DataloaderServer (that might be a GenServer). It would receive messages that ask it to fetch data and also messages that would ask it to return data that it has already fetched. So your context/application module might have a function that looked like this:

defmodule Blog do
  @spec load_posts(list(id), %Viewer{}, dataloader_server_pid) :: (() -> {:ok, %Post{}} | {:error, term})
  def load_posts(ids, _viewer, dataloader_server_pid) do
    DataloaderServer.load(dataloader_server_pid, {Blog, :post, id})
    fn ->
      DataloaderServer.get(dataloader_server_pid, {Blog, :post, id})
    end
  end
end

As long as not all of the code is immediately calling the 0-arity functions that it receives than the DataloaderServer can do appropriate batching (although it may need to use a mechanism like selective receive so it can batch together all of the load’s before responding to any get’s).

While this does have some similarities to your Deferrable concept I do think that the API is more inline with typical Elixir semantics.

jfrolich

jfrolich

Ah I see that it can help with caching. Batching is the most important thing to do because that is will solve the N+1 problem. Caching is just a small optimization.

Regarding solving it with processes. That is not easily possible because Elixir is lacking an event loop (which is a plus, but makes these things harder). Anyway there might be an implementation possible that solves the issue in a way I didn’t think of, interested to hear about other approaches.

You can also use Deferrables without the syntax sugar (calling Defer.then(fn ... end)), but then you have to deal with callbacks. Might be more explicit and the Elixir way.

I do think deferrables are a pretty good compromise, lazy data structures are common in functional languages and very powerful. You can compare it to the Stream as another example of a lazy data structure in Elixir.

dimitarvp

dimitarvp

I have read the comments here and Dataloader’s README but I am still not quite sure what is @axelson trying to achieve… can you please elaborate? You immediately started talking about request contexts and I lost the main goal.

jfrolich

jfrolich

You need to have the right sources available when you run the deferrable indeed. But this is the same situation right now if you put the dataloader in the graphql context (need the right sources).

jfrolich

jfrolich

I did a meetup talk about this issue, I think this gives an idea about the high level problem.

Also see the more general talk of Dan Schafer.

In short Absinthe GraphQL wrote a Dataloader module that solves the n+1 problem in loading data. However their approach comes short that you now have to load data in the resolver module instead of your business logic modules.

dimitarvp

dimitarvp

Apologies, I haven’t listened to the talk yet, but can you tell me, from the outset, what does your code bring in as an added value compared to doing Ecto.Multi operations inside the GraphQL endpoints?

I will check out the talk but cannot do it right now.

jfrolich

jfrolich

Solve the N+1 problem. In short if you query a list of users and for each user you would like to query their best friend. You now have N+1 queries that could have been two queries if you would batch the “best friend” queries together.

dimitarvp

dimitarvp

I still don’t understand. In cases like these I have something like this:

defmodule User do
  def with_best_friend(list_of_user_ids) do
    # an elaborate Ecto query here
  end
end

Then your GraphQL endpoint simply calls your convenience function that specifically takes care to avoid the N+1 query problem. Am I missing something obvious?

jfrolich

jfrolich

The dynamic nature of GraphQL makes it hard to hand optimize all N+1 situations.

It’s pretty easily fixed with a batch function in the resolver, or the current Dataloader. So it’s a fixed problem. The only issue is that it would be nicer to have data loading happen at the busines logic layer.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews