How to do relationships in API Star?


#1

This might be too generic a question for this forum; or it maybe a fundamental question about API design in general.

I have my relationship setup in the database and it pretty much goes project (parent) --> location (child), now I’m quite lost in terms of how to construct my routes as it doesn’t seem to be dealt with in the documentation and I would be purely guessing. My amateur attempt is on github:

https://github.com/niko86/lab_api

Additionally, the /docs/ page lets me enter data and when i make a location it just creates a record with no obvious way to link it to the parent project.


#2

Yeah I think this is a question more about ReST API design. So if there is a one to many association between a project and a location I would probably have a path that looks like this:

/projects/{project_id}/locations/{location_id}}

So you Post new projects to /projects. You Put/Patch/Delete changes to a project at /projects/{project_id}. Post new locations to a project at /projects/{project_id}/locations. Then Put/Patch/Delete changes to a location at /projects/{project_id}/locations/{location_id}.

This is just based off the assumption that locations for a project are strict one to many and not many to many. Then you have to think about “do you embed a resource within it’s parent, or do you use HATEOS and just link the location for where a parent’s children can be found?”.

Keep in mind ReST is a style. People do it a bunch of different ways and to varying degrees. Hell I could probably argue that I am not doing it right either.


#3

Thanks drew, I see I was missing the project/{code}/location/{location} in my route. I think rather than continue muddling along is best go and research some more in terms of theory.