adelin's avatar

API design/approach

Hello!

Target: I am working on an app and i want to design it's API a bit like in "incremental-api-development" series. [https://laracasts.com/series/incremental-api-development]

Problem: I want the API to be used directly by the web-app, but the API design means that i need to make multiple requests in order to retrieve more information about one entity. E.g.: One post can have multiple tags, but i don't want the tags that belong to that post to be retrieved in a 2nd request when the request is made "by the webapp"

Question: Is it a way to design the API so that i can leverage eloquent's relations? Would this be a "normal" thing to do in order to not double the work load? Am i over-complicating things?

I'm opened to suggestions and ideas or techniques you've used/you'd use in this kind of situations.

Thank you.

0 likes
5 replies
ehtasham's avatar

you can get relative data with with joins and data can send as object through call.

Example:

$userInfo = User::join('user_detail', 'user.id', '=', 'user_detail.user_id')->where('user.type', '1')->get();

return response()->json(['status'=> 1, 'data' => $userInfo, 'message' => 'All User detail with type 1'], 200); 

Some thing like above.

adelin's avatar

I understand how that works, but it doesn't really answer my question. In the end i want to make an API that has some extra fields in case the request is made directly from the web-app. So instead of making X requests to get what all my app requires upon load, make only one. Main idea is to not over-complicate it and/or not duplicate the code/logic.

gregrobson's avatar
Level 6

@boovad - I think you're referring to the trade off between being a purist (each route returns one resource - and nothing that's related), and being a bit more adaptable (e.g. when you query /books/4 you also return details of the authors in the same JSON response as the book details).

I've tried sticking to JSON API for some personal work, but related resources don't really fit cleanly into a Vue.js architecture. http://jsonapi.org/format/upcoming/

I recommend reading Phil Sturgeon's book "Build APIs you won't hate" https://apisyouwonthate.com/

Largely, there's a trade off, if the related resources are low (e.g. three authors when you get the details of a book) it's probably quicker to return everything in one request (especially if your designing for mobile devices). However, if you have a lot of related resources (e.g. posts on the first page of a forum), you might want to retrieve a restrictive set of fields (just id, title and author when requesting "/forum/5") and allow the user to make further requests "/posts/1234" to get the content, timestamp of any individual post they click.

The important thing is to be consistent in your design and approach. JSON-API works 90% of the time for me (I use meta for Vue.js select box lists and details of required fields), but the related resources just won't fit with Vue.js neatly!

Hope that helps :-)

1 like
adelin's avatar

Yes, you are right. My target is to have a pure API (even related information, but not directly in response - like linking) but add some flavor for the app itself (relation data, directly in response). Main purpose is to minimize the # of requests or have it all fetched in one request. And yes i'll probably use a bit of magic to handle that part ("when" to add relations and filtering the data).

PS: I am also using vuejs

PPS: That book is already on my todos :D

jason's avatar

Maybe your looking for something like l5-repository?

https://github.com/andersao/l5-repository

It allows you make requests to your API like this:

/author/5?include=books.publisher

This would return the author with id 5. A collection of all books by this author would be nested within the author data and keyed off of books. You can get additional related data with dot notation, in this case each book would have a publisher key with the value being publisher data.

I've had decent success with it for simple relational schema. Things can get a bit verbose on the front-end for complex relationships.

Please or to participate in this conversation.