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"
}
]
}
}
}
{
"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
# 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
# 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
45 Replies
bod123
bod123OP4mo ago
# lead resource

actions do
defaults([:create])
# create :create do
# accept([:type, :description, :priority])
# end
end

attributes do
uuid_primary_key(:id)

attribute :type, :string do
allow_nil?(false)
public?(true)
end

attribute :description, :string do
allow_nil?(false)
public?(true)
end

attribute :priority, :string do
allow_nil?(false)
public?(true)
end

attribute :location_id, :uuid do
allow_nil?(false)
public?(true)
end
end
# lead resource

actions do
defaults([:create])
# create :create do
# accept([:type, :description, :priority])
# end
end

attributes do
uuid_primary_key(:id)

attribute :type, :string do
allow_nil?(false)
public?(true)
end

attribute :description, :string do
allow_nil?(false)
public?(true)
end

attribute :priority, :string do
allow_nil?(false)
public?(true)
end

attribute :location_id, :uuid do
allow_nil?(false)
public?(true)
end
end
the close that I am rg is getting the following:
"errors": [
{
"code": "required",
"id": "8c27cdcc-2b61-4d77-a9c3-d30937fc6d29",
"meta": {},
"status": "400",
"title": "Required",
"source": {
"pointer": "/data/attributes/leads/0/type"
},
"detail": "is required"
}, ///......
"errors": [
{
"code": "required",
"id": "8c27cdcc-2b61-4d77-a9c3-d30937fc6d29",
"meta": {},
"status": "400",
"title": "Required",
"source": {
"pointer": "/data/attributes/leads/0/type"
},
"detail": "is required"
}, ///......
This seems odd since "type" is clearly included in the payload. I’m not sure if I’m missing something in how the leads are being parsed or managed. - Should I use manage_relationship/4 inside the change block to handle the creation of leads? - Is there a better pattern for handling nested creates like this in Ash? - Do I need to change something in the relationship or action definitions? Thanks in advance! 🙏 Any pointers are appreciated.
ZachDaniel
ZachDaniel4mo ago
You're looking for arguments w/ managed relationships 🙂 oh Sorry, ther was more message than I realized 😆
bod123
bod123OP4mo ago
if I change things to work with relashiionship I got the following: [error] ** (KeyError) key :class not found in: "invalid relationship input: leads"
ZachDaniel
ZachDaniel4mo ago
🤔
bod123
bod123OP4mo ago
hahah no worries! response time awesome 😂
ZachDaniel
ZachDaniel4mo ago
change manage_relationship(:leads, type: :create) is that what you have?
bod123
bod123OP4mo ago
yup
ZachDaniel
ZachDaniel4mo ago
oh, does the primary create action on lead accept type?
bod123
bod123OP4mo ago
it should, let me double check, to see if I am not missing something
# lead
actions do
# defaults([:create])
create :create do
accept([:type, :description, :priority])
end
end
relationships do
belongs_to :location, ProjectX.Leads.Location
end
# lead
actions do
# defaults([:create])
create :create do
accept([:type, :description, :priority])
end
end
relationships do
belongs_to :location, ProjectX.Leads.Location
end
ZachDaniel
ZachDaniel4mo ago
Can I see the parent action in full as it is now?
bod123
bod123OP4mo ago
sure!
# locations..
actions do
create :create do
accept([:name, :location, :images])
argument(:leads, {:array, :map}, allow_nil?: true)
change(manage_relationship(:leads, type: :create))
end
end
# locations..
actions do
create :create do
accept([:name, :location, :images])
argument(:leads, {:array, :map}, allow_nil?: true)
change(manage_relationship(:leads, type: :create))
end
end
I am getting the following:
** (EXIT from #PID<0.107.0>) an exception was raised:
** (Spark.Error.DslError) actions -> create -> create -> change -> manage_relationship -> leads:
** (EXIT from #PID<0.107.0>) an exception was raised:
** (Spark.Error.DslError) actions -> create -> create -> change -> manage_relationship -> leads:
ZachDaniel
ZachDaniel4mo ago
what is the body of the error? that part just shows the path
bod123
bod123OP4mo ago
** (EXIT from #PID<0.107.0>) an exception was raised:
** (Spark.Error.DslError) actions -> create -> create -> change -> manage_relationship -> leads:
Action create has no argument `:leads`.
(elixir 1.18.0) lib/process.ex:886: Process.info/2
(spark 2.2.65) lib/spark/error/dsl_error.ex:30: Spark.Error.DslError.exception/1
(ash 3.5.21) lib/ash/resource/verifiers/validate_manage_relationship_opts.ex:25: anonymous fn/3 in Ash.Resource.Verifiers.ValidateManagedRelationshipOpts.verify/1
(elixir 1.18.0) lib/enum.ex:987: Enum."-each/2-lists^foreach/1-0-"/2
(ash 3.5.21) lib/ash/resource/verifiers/validate_manage_relationship_opts.ex:18: Ash.Resource.Verifiers.ValidateManagedRelationshipOpts.verify/1
lib/project_x/leads/location.ex:1: anonymous fn/1 in ProjectX.Leads.Location.__verify_spark_dsl__/1
(elixir 1.18.0) lib/enum.ex:4438: Enum.flat_map_list/2
lib/project_x/leads/location.ex:1: ProjectX.Leads.Location.__verify_spark_dsl__/1
** (EXIT from #PID<0.107.0>) an exception was raised:
** (Spark.Error.DslError) actions -> create -> create -> change -> manage_relationship -> leads:
Action create has no argument `:leads`.
(elixir 1.18.0) lib/process.ex:886: Process.info/2
(spark 2.2.65) lib/spark/error/dsl_error.ex:30: Spark.Error.DslError.exception/1
(ash 3.5.21) lib/ash/resource/verifiers/validate_manage_relationship_opts.ex:25: anonymous fn/3 in Ash.Resource.Verifiers.ValidateManagedRelationshipOpts.verify/1
(elixir 1.18.0) lib/enum.ex:987: Enum."-each/2-lists^foreach/1-0-"/2
(ash 3.5.21) lib/ash/resource/verifiers/validate_manage_relationship_opts.ex:18: Ash.Resource.Verifiers.ValidateManagedRelationshipOpts.verify/1
lib/project_x/leads/location.ex:1: anonymous fn/1 in ProjectX.Leads.Location.__verify_spark_dsl__/1
(elixir 1.18.0) lib/enum.ex:4438: Enum.flat_map_list/2
lib/project_x/leads/location.ex:1: ProjectX.Leads.Location.__verify_spark_dsl__/1
ZachDaniel
ZachDaniel4mo ago
🤔 have you saved and recompiled? something seems quite strange
bod123
bod123OP4mo ago
yes, between changing files 🙈 . this is the code that is running without any mod, my bad, sorry:
actions do
create :create do
accept([:name, :location, :images])
argument(:leads, {:array, :map}, allow_nil?: true)

change(fn changeset, _ctx ->
leads = Ash.Changeset.get_argument(changeset, :leads)

case leads do
nil -> changeset
leads -> Ash.Changeset.manage_relationship(changeset, :leads, leads, type: :create)
end
|> dbg()
end)
end

# update :update do
# # accept([:name, :location, :images])
# argument(:leads, {:array, :map}, allow_nil?: true)
# require_atomic?(false)

# change(manage_relationship(:leads, type: :create))
# end
end
actions do
create :create do
accept([:name, :location, :images])
argument(:leads, {:array, :map}, allow_nil?: true)

change(fn changeset, _ctx ->
leads = Ash.Changeset.get_argument(changeset, :leads)

case leads do
nil -> changeset
leads -> Ash.Changeset.manage_relationship(changeset, :leads, leads, type: :create)
end
|> dbg()
end)
end

# update :update do
# # accept([:name, :location, :images])
# argument(:leads, {:array, :map}, allow_nil?: true)
# require_atomic?(false)

# change(manage_relationship(:leads, type: :create))
# end
end
Is the workaround that I have tried to create leads from here
ZachDaniel
ZachDaniel4mo ago
Yeah, that should work What is the value of leads in that case? Does it look right? You can also remove a layer of complexity by just calling the action from iex to se if it works
bod123
bod123OP4mo ago
let me send you the request body:
{
"data": {
"type": "location",
"attributes": {
"name": "Test Lead",
"location": {
"lat": 32323,
"long": 23232,
"address": "dsdsds"
},
"images": ["url1", "url2", "url3"]
},
"relationships": {
"leads": {
"data": [
{
"type": "lead",
"attributes": {
"type": "lead1",
"description": "description1",
"priority": "1"
}
},
{
"type": "lead",
"attributes": {
"type": "lead2",
"description": "description2",
"priority": "2"
}
}
]
}
}
}
}
{
"data": {
"type": "location",
"attributes": {
"name": "Test Lead",
"location": {
"lat": 32323,
"long": 23232,
"address": "dsdsds"
},
"images": ["url1", "url2", "url3"]
},
"relationships": {
"leads": {
"data": [
{
"type": "lead",
"attributes": {
"type": "lead1",
"description": "description1",
"priority": "1"
}
},
{
"type": "lead",
"attributes": {
"type": "lead2",
"description": "description2",
"priority": "2"
}
}
]
}
}
}
}
ZachDaniel
ZachDaniel4mo ago
ah okay Are you setting relationship_arguments?
Elixord
Elixord4mo ago
Hexdocs Search Results
Searched ash_json_api-1.4.35 for relationships
ash_json_api-1.4.35 | extras
ZachDaniel
ZachDaniel4mo ago
top result there shows how
bod123
bod123OP4mo ago
yes. but just because I was reading the docs to see if that worked out of the box I can remove it. this is what I have the following:
relationships do
has_many :leads, ProjectX.Leads.Lead
end
relationships do
has_many :leads, ProjectX.Leads.Lead
end
cool I will give it a go and show the result now I have some progress:
[error] ** (FunctionClauseError) no function clause matching in anonymous fn/2 in AshJsonApi.Request.relationship_change_value/1
(ash_json_api 1.4.34) lib/ash_json_api/request.ex:930: anonymous fn(:error, {:ok, []}) in AshJsonApi.Request.relationship_change_value/1
(elixir 1.18.0) lib/enum.ex:4511: anonymous fn/3 in Enum.reduce/3
(elixir 1.18.0) lib/stream.ex:1761: anonymous fn/3 in Enumerable.Stream.reduce/3
(elixir 1.18.0) lib/enum.ex:4964: Enumerable.List.reduce/3
(elixir 1.18.0) lib/stream.ex:1773: Enumerable.Stream.do_each/4
(elixir 1.18.0) lib/enum.ex:4511: Enum.reduce/3
(ash_json_api 1.4.34) lib/ash_json_api/request.ex:930: AshJsonApi.Request.relationship_change_value/1
(ash_json_api 1.4.34) lib/ash_json_api/request.ex:861: anonymous fn/4 in AshJsonApi.Request.parse_relationships/1
(stdlib 6.0.1) maps.erl:860: :maps.fold_1/4
(ash_json_api 1.4.34) lib/ash_json_api/request.ex:117: AshJsonApi.Request.from/7
(ash_json_api 1.4.34) lib/ash_json_api/controllers/post.ex:19: AshJsonApi.Controllers.Post.call/2
(project_x 0.1.0) deps/plug/lib/plug/router.ex:246: anonymous fn/4 in ProjectXWeb.AshJsonApiRouter.dispatch/2
(telemetry 1.3.0) /Users/bodhert/Workbench/project-x/deps/telemetry/src/telemetry.erl:324: :telemetry.span/3
(project_x 0.1.0) deps/plug/lib/plug/router.ex:242: ProjectXWeb.AshJsonApiRouter.dispatch/2
(project_x 0.1.0) lib/project_x_web/ash_json_api_router.ex:1: ProjectXWeb.AshJsonApiRouter.plug_builder_call/2
(phoenix 1.7.17) lib/phoenix/router/route.ex:42: Phoenix.Router.Route.call/2
(phoenix 1.7.17) lib/phoenix/router.ex:484: Phoenix.Router.__call__/5
(project_x 0.1.0) lib/project_x_web/endpoint.ex:1: ProjectXWeb.Endpoint.plug_builder_call/2
(project_x 0.1.0) lib/project_x_web/endpoint.ex:1: ProjectXWeb.Endpoint."call (overridable 3)"/2
(project_x 0.1.0) deps/plug/lib/plug/debugger.ex:155: ProjectXWeb.Endpoint."call (overridable 4)"/2
[error] ** (FunctionClauseError) no function clause matching in anonymous fn/2 in AshJsonApi.Request.relationship_change_value/1
(ash_json_api 1.4.34) lib/ash_json_api/request.ex:930: anonymous fn(:error, {:ok, []}) in AshJsonApi.Request.relationship_change_value/1
(elixir 1.18.0) lib/enum.ex:4511: anonymous fn/3 in Enum.reduce/3
(elixir 1.18.0) lib/stream.ex:1761: anonymous fn/3 in Enumerable.Stream.reduce/3
(elixir 1.18.0) lib/enum.ex:4964: Enumerable.List.reduce/3
(elixir 1.18.0) lib/stream.ex:1773: Enumerable.Stream.do_each/4
(elixir 1.18.0) lib/enum.ex:4511: Enum.reduce/3
(ash_json_api 1.4.34) lib/ash_json_api/request.ex:930: AshJsonApi.Request.relationship_change_value/1
(ash_json_api 1.4.34) lib/ash_json_api/request.ex:861: anonymous fn/4 in AshJsonApi.Request.parse_relationships/1
(stdlib 6.0.1) maps.erl:860: :maps.fold_1/4
(ash_json_api 1.4.34) lib/ash_json_api/request.ex:117: AshJsonApi.Request.from/7
(ash_json_api 1.4.34) lib/ash_json_api/controllers/post.ex:19: AshJsonApi.Controllers.Post.call/2
(project_x 0.1.0) deps/plug/lib/plug/router.ex:246: anonymous fn/4 in ProjectXWeb.AshJsonApiRouter.dispatch/2
(telemetry 1.3.0) /Users/bodhert/Workbench/project-x/deps/telemetry/src/telemetry.erl:324: :telemetry.span/3
(project_x 0.1.0) deps/plug/lib/plug/router.ex:242: ProjectXWeb.AshJsonApiRouter.dispatch/2
(project_x 0.1.0) lib/project_x_web/ash_json_api_router.ex:1: ProjectXWeb.AshJsonApiRouter.plug_builder_call/2
(phoenix 1.7.17) lib/phoenix/router/route.ex:42: Phoenix.Router.Route.call/2
(phoenix 1.7.17) lib/phoenix/router.ex:484: Phoenix.Router.__call__/5
(project_x 0.1.0) lib/project_x_web/endpoint.ex:1: ProjectXWeb.Endpoint.plug_builder_call/2
(project_x 0.1.0) lib/project_x_web/endpoint.ex:1: ProjectXWeb.Endpoint."call (overridable 3)"/2
(project_x 0.1.0) deps/plug/lib/plug/debugger.ex:155: ProjectXWeb.Endpoint."call (overridable 4)"/2
I did the following on the domain:
base_route "/location", ProjectX.Leads.Location do
post :create, relationship_arguments: [:leads]
end
base_route "/location", ProjectX.Leads.Location do
post :create, relationship_arguments: [:leads]
end
but If I changed with what the docs says
# location
create :create do
accept([:name, :location, :images])
argument(:leads, {:array, :map}, allow_nil?: false)

change(manage_relationship(:leads, type: :create))
# change(fn changeset, _ctx ->
# leads = Ash.Changeset.get_argument(changeset, :leads)

# case leads do
# nil -> changeset
# leads -> Ash.Changeset.manage_relationship(changeset, :leads, leads, type: :create)
# end
# |> dbg()
# end)
end
# location
create :create do
accept([:name, :location, :images])
argument(:leads, {:array, :map}, allow_nil?: false)

change(manage_relationship(:leads, type: :create))
# change(fn changeset, _ctx ->
# leads = Ash.Changeset.get_argument(changeset, :leads)

# case leads do
# nil -> changeset
# leads -> Ash.Changeset.manage_relationship(changeset, :leads, leads, type: :create)
# end
# |> dbg()
# end)
end
I got the following:
Erlang/OTP 27 [erts-15.0.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Compiling 4 files (.ex)
** (EXIT from #PID<0.107.0>) an exception was raised:
** (Spark.Error.DslError) actions -> create -> create -> change -> manage_relationship -> leads:
The following error was raised when validating options provided to manage_relationship.

** (RuntimeError) Required primary create action for ProjectX.Leads.Lead.
(ash 3.5.21) lib/ash/resource/info.ex:581: Ash.Resource.Info.primary_action!/2
(ash 3.5.21) lib/ash/changeset/managed_relationship_helpers.ex:323: Ash.Changeset.ManagedRelationshipHelpers.primary_action_name!/2
(ash 3.5.21) lib/ash/changeset/managed_relationship_helpers.ex:36: anonymous fn/4 in Ash.Changeset.ManagedRelationshipHelpers.sanitize_opts/2
(elixir 1.18.0) lib/keyword.ex:1144: Keyword.update!/4
(elixir 1.18.0) lib/keyword.ex:1148: Keyword.update!/4
(ash 3.5.21) lib/ash/changeset/managed_relationship_helpers.ex:22: Ash.Changeset.ManagedRelationshipHelpers.sanitize_opts/2
(ash 3.5.21) lib/ash/resource/verifiers/validate_manage_relationship_opts.ex:66: anonymous fn/3 in Ash.Resource.Verifiers.ValidateManagedRelationshipOpts.verify/1
(elixir 1.18.0) lib/enum.ex:987: Enum."-each/2-lists^foreach/1-0-"/2


(elixir 1.18.0) lib/process.ex:886: Process.info/2
(spark 2.2.65) lib/spark/error/dsl_error.ex:30: Spark.Error.DslError.exception/1
(ash 3.5.21) lib/ash/resource/verifiers/validate_manage_relationship_opts.ex:69: anonymous fn/3 in Ash.Resource.Verifiers.ValidateManagedRelationshipOpts.verify/1
(elixir 1.18.0) lib/enum.ex:987: Enum."-each/2-lists^foreach/1-0-"/2
(ash 3.5.21) lib/ash/resource/verifiers/validate_manage_relationship_opts.ex:18: Ash.Resource.Verifiers.ValidateManagedRelationshipOpts.verify/1
lib/project_x/leads/location.ex:1: anonymous fn/1 in ProjectX.Leads.Location.__verify_spark_dsl__/1
Erlang/OTP 27 [erts-15.0.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Compiling 4 files (.ex)
** (EXIT from #PID<0.107.0>) an exception was raised:
** (Spark.Error.DslError) actions -> create -> create -> change -> manage_relationship -> leads:
The following error was raised when validating options provided to manage_relationship.

** (RuntimeError) Required primary create action for ProjectX.Leads.Lead.
(ash 3.5.21) lib/ash/resource/info.ex:581: Ash.Resource.Info.primary_action!/2
(ash 3.5.21) lib/ash/changeset/managed_relationship_helpers.ex:323: Ash.Changeset.ManagedRelationshipHelpers.primary_action_name!/2
(ash 3.5.21) lib/ash/changeset/managed_relationship_helpers.ex:36: anonymous fn/4 in Ash.Changeset.ManagedRelationshipHelpers.sanitize_opts/2
(elixir 1.18.0) lib/keyword.ex:1144: Keyword.update!/4
(elixir 1.18.0) lib/keyword.ex:1148: Keyword.update!/4
(ash 3.5.21) lib/ash/changeset/managed_relationship_helpers.ex:22: Ash.Changeset.ManagedRelationshipHelpers.sanitize_opts/2
(ash 3.5.21) lib/ash/resource/verifiers/validate_manage_relationship_opts.ex:66: anonymous fn/3 in Ash.Resource.Verifiers.ValidateManagedRelationshipOpts.verify/1
(elixir 1.18.0) lib/enum.ex:987: Enum."-each/2-lists^foreach/1-0-"/2


(elixir 1.18.0) lib/process.ex:886: Process.info/2
(spark 2.2.65) lib/spark/error/dsl_error.ex:30: Spark.Error.DslError.exception/1
(ash 3.5.21) lib/ash/resource/verifiers/validate_manage_relationship_opts.ex:69: anonymous fn/3 in Ash.Resource.Verifiers.ValidateManagedRelationshipOpts.verify/1
(elixir 1.18.0) lib/enum.ex:987: Enum."-each/2-lists^foreach/1-0-"/2
(ash 3.5.21) lib/ash/resource/verifiers/validate_manage_relationship_opts.ex:18: Ash.Resource.Verifiers.ValidateManagedRelationshipOpts.verify/1
lib/project_x/leads/location.ex:1: anonymous fn/1 in ProjectX.Leads.Location.__verify_spark_dsl__/1
ZachDaniel
ZachDaniel4mo ago
ah
create :create do
accept([:type, :description, :priority])
end
create :create do
accept([:type, :description, :priority])
end
thats not primary primary? true designates it as primary default actions just do that implicitly
bod123
bod123OP4mo ago
cool, thanks for the explanation 🙏 , it added the property in both actions of create (lead and locations) now I got:
[error] ** (FunctionClauseError) no function clause matching in anonymous fn/2 in AshJsonApi.Request.relationship_change_value/1
(ash_json_api 1.4.34) lib/ash_json_api/request.ex:930: anonymous fn(:error, {:ok, []}) in AshJsonApi.Request.relationship_change_value/1
(elixir 1.18.0) lib/enum.ex:4511: anonymous fn/3
[error] ** (FunctionClauseError) no function clause matching in anonymous fn/2 in AshJsonApi.Request.relationship_change_value/1
(ash_json_api 1.4.34) lib/ash_json_api/request.ex:930: anonymous fn(:error, {:ok, []}) in AshJsonApi.Request.relationship_change_value/1
(elixir 1.18.0) lib/enum.ex:4511: anonymous fn/3
ZachDaniel
ZachDaniel4mo ago
dafuq How has no one reported this So, relationship inputs don't accept attributes like that
{
"data": {
"type": "location",
"attributes": {
"name": "Test Lead",
"location": {
"lat": 32323,
"long": 23232,
"address": "dsdsds"
},
"images": ["url1", "url2", "url3"]
},
"relationships": {
"leads": {
"data": [
{
"type": "lead",
"meta": {
"type": "lead1",
"description": "description1",
"priority": "1"
}
},
{
"type": "lead",
"meta": {
"type": "lead2",
"description": "description2",
"priority": "2"
}
}
]
}
}
}
{
"data": {
"type": "location",
"attributes": {
"name": "Test Lead",
"location": {
"lat": 32323,
"long": 23232,
"address": "dsdsds"
},
"images": ["url1", "url2", "url3"]
},
"relationships": {
"leads": {
"data": [
{
"type": "lead",
"meta": {
"type": "lead1",
"description": "description1",
"priority": "1"
}
},
{
"type": "lead",
"meta": {
"type": "lead2",
"description": "description2",
"priority": "2"
}
}
]
}
}
}
additional fields have to go in meta This is from the JSON:API spec I've pushed a fix for that issue to main It should have given you some at least mildly better error
bod123
bod123OP4mo ago
cool! I will point to main and see what arises! with the payload that you give me I am still getting
[error] ** (KeyError) key :class not found in: "invalid relationship input: leads"

If you are using the dot syntax, such as map.field, make sure the left-hand side of the dot is a map
(ash_json_api 1.4.35) lib/ash_json_api/error/error.ex:115: AshJsonApi.Error.to_json_api_errors/4
(ash_json_api 1.4.35) lib/ash_json_api/request.ex:185: anonymous fn/4 in AshJsonApi.Request.add_error/4
(elixir 1.18.0) lib/enum.ex:2546: Enum."-reduce/3-lists^foldl/2-0-"/3
(stdlib 6.0.1) maps.erl:860: :maps.fold_1/4
(ash_json_api 1.4.35) lib/ash_json_api/request.ex:117: AshJsonApi.Request.from/7
(ash_json_api 1.4.35) lib/ash_json_api/controllers/post.ex:19: AshJsonApi.Controllers.Post.call/2
(project_x 0.1.0) deps/plug/lib/plug/router.ex:246: anonymous fn/4 in ProjectXWeb.AshJsonApiRouter.dispatch/2
(telemetry 1.3.0)
[error] ** (KeyError) key :class not found in: "invalid relationship input: leads"

If you are using the dot syntax, such as map.field, make sure the left-hand side of the dot is a map
(ash_json_api 1.4.35) lib/ash_json_api/error/error.ex:115: AshJsonApi.Error.to_json_api_errors/4
(ash_json_api 1.4.35) lib/ash_json_api/request.ex:185: anonymous fn/4 in AshJsonApi.Request.add_error/4
(elixir 1.18.0) lib/enum.ex:2546: Enum."-reduce/3-lists^foldl/2-0-"/3
(stdlib 6.0.1) maps.erl:860: :maps.fold_1/4
(ash_json_api 1.4.35) lib/ash_json_api/request.ex:117: AshJsonApi.Request.from/7
(ash_json_api 1.4.35) lib/ash_json_api/controllers/post.ex:19: AshJsonApi.Controllers.Post.call/2
(project_x 0.1.0) deps/plug/lib/plug/router.ex:246: anonymous fn/4 in ProjectXWeb.AshJsonApiRouter.dispatch/2
(telemetry 1.3.0)
I am not able to understand the class, what I am missing :thinkies:
ZachDaniel
ZachDaniel4mo ago
Also a deficiency on our part one sec Can you try main?
bod123
bod123OP4mo ago
I did main. got the following in my deps: {:ash_json_api, git: "https://github.com/ash-project/ash_json_api.git", branch: "main"}
ZachDaniel
ZachDaniel4mo ago
I just pushed more I mean so mix deps.update ash_json_api
bod123
bod123OP4mo ago
roger! on it... cool, so what I got on the lock "ash_json_api": {:git, "https://github.com/ash-project/ash_json_api.git", "5f91a77d0d0964c1444a770408cbde491ba66f76", [branch: "main"]}, still getting the same issue of the class not being found
ZachDaniel
ZachDaniel4mo ago
not the right sha
bod123
bod123OP4mo ago
ok, will hard update, then.
ZachDaniel
ZachDaniel4mo ago
try setting ref to main instead of branch? I've seen weirdness around those the sha is 5bd1b70577e8ba6006df473a36328cadbf32be3b
bod123
bod123OP4mo ago
cool, it worked and returned an error that I knew how to solve it, I just took the properties from the "meta"' in the payload and worked, I dunno if is open api compatible but worked so the payload that is working rn is the following:
{
"data": {
"type": "location",
"attributes": {
"name": "Test Lead",
"location": {
"lat": 32323,
"long": 23232,
"address": "dsdsds"
},
"images": [
"url1",
"url2",
"url3"
]
},
"relationships": {
"leads": {
"data": [
{
"type": "lead1",
"description": "description1",
"priority": "1"
},
{
"type": "lead2",
"description": "description2",
"priority": "2"
}
]
}
}
}
}
{
"data": {
"type": "location",
"attributes": {
"name": "Test Lead",
"location": {
"lat": 32323,
"long": 23232,
"address": "dsdsds"
},
"images": [
"url1",
"url2",
"url3"
]
},
"relationships": {
"leads": {
"data": [
{
"type": "lead1",
"description": "description1",
"priority": "1"
},
{
"type": "lead2",
"description": "description2",
"priority": "2"
}
]
}
}
}
}
ZachDaniel
ZachDaniel4mo ago
ah, hmm.... Yeah, technically wrong but we just merge meta and the properties oh, actually nvm I did the wrong thing put them back in meta haha one sec
bod123
bod123OP4mo ago
ash you wish 😉 just fire me when you need to test
ZachDaniel
ZachDaniel4mo ago
okay, pushed
bod123
bod123OP4mo ago
working with meta! ref: b49b9d0e66fcdf014d6e9615a711e1b93ca34a4e
ZachDaniel
ZachDaniel4mo ago
🥳 So technically I've let you break the spec here but I think its fine technically relationships are supposed to have ids in them And otherwise they should be supplied in attributes or created other ways etc. but its not really a big deal IMO
bod123
bod123OP4mo ago
thanks! anything you need for me to contribute?
ZachDaniel
ZachDaniel4mo ago
More docs that might explain this for other folks would be great 🙇‍♂️ if you can find anything you think would help to add also, 1 million dollars
bod123
bod123OP4mo ago
I can do the docs first, the million dollars I am working on it with my next ai/blockchain/ash/elixir/json revolutionary app that will change the way we eat ramen
ZachDaniel
ZachDaniel4mo ago
sick, I'll send you an invoice you can pay up when you're rich 😜
bod123
bod123OP4mo ago
It will be good to add the return of the relashionship even if is just the ids or somekind of response that told me the associated resource were created :thinkies: ? should I do the docs in a pr to AshJsonApi or just explain in this same chat what I was trying to achieve?
ZachDaniel
ZachDaniel4mo ago
Docs PR always better!
bod123
bod123OP4mo ago
A promise is a promise.! https://github.com/ash-project/ash_json_api/pull/355/files any feedback is welcome, I am all hands
GitHub
Docs: Enhance relationships documentation clarity and accuracy by B...
Contributor checklist Leave anything that you believe does not apply unchecked. Bug fixes include regression tests Chores Documentation changes Features include unit/acceptance tests Refactor...

Did you find this page helpful?