Ash FrameworkAF
Ash Framework7mo ago
100 replies
bod123

Creating a resource from another endpoint

Hey everyone! I’m running into a bit of a challenge and could use your input.

I have an endpoint called locations, and I want to allow users to create leads as part of a POST request to:

{{url}}/api/json/location

The idea is to allow creating a location along with multiple leads in a single request. Here's an example of the payload I’m trying to send:

{
  "data": {
    "type": "location",
    "attributes": {
      "name": "Test Lead",
      "location": {
        "lat": 32323,
        "long": 23232,
        "address": "dsdsds"
      },
      "images": ["url1", "url2", "url3"],
      "leads": [
        {
          "type": "lead1",
          "description": "description1",
          "priority": "1"
        },
        {
          "type": "lead2",
          "description": "description2",
          "priority": "2"
        }
      ]
    }
  }
}


- What I’ve Tried
Checked Ash JSON API relationship manipulation docs
- Attempted to manually create the leads inside a change block using Ash.Changeset.manage_relationship/4

relevant code:

# my domain
#... code for doamins 
      base_route "/location", ProjectX.Leads.Location do
        post :create
      end


---
# location resource
  actions do
    create :create do
      accept([:name, :location, :images])
      argument(:leads, {:array, :map}, allow_nil?: true)

      change(fn changeset, _ctx ->
      # should I do it here
      end)
    end

# Ash.Changeset.manage_relationship(changeset, :leads, leads, type: :create) # should I do it as a relashionship?


  relationships do
    has_many :leads, ProjectX.Leads.Lead
  end
end
Was this page helpful?