0
私のブログのアプリには、Comment
がたくさんあるPost
モデルがあります。もっと進んだ:アソシエントとエリクシル・アンプンテフ
schema "post" do
field :title, :string
field :content, :string
belongs_to :owner, MyApp.Accounts.User, foreign_key: :owner_id
has_many :comments, MyApp.Content.Comment, foreign_key: :post_id
timestamps()
end
私は関連するabsentオブジェクトも持っています。
object :post do
field :id, :integer
field :title, :string
field :content, :string
field :owner, :user, resolve: assoc(:owner)
field :comments, list_of(:comment), resolve: assoc(:comments)
end
クエリは期待通りに機能します。
スキーマにブール値active
フィールドを追加して、ソフト削除を実行できるようにします。コメントを選択するのはactive == true
です。active
をfalse
に設定してコメントを削除します。
私は自分のスキーマにフィールドを追加します。
field :active, :boolean, default: true, null: false
は今、私は唯一のアクティブなコメントを返すためにアブサン:comments
フィールドをしたいです。私はこのためのカスタムリゾルバを持っています...
field :comments, list_of(:comment), resolve: fn (query,_,resolution) ->
query =
from c in MyApp.Content.Comment,
where: c.post_id == ^query.id,
where: c.active == true,
select: c
{:ok, MyApp.Repo.all(query)}
end
私はこれがN + 1の問題に遭遇するだろうと心配しています。これを行うよりエレガントな方法はありますか?