absinthe dataloader 简单说明

dataloader 核心是为了解决graphql 对于后段服务的频繁调用,absinthe 基于graphql的dataloader 实现了elixir 版本的

参考使用

  • 代码
source = Dataloader.Ecto.new(MyApp.Repo)
 
# setup the loader
loader = Dataloader.new |> Dataloader.add_source(:db, source)
 
# load some things
loader =
  loader
  |> Dataloader.load(:db, Organization, 1)
  |> Dataloader.load_many(:db, Organization, [4, 9])
 
# actually retrieve them
loader = Dataloader.run(loader)
 
# Now we can get whatever values out we want
organizations = Dataloader.get_many(loader, :db, Organization, [1,4])

简单说明: 上边使用了Dataloader 查询数据库的数据,对于数据的查询,可以一个查询就能解决Organization 查询1,4,9 id 的问题
目前dataloader 包含了两类source,ecto 的以及kv 的

参考资料

https://hexdocs.pm/absinthe/dataloader.html
https://github.com/graphql/dataloader
https://hexdocs.pm/dataloader/2.0.0/Dataloader.KV.html

posted on 2024-01-03 19:45  荣锋亮  阅读(3)  评论(0编辑  收藏  举报

导航