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

Support shallow routing for belongs_to? #180

Closed
jsamos opened this issue Apr 25, 2016 · 7 comments · Fixed by #315
Closed

Support shallow routing for belongs_to? #180

jsamos opened this issue Apr 25, 2016 · 7 comments · Fixed by #315

Comments

@jsamos
Copy link

jsamos commented Apr 25, 2016

Unless I am mistaken, it seems belongs_to resources only support nested routes for find, update and delete. Has there been any thought about supporting shallow routes? Currently in have to create two classes: one with the belongs_to defined and the other without.

@chingor13
Copy link
Collaborator

The belongs_to is there for nested routes. If your API returns link definitions like the jsonapi.org spec provides, the client can follow those links without having to declare them in code.

Personally, I don't like to nest routes because you are required to know the parent's id in order to access the child resource(s). Instead, it could be a flat, root resource with a filter by the parent id (which is totally optional).

Is the issue that you have both a flat and nested resource together?

@jsamos
Copy link
Author

jsamos commented May 9, 2016

Yes all of our apis have the shallow: true option set for nested routes.

so for instance we have something like

INDEX, CREATE
/orders/:id/invoices

and

SHOW, UPDATE, DESTROY
/invoices/:id

Currently to support that I need

class Invoice < Resource
end

class Order::Invoice <  Invoice
    belongs_to: order
end

@gottfrois
Copy link

Same issue here. Nested routes helps to reason about a resource in its context (an invoice comes from an order). But when you want to fetch invoice details, you simple hit the flat /invoices/{id} endpoint.

@remear
Copy link

remear commented Sep 16, 2016

My APIs use the same structure as described in other comments. I'd really like to see this implemented. Is there anything I/we could do to get this going?

@mltsy
Copy link

mltsy commented Nov 29, 2017

Has anybody tried adding link information to their serializers, as @chingor13 mentioned? I have not, but it sounds like that might be a pretty good workaround until this is supported. I'd love to know whether that works... if I get a chance, I'll try it out.

Obviously supporting shallow nesting without links would be great too. However, I could also see just dropping all forms of automagic URL construction other than /resources and /resources/1... but that would prevent this from being useful for JSON:API-compatible services that don't return links, so scratch that ;)

Perhaps customizable procs that take parameters (which would vary depending on the action) and return a URL... for instance (if you had some odd routes):

class Invoice < Base
  destroy_with ->(invoice) { [:delete, "/orders/#{invoice.order_id}/invoices/#{invoice.id}"] }
  create_with ->(invoice) { [:post, "/orders/#{invoice.order_id}/new_invoice"] }
  list_with ->(parent) { #  if API provides '/users/:id/invoices' and '/orders/:id/invoices'
    case parent
    when Order; "/orders/#{parent.id}/invoices"
    when User; "/users/#{parent.id}/invoices"
    end
  } 
  update_with ->(invoice) { :patch, "/orders/#{invoice.order_id}/invoices/#{resource.id}"]
end

@mltsy
Copy link

mltsy commented Nov 29, 2017

But then again, just supporting shallow nesting should probably be a simple flag on the model! ;)

class Invoice < Base
  belongs_to :order, shallow_paths: true
end

@gcrofils
Copy link

Any update on this ?

senid231 added a commit to senid231/json_api_client that referenced this issue Oct 26, 2018
senid231 added a commit to senid231/json_api_client that referenced this issue Oct 29, 2018
decouple attributes and path params

fix JsonApiClient#180
senid231 added a commit to senid231/json_api_client that referenced this issue Oct 29, 2018
decouple attributes and path params

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

Successfully merging a pull request may close this issue.

6 participants