itopiz

itopiz

How to run the Absinthe dataloader only once for all types implementing an interface?

I have an interface in my schema:

interface :authentication_source do
  field(:id, non_null(:id))
  field(:name, non_null(:string))
  field(:slug, non_null(:string))
  field(:user_authentications, list_of(non_null(:user_authentication)))

  resolve_type(fn
    %OidcAuthenticationSource{}, _ -> :oidc_authentication_source
    %MicrosoftAuthenticationSource{}, _ -> :microsoft_authentication_source
  end)
end

as well as 2 nodes types that implement that interface:

node object(:oidc_authentication_source, id_fetcher: &authentication_source_id_fetcher/2) do
  field(:name, non_null(:string))
  field(:slug, non_null(:string))
  field(:metadata_document, :string)
  field(:client_id, :string)
  field(:client_secret, :string)
  field(:user_authentications, list_of(non_null(:user_authentication))) do
    resolve(dataloader(Accounts))
  end

  interface(:authentication_source)
end

node object(:microsoft_authentication_source, id_fetcher: &authentication_source_id_fetcher/2) do
  field(:name, non_null(:string))
  field(:slug, non_null(:string))
  field(:tenant_id, non_null(:string))
  field(:client_id, non_null(:string))
  field(:client_secret, non_null(:string))
  field(:user_authentications, list_of(non_null(:user_authentication))) do
    resolve(dataloader(Accounts))
  end

  interface(:authentication_source)
end

The Accounts source is using Dataloader.Ecto and when I run this GraphQL query:

query authenticationSources {
  authenticationSources {
    __typename
    id
    name
    slug
    ... on OidcAuthenticationSource {
      metadataDocument
      clientId
      clientSecret
    }
    ... on MicrosoftAuthenticationSource {
      tenantId
      clientId
      clientSecret
    }
    userAuthentications {
      externalUserId
      user {
        id
        email
        displayName
      }
    }
  }
}

I observe that 2 identical SQL queries are executed to load the :user_authentications:

  • 1 query with all the IDs of the authentication sources of type :oidc_authentication_source
  • 1 query with all the IDs of the authentication sources of type :microsoft_authentication_source

The dataloader is very helpful but still it is triggering 1 SQL query per type implementing my interface.

I have re-read the following documentations but I cannot find a way to reduce this to a single query :

Is it possible?

First Post!

itopiz

itopiz

I discovered that using a key function seems to do the trick:

defp dataloader_user_authentications(authentication_source, _args, _info) do
  %{
    batch: {{:many, UserAuthentication}, %{}},
    item: [authentication_source_dbid: authentication_source.authentication_source_dbid]
  }
end

node object(:oidc_authentication_source, id_fetcher: &authentication_source_id_fetcher/2) do
  field(:name, non_null(:string))
  field(:slug, non_null(:string))
  field(:metadata_document, :string)
  field(:client_id, :string)
  field(:client_secret, :string)
  field(:user_authentications, list_of(non_null(:user_authentication))) do
    resolve(dataloader(Accounts, &dataloader_user_authentications/3))
  end

  interface(:authentication_source)
end

node object(:microsoft_authentication_source, id_fetcher: &authentication_source_id_fetcher/2) do
  field(:name, non_null(:string))
  field(:slug, non_null(:string))
  field(:tenant_id, non_null(:string))
  field(:client_id, non_null(:string))
  field(:client_secret, non_null(:string))
  field(:user_authentications, list_of(non_null(:user_authentication))) do
    resolve(dataloader(Accounts, &dataloader_user_authentications/3))
  end

  interface(:authentication_source)
end

Now a single SQL query is sent. The SQL query looks almost the same as before except it does not duplicate authentication_source_dbid at the end of SELECT and its does not use authentication_source_dbid in ORDER BY.

This was not obvious from the documentation.

@benwilson512 Does that mean it is simply an undocumented feature or it is something that could disappear in future release? Could this behavior be added in the simpler form resolve(dataloader(Accounts)) as well?

Where Next?

Popular in Questions Top

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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
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
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
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

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
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
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
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
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

We're in Beta

About us Mission Statement