-
Notifications
You must be signed in to change notification settings - Fork 35
Posting to nested routes #25
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
Comments
Hey @klobuczek, I do have an option for you, but would also like to better understand the use case. I've personally found these relationship endpoints to be unnecessary. Since these libs honor "sideposting", I either "sidepost" the comment using the Just as an example, using the jsorm client makes this easy: Post.find('1').then((response) => {
let post = response.data;
post.comments.push(new Comment({ content: 'Some comment' }));
post.save({ with: ['comments'] }).then((response) => { ... });
}); That said, you could do something like this to get the desired behavior, I think: before_action :merge_foreign_key
def merge_foreign_key
params[:data][:attributes].merge!(post_id: params[:id])
# or
deserialized_params.attributes.merge!(post_id: params[:id])
end |
@richmolj the way you are doing this in the jsorm snippet requires that the client has the entire list comments and PATCHES the |
Yeah. Unfortunately jsonapi has punted on nested relationships despite tons of need/issues on github. If this had been addressed, I feel pretty confident the specification would not require the POST to include every comment. It's a rather silly requirement - how often are you trying to fully replace the relationship versus add/update/destroy/disassociate a single entity? So, because I - and many others - believe this should be part of the spec, we deviate and do not require the full comments collection. I recognize deviating from the spec is very much less than ideal. I wish jsonapi would address this issue so we would not have to. FWIW, I now run only non-nested URLs with sideposting - no nested routes or relationship routes - and the only thing that changed was everything became a lot easier.
I avoid nested routes altogether and I haven't had problems so far. This appears to be the 'ember way' - it's rather hard to use ember-data with nested URLs, and you can see lots of discussion around this issue in the ember community. I would POST to Regarding the URL in the payload and the URL - I hear you, I'd be open to a PR to the Deserializer |
Hmm, I have heard that opinion before. jsonapi by itself is already hard for humans. Getting rid of nested routes is like removing the last part about the domain model which is still graspable to humans. |
@klobuczek I'm not unsympathetic to nested routes, but I'm currently leaning against out-of-the-box support. The root of the issue is, I dislike two ways to do the same thing. Once you have sideposting - which is a must have in my opinion, since sometimes things need to run within a transaction - the nested routes become another way to do the same thing. Regarding human readability - I think it's mostly personal preference and what use case you are optimizing for. I personally think nested routes / jsonapi relationship routes are unnecessarily confusing and are inherently less performant (due to multiple http requests needed). The sideposting payloads are hard to read for humans, completely agree there...but this is mostly mitigated by the client you are using. This follows the same tradeoff as If you're looking for canonical traversal, I suggest All that said, I would be in favor of a separately maintained library. |
In a case of
/posts/1/comments
and a payloadI would like to be able to add the relationship to
posts
without the client having to specify the relationship in the payload. I really hope this is compliant with the spec.How would I implement this in json_api compliable?
jsonapi_create
does not take any params, but it would be helpful if it did.The text was updated successfully, but these errors were encountered: