Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST returning an object graph

I am new to the REST architecural design, however I think I have the basics of it covered.

I have a problem with returning objects from a RESTful call. If I make a request such as http://localhost/{type A}/{id} I will return an instance of A from the database with the specified id.

My question is what happens when A contains a collection of B objects? At the moment the XML I generate returns A with a collection of B objects inside of it. As you can imagine if the B type has a collection of C objects then the XML returned will end up being a quite complicated object graph.

I can't be 100% sure but this feels to be against the RESTful principles, the XML for A should return the fields etc. for A as well as a collection of URI's to the collection of B's that it owns.

Sorry if this is a bit confusing, I can try to elaborate more. This seems like a relatively basic question, however I can't decide which approach is "more" RESTful.

Cheers,

Aidos

like image 392
Aidos Avatar asked Dec 23 '08 01:12

Aidos


1 Answers

One essential RESTful principle is that everything has a URI.

You have URI's like this.

  • /A/ and /A/id/ to get a list of A's and a specific A. The A response includes the ID's of B's.
  • /B/ and /B/id/ to get a list of B's and a specific B. The B response includes the ID's of C's.
  • /C/ and /C/id/ to get a list of C's and a specific C.

You can, through a series of queries, rebuild the A-B-C structure. You get the A, then get the relevant B's. When getting a B, you get the various C's that are referenced.


Edit

Nothing prevents you from returning more.

For example, you might have the following kinds of URI's.

  • /flat/A/id/, /flat/B/id/ and /flat/C/id/ to return "flat" (i.e., no depth) structures.

  • /deep/A/id/, /deep/B/id/ and /deep/C/id/ to return structures with complete depth.

The /deep/A/id/ would be the entire structure, in a big, nested XML document. Fine for clients that can handle it. /flat/A/id/ would be just the top level in a flat document. Best for clients that can't handle depth.

like image 187
S.Lott Avatar answered Oct 06 '22 02:10

S.Lott