0

I designed a RESTful web API that has an endpoint that outputs objects in this structure:

{
    "id": 2,
    "name": "Test Object",
    "owner": 3,
}

The "owner" is also represented by a separate API endpoint which gives more data about the particular user who owns the object. Neat.

What I'm trying to do is, upon retrieving the objects (either via query or get), to also look up each user by ID and replace the "owner" attribute of the objects with an object containing user data like a username and email address. For reference, I'm trying to use this in a template using ngRepeat.

Is there any practical way to do this? Or should I simply redesign the API to output the related data automatically? I haven't found any resources on RESTful APIs that point me in one direction or the other.

Thanks!

1 Answer 1

1

So there's a few approaches to this. The three that I've seen that seem to be the most popular are (in no particular order):

1) Have the related data automatically populated on that REST endpoint, always. If another "shape" needs to be presented, offer a different endpoint. This is possibly the most common among the home-grown API crowd.

2) Use Querystring params to allow the client to ask for certain properties to be expanded (this is the approach OData uses).

3) (possibly the most "in line" with the original REST concept) Either replace the owner property with the URL to the related entity, or have a separate collection of 'related links' appended to the object. This is the approach Neo4J uses in their REST API.

Obviously, these are all server-side things. If you want to do it purely in Angular, you'll probably end up writing custom logic in your Resource, or else perhaps create an interceptor. Not super clean, imo, unless the server exposes some kind of standard as mentioned above.

* Edit * If I were to investigate doing this purely on the Angular side, I'd look into adding an http interceptor to my Resource definition to intercept the response, fire off another request for the related resource, then transform the response to add the updated info to the main model. Since interceptors work in Promises, this should provide the same experience to the calling code as a normal request, though obviously take longer since you're firing off two (or more) Ajax calls for a single entity. This speed/ performance hit is the main reason I've not bothered doing it clientside yet.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the REST guidance! For the third approach, how would I implement that in Angular? That's what I'm primarily trying to do (or at least learn about), but the first approach would also be satisfactory as it would cut down on the number of requests I make to the API.
Like I said, those are all server side things.
I'm afraid there must have been a bit of confusion. I meant the third approach in terms of how ngResource would fetch the related entities from the links or IDs given by the API. Looking at your post history though it seems Angular isn't really your background (I may be wrong) so I'll probably just use the first approach to make things a bit simpler. Thanks!
I've done a fair bit of Angular as well, though more recently and I've never tried solving this particular problem in Angular.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.