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:
- 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:
---
45 Replies
the close that I am rg is getting the following:
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.
You're looking for arguments w/ managed relationships 🙂
oh
Sorry, ther was more message than I realized 😆
if I change things to work with relashiionship I got the following:
[error] ** (KeyError) key :class not found in: "invalid relationship input: leads"
🤔
hahah no worries! response time awesome 😂
change manage_relationship(:leads, type: :create)
is that what you have?yup
oh, does the primary create action on lead accept type?
it should, let me double check, to see if I am not missing something
Can I see the parent action in full as it is now?
sure!
I am getting the following:
what is the body of the error?
that part just shows the path
🤔
have you saved and recompiled?
something seems quite strange
yes, between changing files 🙈 .
this is the code that is running without any mod, my bad, sorry:
Is the workaround that I have tried to create leads from here
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 workslet me send you the request body:
ah
okay
Are you setting
relationship_arguments
?Hexdocs Search Results
Searched
ash_json_api-1.4.35
for relationships
ash_json_api-1.4.35 | extras
ash_json_api-1.4.35 | extras
top result there shows how
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:
cool I will give it a go and show the result
now I have some progress:
I did the following on the domain:
but If I changed with what the docs says
I got the following:
ah
thats not primary
primary? true
designates it as primary
default actions just do that implicitlycool, thanks for the explanation 🙏 , it added the property in both actions of create (lead and locations) now I got:
dafuq
How has no one reported this
So, relationship inputs don't accept attributes like that
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 errorcool! I will point to main and see what arises!
with the payload that you give me I am still getting
I am not able to understand the
class
, what I am missing :thinkies:Also a deficiency on our part
one sec
Can you try
main
?I did main. got the following in my deps:
{:ash_json_api, git: "https://github.com/ash-project/ash_json_api.git", branch: "main"}
I just pushed more I mean
so
mix deps.update ash_json_api
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 foundnot the right sha
ok, will hard update, then.
try setting
ref
to main
instead of branch
?
I've seen weirdness around those
the sha is 5bd1b70577e8ba6006df473a36328cadbf32be3b
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:
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
ash
you wish 😉
just fire me when you need to testokay, pushed
working with meta!
ref: b49b9d0e66fcdf014d6e9615a711e1b93ca34a4e
🥳
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
thanks! anything you need for me to contribute?
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
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
sick, I'll send you an invoice you can pay up when you're rich 😜
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?
Docs PR always better!
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...