Skip to content
Permalink
Browse files

Migrate to Dataloader instead of Absinthe.Ecto

  • Loading branch information...
schneidmaster committed Jul 14, 2019
1 parent 655776d commit ff0925d7ea8749a4a5304d730084e8e819ad3b33
@@ -0,0 +1,14 @@
defmodule SocializerWeb.Data do
import Ecto.Query

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

def query(queryable, params) do
case Map.get(params, :order_by) do
nil -> queryable
order_by -> from record in queryable, order_by: ^order_by
end
end
end
@@ -1,5 +1,8 @@
defmodule SocializerWeb.Schema do
use Absinthe.Schema

alias SocializerWeb.Data

import_types(Absinthe.Type.Custom)
import_types(SocializerWeb.Schema.CommentTypes)
import_types(SocializerWeb.Schema.ConversationTypes)
@@ -30,6 +33,18 @@ defmodule SocializerWeb.Schema do
import_fields(:post_subscriptions)
end

def context(ctx) do
loader =
Dataloader.new()
|> Dataloader.add_source(Data, Data.data())

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

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

def middleware(middleware, _field, _object) do
[NewRelic.Absinthe.Middleware] ++ middleware
end
@@ -1,17 +1,18 @@
defmodule SocializerWeb.Schema.CommentTypes do
use Absinthe.Schema.Notation
use Absinthe.Ecto, repo: Socializer.Repo

alias SocializerWeb.Resolvers
import Absinthe.Resolution.Helpers, only: [dataloader: 1]

alias SocializerWeb.{Data, Resolvers}

@desc "A comment on the site"
object :comment do
field :id, :id
field :body, :string
field :inserted_at, :naive_datetime

field :post, :post, resolve: assoc(:post)
field :user, :user, resolve: assoc(:user)
field :post, :post, resolve: dataloader(Data)
field :user, :user, resolve: dataloader(Data)
end

object :comment_queries do
@@ -1,9 +1,9 @@
defmodule SocializerWeb.Schema.ConversationTypes do
use Absinthe.Schema.Notation
use Absinthe.Ecto, repo: Socializer.Repo
import Ecto.Query

alias SocializerWeb.Resolvers
import Absinthe.Resolution.Helpers, only: [dataloader: 1, dataloader: 3]

alias SocializerWeb.{Data, Resolvers}
alias Socializer.Conversation

@desc "A conversation on the site"
@@ -12,15 +12,10 @@ defmodule SocializerWeb.Schema.ConversationTypes do
field :title, :string
field :updated_at, :naive_datetime

field :messages, list_of(:message) do
resolve(
assoc(:messages, fn messages_query, _args, _context ->
messages_query |> order_by(asc: :id)
end)
)
end
field :messages, list_of(:message),
resolve: dataloader(Data, :messages, args: %{order_by: :id})

field :users, list_of(:user), resolve: assoc(:users)
field :users, list_of(:user), resolve: dataloader(Data)
end

object :conversation_queries do
@@ -1,17 +1,18 @@
defmodule SocializerWeb.Schema.MessageTypes do
use Absinthe.Schema.Notation
use Absinthe.Ecto, repo: Socializer.Repo

import Absinthe.Resolution.Helpers, only: [dataloader: 1]

alias Socializer.Conversation
alias SocializerWeb.Resolvers
alias SocializerWeb.{Data, Resolvers}

@desc "A message on the site"
object :message do
field :id, :id
field :body, :string

field :conversation, :conversation, resolve: assoc(:conversation)
field :user, :user, resolve: assoc(:user)
field :conversation, :conversation, resolve: dataloader(Data)
field :user, :user, resolve: dataloader(Data)
end

object :message_queries do
@@ -1,25 +1,20 @@
defmodule SocializerWeb.Schema.PostTypes do
use Absinthe.Schema.Notation
use Absinthe.Ecto, repo: Socializer.Repo
import Ecto.Query

alias SocializerWeb.Resolvers
import Absinthe.Resolution.Helpers, only: [dataloader: 1, dataloader: 3]

alias SocializerWeb.{Data, Resolvers}

@desc "A post on the site"
object :post do
field :id, :id
field :body, :string
field :inserted_at, :naive_datetime

field :user, :user, resolve: assoc(:user)
field :user, :user, resolve: dataloader(Data)

field :comments, list_of(:comment) do
resolve(
assoc(:comments, fn comments_query, _args, _context ->
comments_query |> order_by(asc: :id)
end)
)
end
field :comments, list_of(:comment),
resolve: dataloader(Data, :comments, args: %{order_by: :id})
end

object :post_queries do
@@ -1,8 +1,9 @@
defmodule SocializerWeb.Schema.UserTypes do
use Absinthe.Schema.Notation
use Absinthe.Ecto, repo: Socializer.Repo

alias SocializerWeb.Resolvers
import Absinthe.Resolution.Helpers, only: [dataloader: 1]

alias SocializerWeb.{Data, Resolvers}

@desc "A user of the site"
object :user do
@@ -17,7 +18,7 @@ defmodule SocializerWeb.Schema.UserTypes do
end)
end

field :posts, list_of(:post), resolve: assoc(:posts)
field :posts, list_of(:post), resolve: dataloader(Data)
end

object :user_queries do
@@ -47,9 +47,9 @@ defmodule Socializer.MixProject do
{:bcrypt_elixir, "~> 2.0"},
{:guardian, "~> 1.0"},
{:absinthe, "~> 1.4"},
{:absinthe_ecto, "~> 0.1.3"},
{:absinthe_phoenix, "~> 1.4.0"},
{:absinthe_plug, "~> 1.4"},
{:dataloader, "~> 1.0.0"},
{:poison, "~> 2.1.0"},
{:quantum, "~> 2.3"},
{:timex, "~> 3.0"},
@@ -13,6 +13,7 @@
"cowboy": {:hex, :cowboy, "2.6.3", "99aa50e94e685557cad82e704457336a453d4abcb77839ad22dbe71f311fcc06", [:rebar3], [{:cowlib, "~> 2.7.3", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
"cowlib": {:hex, :cowlib, "2.7.3", "a7ffcd0917e6d50b4d5fb28e9e2085a0ceb3c97dea310505f7460ff5ed764ce9", [:rebar3], [], "hexpm"},
"crontab": {:hex, :crontab, "1.1.5", "2c9439506ceb0e9045de75879e994b88d6f0be88bfe017d58cb356c66c4a5482", [:mix], [{:ecto, "~> 1.0 or ~> 2.0 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm"},
"dataloader": {:hex, :dataloader, "1.0.6", "fb724d6d3fb6acb87d27e3b32dea3a307936ad2d245faf9cf5221d1323d6a4ba", [:mix], [{:ecto, ">= 0.0.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm"},
"db_connection": {:hex, :db_connection, "2.0.6", "bde2f85d047969c5b5800cb8f4b3ed6316c8cb11487afedac4aa5f93fd39abfa", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm"},
"decimal": {:hex, :decimal, "1.8.0", "ca462e0d885f09a1c5a342dbd7c1dcf27ea63548c65a65e67334f4b61803822e", [:mix], [], "hexpm"},
"ecto": {:hex, :ecto, "3.1.1", "d6677f95f1e0bd39bc3db3db6b23a59977cb154ed2cceec69a56becd805128be", [:mix], [{:decimal, "~> 1.6", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},

0 comments on commit ff0925d

Please sign in to comment.
You can’t perform that action at this time.