Skip to content

Commit

Permalink
Move dataloader config
Browse files Browse the repository at this point in the history
  • Loading branch information
johnshaughnessy committed Oct 29, 2020
1 parent 560da97 commit 41499f5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
10 changes: 10 additions & 0 deletions lib/ret/api/dataloader.ex
@@ -0,0 +1,10 @@
defmodule Ret.Api.Dataloader do
@moduledoc "Configuration for dataloader"

alias Ret.{Repo, Scene, SceneListing}

def source(), do: Dataloader.Ecto.new(Repo, query: &query/2)
# Guard against loading removed scenes or delisted scene listings
def query(Scene, _), do: from(s in Scene, where: s.state != ^:removed)
def query(SceneListing, _), do: from(sl in SceneListing, where: sl.state != ^:delisted)
end
6 changes: 0 additions & 6 deletions lib/ret/scene.ex
Expand Up @@ -55,12 +55,6 @@ defmodule Ret.Scene do
timestamps()
end

# Dataloader
def data(), do: Dataloader.Ecto.new(Repo, query: &query/2)
# Guard against loading removed scenes or delisted scene listings
def query(Scene, _), do: from(s in Scene, where: s.state != ^:removed)
def query(SceneListing, _), do: from(sl in SceneListing, where: sl.state != ^:delisted)

def scene_or_scene_listing_by_sid(sid) do
Scene |> Repo.get_by(scene_sid: sid) ||
SceneListing |> Repo.get_by(scene_listing_sid: sid) |> Repo.preload(scene: Scene.scene_preloads())
Expand Down
3 changes: 1 addition & 2 deletions lib/ret_web/schema.ex
Expand Up @@ -2,7 +2,6 @@ defmodule RetWeb.Schema do
@moduledoc false

use Absinthe.Schema
alias Ret.Scene

import RetWeb.Middleware, only: [build_middleware: 3]

Expand All @@ -25,7 +24,7 @@ defmodule RetWeb.Schema do
def context(ctx) do
loader =
Dataloader.new()
|> Dataloader.add_source(Scene, Scene.data())
|> Dataloader.add_source(:db, Ret.Api.Dataloader.source())

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

0 comments on commit 41499f5

Please sign in to comment.