gordoneliel

gordoneliel

I have a resource, eg Post, where the post has many Authors. When creating a Post, there should be at least one (1) Author attached to the payload. I know the json api v1 spec does not really outline creating multiple resources at once, but I figured we should be able to regardless?

What I’ve tried so far:

  1. Passing the relationship in the relationships section of the body - This fails with stack
[error] GenServer #PID<0.767.0> terminating
** (FunctionClauseError) no function clause matching in anonymous fn/2 in AshJsonApi.Request.relationship_change_value/1
    (ash_json_api 0.33.1) lib/ash_json_api/request.ex:668: anonymous fn(:error, {:ok, []}) in AshJsonApi.Request.relationship_change_value/1
    (elixir 1.15.7) lib/enum.ex:4387: anonymous fn/3 in Enum.reduce/3
  1. Passing the relationship into the Post attributes when creating - This does not work, throws an invalid body because the Author attribute is not an attribute on the resource but rather a relationship.

I combed through the relationships section of Ash and AshJsonApi but cant seem to find anything related to creating multiple related resources.

Showing Posts 1 to 10

zachdaniel

zachdaniel

Creator of Ash

The best way to go about this is to use manage_relationship i.e

create :create do
  argument :relationship, {:array, :map}
  change manage_relationship(:relationship, type: :create)
end

Then you can provide that as an input.

gordoneliel

gordoneliel OP

I did try that with #1.

{
  data: {
   attributes: {...attributes},
   relationships: {
     authors: [{type: "author", name: "Eliel", email: "q232"}]
   }
  }
}

This got somewhere but seems to need an id attribute in the relationship. When I add that none of the other attributes in the relationship make it to the changeset attributes under authors.

The other way was to put the relationship in the attributes

{
  data: {
   attributes: {
    ...attributes,
    authors: [{name: "Eliel", email: "q232"}]
   },
  }
}

This did not work either, gave a invalid attribute error

zachdaniel

zachdaniel

Creator of Ash

It should be done in the attributes when using that strategy IIRC. It’s been a while since I looked at that particular code. What invalid attribute error are you getting?

gordoneliel

gordoneliel OP

Getting:
Expected only defined properties, got key ["data", "attributes", "authors"].

This is my create action:

 create :create do
      primary? true

      argument :authors, {:array, :map}

      accept [..someparams]

      change manage_relationship(:authors, type: :create)
    end
zachdaniel

zachdaniel

Creator of Ash

Okay, I can see it now, yeah. It should be in the relationships, but the issue is that we are currently requiring id, which doesn’t make sense in all contexts. I’ve just pushed something to main to make those optional. Additionally, extra attributes have to go in the "meta" key of the relationship, i.e {type: "type", "meta" => {foo: "bar"}}. This is required by the spec, IIRC.

gordoneliel

gordoneliel OP

I think that would work! The json api spec for v1 is a little weird with this, I wonder if the upcoming v1.2 spec makes it easier to do this

gordoneliel

gordoneliel OP

On a similar note, I have a situation where I have to create a Comment.

When creating a comment it should have the associated Post it belongs to. I know we can get the data on a related resource for eg. GET /post/:id/comments, but when I try to send a ‘POST’ request to the same url, I get a route not found error.

Question is, how do we post to a related resource? I tried just posting to the direct resource url, eg /comments, but then I have to manually attach the relationship, and create two different actions on the Comment resource, one for when creating the comment through a Post (I would like to be able to create a comment when creating a new Post), and another when creating a comment after a Post has been created.

gordoneliel

gordoneliel OP

Honestly not sure what the idiomatic way for JSONApi would be.
Should I have a PATCH on Post eg /post/:id/comment, route: "/:id/contributors" (this is on post json_api routes), and an action on the Post resource that delegates creation of the Comment through the managed relationship? Or should it be a POST on /post/:id/comments that goes directly to the Comment create action?

Example for reference:

  Post resource:
  ...

  json_api do
    type "post"

    update :add_comment do
      argument :comment, :map, allow_nil?: false

      accept []

      change manage_relationship(:comments, type: :create)
    end

    routes do
      base("/posts")
      
      patch(:add_comment, route: "/:id/comments")
    end
  end
zachdaniel

zachdaniel

Creator of Ash

So, that part is actually still based on old behavior, and probably needs to be upgraded at some point. What it does is use the primary action and explicitly adds Changeset.manage_relationship (instead of it being an argument in an action).

So you can do this in routes:

post_to_relationship :comments
gordoneliel

gordoneliel OP

I do have that on the Post resource, but getting a no route found when hitting POST /post/:id/comments

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
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
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
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews