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




















