Skip to content

Instantly share code, notes, and snippets.

@tlvenn
Forked from smoak/schema.ex
Created November 16, 2017 03:38
Show Gist options
  • Save tlvenn/936554884efa4b03129fcfda56dce4c2 to your computer and use it in GitHub Desktop.
Save tlvenn/936554884efa4b03129fcfda56dce4c2 to your computer and use it in GitHub Desktop.
defmodule ApiWeb.Schema do
use Absinthe.Schema
import Absinthe.Resolution.Helpers
import_types Absinthe.Type.Custom
@desc "A user"
object :user do
field :id, non_null(:string)
field :email, non_null(:string)
end
query do
@desc "Get a user given its id"
field :user, :user do
arg :id, non_null(:string)
resolve dataloader(:user)
end
end
def context(ctx) do
loader =
Dataloader.new
|> Dataloader.add_source(:user, Api.Loaders.Users.data())
Map.put(ctx, :loader, loader)
end
def plugins do
[Absinthe.Middleware.Dataloader] ++ Absinthe.Plugin.defaults()
end
end
defmodule Api.Loaders.Users do
def data() do
Dataloader.KV.new(&load/2)
end
def load(batch_key, ids) do
# if we made it this far, we haven't fetched before...
# this should return a tuple of
# {id, obj}
# batch_key is {:user, %{id: "1"}}
# ids is [%{}]
%{}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment