gbrlcustodio

gbrlcustodio

How are you approaching rendeding ecto model association using "viewless" JSON views?

Hey everyone! It’s been a while since I last built a Phoenix app, and I noticed that Phoenix 1.7 no longer supports Phoenix views. I know I can always add Phoenix views as a dependency, but I’m trying to do things the new way. Unfortunately, I’m having a hard time finding resources for building JSON-APIs that work with newer Phoenix versions. The JSON and APIs documentation only covers simple scenarios.

So, I’m curious to know how you’re organizing your JSON views when it comes to reusing them for rendering associations like when we used to have render_one and render_many helper functions to cross-reference them. For example, let’s say we have a post with many comments. How are you reusing the rendering logic for both the /posts and /comments resources, where the post also includes its comments but rendering them referencing the CommentJSON view or something?

Are you doing something like this or extracting code that translates Ecto schemas into Elixir maps and reusing it?

defmodule HelloWeb.PostJSON do
  @doc """
  Renders a single post.
  """
  def show(%{post: post}) do
    %{data: data(post)}
  end

  defp data(%Post{} = post) do
    %{
      id: post.id,
      title: post.title,
      comments: HelloWeb.CommentJSON.index(%{comments: post.comments})[:data]
    }
  end
end

defmodule HelloWeb.CommentJSON do
  def index(%{comments: comments}) do
    %{data: for(comment <- comments, do: comment_json(comment))}
  end

  defp comment_json(%Comment{} = comment) do
    %{
      id: comment.id,
      text: comment.type
    }
  end
end

First 2 of 2 Posts! Switch mode

krasenyp

krasenyp

Just use functional programming, like in your example. In my opinion, Ecto schemas have no business in the controller.

gbrlcustodio

gbrlcustodio

Hi @krasenyp, thanks for your reply!

Regarding reusing serialization logic from the CommentJSON module in the PostJSON, are you referring to the module directly? Doing comments: HelloWeb.CommentJSON.index(%{comments: post.comments})[:data] felt a bit strange because almost sounding like I should have it extracted elsewhere and reused across JSON modules like this:

defmodule HelloWeb.PostJSON do
  def show(%{post: post}) do
    %{data: HelloWeb.Serializers.Post.json(post)}
  end
end

defmodule HelloWeb.CommentJSON do
  def index(%{comments: comments}) do
    %{data: for(comment <- comments, do: HelloWeb.Serializers.Comment.json(post)(comment))}
  end
end

Maybe I’m overcomplicating things. CommentJSON.comment_json/1 and PostJSON.data/1 (just realized it’s not called post_json) could be exposed as public functions that associated modules could call, like HelloWeb.CommentJSON.comment_json(comment).

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement