seanyutah

seanyutah

I have a classic nested comment schema where only one level of children is needed.

  schema "comments" do
    field(:message, :string)
    field(:removed_at, :utc_datetime)

    belongs_to(:post, Post)
    belongs_to(:user, User)
    belongs_to(:parent, Comment)
    has_many(:children, Comment, foreign_key: :parent_id)

    timestamps()
  end

I’m working on the query to fetch the comments for a given post in a nested fashion. The query looks great, with the user data joined and only making two queries for the two levels of comments. (only preloading children at the top level)

    children_query =
      from c in Comment,
        join: u in assoc(c, :user),
        order_by: [desc: c.inserted_at],
        preload: [user: u]

    query =
      from c in Comment,
        join: u in assoc(c, :user),
        preload: [user: u, children: ^children_query],
        where: c.post_id == ^post_id,
        where: is_nil(c.parent_id),
        order_by: [desc: c.inserted_at]

    Repo.all(query)

The structure being returned looks great and all the data I need is there. But now I would like to pare down that data by selecting only what I need.

The only way I could figure out how to do that is by mapping over the lists of comments and picking out the attributes. I can’t leave the comment structs to the jason encoder because I want the children for the top level comments but not for the nested comments. (so if I include :children in the @derive {Jason.Encoder... for comments it breaks on the nested comments because children are not preloaded)

So is there a way to select specific fields when dealing with nested, preloaded data?

The mapping and picking doesn’t seem like a performance issue but nested map seems barbaric after such an elegant job with the preload. Here is what I am currently doing:

      Comments.get_by_post(post_id)
      |> Enum.map(fn c ->
        %{
          children:
            Enum.map(c.children, fn c ->
              %{
                id: c.id,
                message: c.message,
                inserted_at: c.inserted_at,
                username: c.user.username
              }
            end),
          id: c.id,
          message: c.message,
          inserted_at: c.inserted_at,
          username: c.user.username
        }
      end)

Showing Posts 1 to 9

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

As a comment about your current queries, I don’t think preloading with joins is the right call here. If a user makes 3 comments, then that user’s information is sent from postgres 3 times, and Ecto has to deduplicate. Using a normal preload([:user]) without the join will make 2 SQL queries, but they will each be smaller and more efficient.

Yes, and this will be even easier if you do simple preloads:

user_query = from u in User, select: struct(u, [:id, :username])
comment_query = from c in Comment,
  select: struct(c, [:id, :parent_id, :inserted_at, :user_id]),
  order_by: [desc: c.inserted_at]

query = from c in comment_query,
        where: c.post_id == ^post_id,
        where: is_nil(c.parent_id),
        preload: [user: ^user_query, children: {^comment_query, [user: ^user_query}]

Repo.all(query)

You may need to still map over things in order to transform it into the shape you want, but either you’re doing that or the DB is doing that, and it’s generally best if your app does it since the database is the more limited resource.

seanyutah

seanyutah OP

Thanks for the reply, Ben. That’s interesting that the separate user queries would be faster than the joins. If the users are different for every comment (which will be typical) and I can select for specific user columns in the two joined queries, I’d imagine the joins would be faster, and leave less logic for elixir. Either way…
Sorry, I should’ve been more clear. The focus of my question is really the :children. With the issue being that I can’t choose to encode or select the :children from the Comment struct at the top level but also omit it when nested. It’s frustrating that I’m logging the ecto output and it’s precisely what I need, but there’s no way to get what I want and omit what I don’t without looping over the lists.
I’m wanting something like “encode all values unless they are NotLoaded” … or “ability to select virtual fields” or ??

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Oh, I think this is just a misunderstanding about best practices then for JSON encoding. It is NOT considered a best practice to use @derive for doing encodings for an API. Instead you should be using JSON views. This post all the way back from 2015 is still relevant: https://rokkincat.com/blog/2015-03-17-json-views-in-phoenix/

seanyutah

seanyutah OP

Yea it makes sense that using @derive isn’t a best practice. I’m seeing the limitations first-hand. I looked into Phoenix views and didn’t see anything that would allow me to get what I want without nested loops. Do you know of anything? Can you agree that it would be nice to specify/select the children for the top level and omit it from the nested comments, therefore avoiding the need to loop?

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Isn’t that what my example did? I built a query that selected values on comments, and then reused that at both levels without having to specify it twice.

seanyutah

seanyutah OP

Your code outputs the same ecto structure as mine in that the top-level comments have children preloaded and the nested comments do not . Like I said, the ecto output isn’t the problem. The issue I’m wondering about is converting the ecto structs to maps (and then json) without having to map over the top level and nested lists.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

The normal thing is to map over them.

03juan

03juan

Have you considered creating a separate schema for child comments on the same table which doesn’t define the has_many :children? Then you can move the belongs_to :parent over to it and derive each one separately without mapping to ignore irrelevant keys for each context.

seanyutah

seanyutah OP

Boom! That is a great idea. Thanks Juan!

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley: Mutonex gamedesign was finish...
New
Stef-Verniers
Hello I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews