manyminds / api2go

JSONAPI.org Implementation for Go
MIT License
700 stars 94 forks source link

Creating a related resource #257

Open arcijsg opened 8 years ago

arcijsg commented 8 years ago

First of all, thanks for the great library - it's nice to see how this project evolves and how responsive the authors are. For me it is a good example on how to drive a small open-source project.

Moving on to the question - i'm creating a simple invoicing api, where we have three main resources: /invoices - is the main, self-sufficient resource: client creates an empty invoice draft by POSTing to /invoices, and then slowly adds details (line items (/lines resource) and /discounts) one by one;

The later two: /lines and /discounts - are not meaningful without the invoice. So, i found myself wishing to be able to create the following endpoints:

POST /invoices/<id>/lines POST /invoices/<id>/discounts

where a line items and discounts for the invoice drasft can be added. Currently, if foud no way on how to create such endpoints for dependent resources without creating custom handler and losing all the goodness the api2go.CRUD handlers provide.

I see that the easiest solution would be to post to /lines and /discounts directly, and provide linkage with the invoice in payload, under the "relationships" section, for example:

{
  "data": {
    "type": "lines",
    "attributes": {
      "description": "Mostest bestest Everest",
      "quantity": 1,
    }
  },
  "relationships": {
    "invoice": {
      "data": { "type": "invoices", "id": "121" }
    }
  }
}

I might go that way, however, i'd like to ask what do you guys think about making available such endpoints for dependent resources?

Would it be hard to implement in addResource, or would it be against the spirit behind this library, or not in line of jsonapi.org specification at all (although i could not find anything like that yet in the specification or discussions)?

sharpner commented 8 years ago

hey :) glad you like the library!

We are already thinking about integration of custom subroutes, but have not yet found a clever way to do this.

The issues #244 #256 are already waiting for it, so we will support it somewhere in the future, but it might take a while :)

If you have an idea, feel free to send us a PR!

arcijsg commented 8 years ago

Thanks for the quick response!

I'm afraid i still have too little knowledge on how to properly program in Go to do a contribution yet, but i'm really looking forward to the solution :)