Hi!
I have a Category and Product relation.
Category has_many Products,
Products belongs_to Category.
I want to get all category and get 10 items for each category for my home screen.
in schema.ex
@desc "Get all categories"
field :categories, list_of(:category) do
resolve(&Resolvers.Category.get_categories/3)
end
object :category do
field :name, non_null(:string)
field :products, list_of(:product) do
arg :limit, type: :integer, default_value: 20
resolve dataloader(Products, :products, arg: %{scope: :category})
end
in products.ex
def datasource() do
Dataloader.Ecto.new(MyApp.Repo, query: &query/2)
end
def query(Product, %{scope: :category, limit: limit}) do
Product
|> where(active: true)
|> limit(^limit)
end
def query(queryable, _) do
queryable
end
in resolvers.category.ex
def get_categories(_, _, _) do
{:ok, Category.get_categories()}
end
in category.ex
def get_categories() do
Repo.all from c in Category
end
in graphiql query I tried
{
categories {
name
products(limit: 10) {
name
}
}
}
But it seems not working correctly.
it returns all categories but empty product and only one category returns 10 products
what am I doing wrong?