Skip to content

Instantly share code, notes, and snippets.

@tlvenn
Forked from smoak/schema.ex
Created Nov 16, 2017
Embed
What would you like to do?
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