-
-
Save tlvenn/936554884efa4b03129fcfda56dce4c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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