sangeethailango
Given I have a nested resource with articles having many comments, how do I create an API endpoint for posting comments.
I want my API for creating comments at POST /articles/:id/comments. How do I create this nested API structure?
My resource for Articles and Comments are shown below:
Article resource:
defmodule Account.Blog.Article do
use Ash.Resource,
domain: Account.Blog,
data_layer: AshPostgres.DataLayer,
extensions: [AshJsonApi.Resource]
alias Account.Blog.Comment
postgres do
table "articles"
repo Account.Repo
end
json_api do
type "article"
end
actions do
defaults [:read]
create :article do
accept [:name]
end
end
attributes do
uuid_primary_key :id
attribute :name, :string
end
relationships do
has_many :comments, Comment
end
end
Comments resource
defmodule Account.Blog.Comment do
use Ash.Resource,
domain: Account.Blog,
data_layer: AshPostgres.DataLayer,
extensions: [AshJsonApi.Resource]
alias Account.Blog.Article
postgres do
table "comments"
repo Account.Repo
end
json_api do
type "comment"
end
actions do
defaults [:read]
create :comment do
accept [:comment, :article_id]
end
end
attributes do
uuid_primary_key :id
attribute :comment, :string
end
relationships do
belongs_to :article, Article
end
end
Domain:
defmodule Account.Blog do
use Ash.Domain,
extensions: [AshJsonApi.Domain]
alias Account.Blog.{Article, Comment}
resources do
resource Article
resource Comment
end
json_api do
routes do
base_route "/articles", Article do
get(:read)
post(:article)
end
base_route "/comments", Comment do
get(:read)
post(:comment)
end
end
end
end
I am currently giving the request at POST /comments with the article_id to mention which article the comments belongs to.
Example:
{
"data": {
"type": "comment",
"attributes": {
"comment": "Good",
"article_id": "article_id"
}
}
}
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
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
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
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
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
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 6- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
zachdaniel
You should be able to do that by including an
article_idin the route params for yourpost.Your post pointed out to me that being able to nest
base_routescould clean this up greatly, so I’m pushing a release w/ that ability soon. Then it would look like this:shankardevy
wow @zachdaniel that’s super fast! You comment here is 18hours ago and I see this commit implementing this feature17hrs ago! improvement: support nested `base_route`s · ash-project/ash_json_api@bf77d4d · GitHub
This is something I was also looking for as well. However, I would like to understand the difference between using
relatedorpost_to_relationshipmacro inside theroutesvs what you have proposed as a new solution here:For eg., can the following do the same as what you have proposed as nested
base_routefor creating comment?sangeethailango
@zachdaniel Thank you so much for the quick reply
. Updated to the newer version and applied the solution.
zachdaniel
relatedis a route for reading the records of a relationship, i.e/posts/:id/commentsIt can be thought of as an index route that only returns the related records.
post_to_relationshipis for “linking” a record or records to another via a relationship. It accepts a very specific input (something called “linkage”), and is typically only used to connect existing things together.You can see more here: JSON:API — Latest Specification (v1.1)
shankardevy
Just to clarify, please check if my understanding is correct:
1.
relatedoption onroutesUse this only for
GETrequests for related records. Doesn’t work for any other methods.If
Articleresource has manyTagrelationship, thisrelatedoption is used to fetch only the associated tags of a specific article.In this case, the Ash
actionfor the api is placed in theArticleresource.2.
post_to_relationshipoption onroutesUse this option for any other methods other than
GETfor related records. The related record must already exist. In the case ofArticleandTagrelationships, both the records must exist already.post_to_relationshipis used to create the linkage inArticleTagsresource.The same applies for
patch_relationship,delete_from_relationshipto update or delete the linkArticleTagsresource record.In this case, the Ash
actionfor the api is placed in theArticleresource.3. Nested
base_routeas explained in this post by @zachdanielUse this option to do any of the above actions with the main differences as below:
actionfor the nested route is present in the nested resource rather than in the parent resource as in the above options. i.e., GET/articles/:id/tagswill be responded by an action in theTagresource.Is my understanding correct?
zachdaniel
Yep! Looks right to me