Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to "create" associated resources at the same time as the main resource? #270

Open
carlisia opened this issue Nov 11, 2016 · 15 comments

Comments

@carlisia
Copy link

I'm hitting a post endpoint for a resource that has a relation. To use your example, it would be much like posting a user that has data for two chocolates.

I notice that the resource user gets created, but not the associated chocolates.

I am now doubting that this is even possible and would love it if you could confirm one way or the other. When I look at your example for a post, I see that there is no payload for the association:

https://github.com/manyminds/api2go/blob/master/examples/crud_example_test.go#L60

On my end, I've debugged enough to see that when SetToManyReferenceIDs is called, the IDs that are passed in are correct in number (there are two associations and there are two IDs), but they are set to zero. This makes sense because at this point create hasn't been called yet.

When create does get called, I inspect the obj and the array for the association (which would be chocolates) is empty. If it wasn't empty I could grab the data and save it to the db.

I have implemented all the interfaces to make associations work, at least I'm pretty sure I have.

@wwwdata
Copy link
Member

wwwdata commented Nov 11, 2016

Hi,

this should work, however the example is very basic and I haven't implemented that in the code. I will try to add this feature in the example and extend the example tests to verify that this really works when implemented.

@wwwdata wwwdata self-assigned this Nov 11, 2016
@carlisia
Copy link
Author

Hey @wwwdata I'd appreciate that. Especially since the alternative is to create one resource then make individual api calls for each relation.

I think I was very diligent in making sure I was doing everything correctly, so I'm looking forward to the results of your test.

@wwwdata
Copy link
Member

wwwdata commented Nov 11, 2016

I found the problem in our examples. As expected I just made a mistake. When reference IDs are in the payload, the SetToManyReferenceIDs and SetToOneReferenceID depending on the reference should be called in your resource model. Depending on what you do there, this should be working.

@carlisia
Copy link
Author

Ok I will make that change. But still, I noted that in the updated example the ID is 1 (https://github.com/manyminds/api2go/pull/271/files#diff-f6c08d9b83fa8e3e50949d71d299271dR223). But it can't be 1 in real life because it hasn't been created yet. So it will always be zero, correct?

@wwwdata
Copy link
Member

wwwdata commented Nov 11, 2016

ah, now I understand what you mean. You want to create the user and new referenced data in one request. So one user and 2 chocolate datasets. Hmm. Are you trying to send the chocolates in the included part of the JSON?

I think there also is no official example in the jsonapi.org spec, right? They only have examples with reference IDs.

@carlisia
Copy link
Author

That is precisely what I'm trying to do.

@carlisia
Copy link
Author

Yes, the chocolates are in the included part of the JSON. That's actually how api2go spits it out after the struct (user) with the slice of associated struct (chocolates) is marshalled.

@wwwdata
Copy link
Member

wwwdata commented Nov 11, 2016

api2go cannot do that for now. Unmarshaling is only working with IDs. One other problem we have is how we manage the IDs of the new chocolates. The included section can include many references. There are also scenarios possible where the same included types can belong to different relationships.

We should look a bit more into the jsonapi specification if anything is specified how to do this properly or if something is in the works that we can early adopt. Did you find anything in their documentation how to correctly send payload to create relationships along the the main object @carlisia ?

@carlisia
Copy link
Author

Hm. This is interesting. Looking now I see that they specify how to do a post with an association if it is something like, posting a chocolate with an association back to the user: http://jsonapi.org/format/#crud-creating

Nothing yet on how to do it the other way around.

@sharpner
Copy link
Member

sharpner commented Nov 11, 2016

This feature seems to be planned for 1.1 http://discuss.jsonapi.org/t/json-api-weekly-meeting-june-22nd-2015/23 but we only support 1.0 at the moment..

@svperfecta
Copy link

This seems like a common problem. I found several folks complaining about the lack of atomicity in create calls to JSON API. My solution was the same as @carlisia, I'd like to post the main object, and it's related object in the included array.

Like @carlisia i think the simple solution would be to include the entire object (in raw format) as part of the request object (or the object, object) so that I could at least implement this myself if I wanted to.

@carlisia
Copy link
Author

It seems that this is the issue where this feature is being discussed: json-api/json-api#1089

@irene-m
Copy link

irene-m commented Jun 12, 2017

Wondering if there has been some update with resolving this issue?

@sharpner
Copy link
Member

unfortunately not, you should watch this issue json-api/json-api#1089

@irene-m
Copy link

irene-m commented Jun 12, 2017

too bad, thanks though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants