kotserubas

kotserubas

Hi All,

I have these objects in graphql scheme:

  import Absinthe.Resolution.Helpers

  object :user do
    field :uuid, :id
    field :email, non_null(:string)
  end

  object :asset do
    field :uuid, :id
    field :name, non_null(:string)
    field :user, non_null(:user) do
      resolve dataloader(Auth, :user)
    end
  end

  def context(ctx) do
    loader =
      Dataloader.new
      |> Dataloader.add_source(Auth, Auth.dataloader_source())

    Map.put(ctx, :loader, loader)
  end

  def plugins do
    [Absinthe.Middleware.Dataloader] ++ Absinthe.Plugin.defaults()
  end

where Auth is Phoenix context:

defmodule App.Auth do
  @moduledoc """
  The Auth context.
  """

  import Ecto.Query, warn: false

  def dataloader_source() do
    Dataloader.Ecto.new(Repo, query: &query/2)
  end

  def query(queryable, _params) do
    queryable
  end

  ...

And this mutation:

  field :create_asset, type: :asset do
    arg :name, non_null(:string)

    resolve &Resolvers.create_asset/3
  end

And these resolver functions:

  def create_asset(_parent, args, %{context: %{current_user: user}}) do
    case Accounting.create_asset(Map.put(args, :user, user)) do
      {:ok, asset} -> {:ok, asset}
      {:error, %Ecto.Changeset{} = cs} ->
        {:error, message: Helpers.format_changeset_errors(cs)}
    end
  end
  def create_asset(_parent, _args, _context) do
    {:error, "not authorized"}
  end

Accounting.create_asset is something like:

  def create_asset(attrs \\ %{}) do
    %Asset{}
    |> ...
    |> put_assoc(:user, attrs.user)
    |> Repo.insert()
  end

So Accounting.create_asset accepts user structure instead of user_id.
That means at the moment asset is successfully created asset.user
is loaded (meta: #Ecto.Schema.Metadata<:loaded, “users”>,).

But when graphql server is processing mutation like:

mutation createAsset {
  createAsset (name: "foobar") {
    uuid
    name
    user {
      email
      uuid
    }
  }
}

three database query is executed:

  1. get current user from auth token. → ok
  2. insert asset. → ok
  3. get asset user (to resolve field user in mutation). → ?

I suppose the last query should not be executed, becase at the time
user field is resolved it’s already loaded to parent (asset) structire.

I tried this approach.

Instead of:

  object :asset do
    field :uuid, :id
    field :name, non_null(:string)
    field :user, non_null(:user) do
      resolve dataloader(Auth, :user)
      # resolve &Resolvers.get_related_user/3
    end
  end

I do (change resolver function to custom one):

  object :asset do
    field :uuid, :id
    field :name, non_null(:string)
    field :user, non_null(:user) do
      resolve &Resolvers.get_related_user/3
    end
  end

where Resolvers.get_related_user/3 is:

  def get_related_user(parent, args, context) do
    case parent.user do
      %Ecto.Association.NotLoaded{} ->
        dataloader(Auth, :user).(parent, args, context)
      _ ->
        {:ok, parent.user}
    end
  end

It works. Now there is only 2 db requests. But I believe it’s ugly approach and there is definitely some nicer way to let Dataloader know that user object is already loaded.

Can you please give an advice?

Showing Posts 1 to 3

axelson

axelson

Scenic Core Team

Your Resolvers.get_related_user/3 makes sense to me. Although it might make sense not to call the dataloader helper from it, but instead use Dataloader directly. If you want you could even wrap it up into a helper function.

kotserubas

kotserubas OP

@axelson, thanks for response.

It seems strange to me that even in this simple example Dataloader doesn’t provide something out of the box. All it have to do is check where relation (that should be rendered in mutation response) is loaded or not.

In my opinion practicing approach I provided above would lead you to mess - you would need to have these helpers for all of your relations.

I’m pretty new to GraphQL, but for now it seems that there are a lot of problems with control over DB logic.
In REST you control pretty everyting (in terms of DB queries and their number).
Here, in GraphQL, you control much less.
Even my simple example shows that default logic would perform unnecessary DB roundtrips.

Aren’t there any other solutions to this problem?

hisa

hisa

Hi,

I just ran into a similar problem, came into this post and then asked for help in #absinthe-graphql Slack channel, referencing this post.

Luckily and quickly @dpehrson suggested me to avoid calling preload inside my context and leave all the assocs work to dataloader.

I hope that this approach may work for you as well

— All posts loaded —

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
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews