我需要能够基于对象集合来获取集合。为此,我正在考虑几种方法,但不确定哪一种最有意义,所以我将两种方法都发布。如果有我可能没有考虑过的更好的方法,请随时发表您的建议:)
例如,我将使用产品和评论(即产品评论)作为资源。
目标: 我们希望获得所有产品的评论
方法A:
POST api/Products { json: filters, limits, etc } // returns api/Products/<id>
GET api/Products/<id>/Reviews { json: filters, limits, etc } // returns json array
// ... of reviews present in products
// id in this case would refer to the collection, which would be cached
方法B:
GET api/Reviews { json: filters } // returns json array of reviews, but needs to
// ... have either all of the product ids passed as an array (not practical with
// ... huge collections), or needs to have all of the api/Products filters passed,
// ... in which case making the code unDRY
方法C:
GET api/Reviews { json: filters: { products: 'api/Products?filters' }...
// is this reasonable?
提前感谢您的任何帮助,如果这个问题很菜鸟,我深表歉意,我对 REST 还很陌生
方法D:
access individual products
GET api/Product/<id>
access collection of products:
POST api/Products?filters=stuff..
GET api/Products/<id>
access collection of products' reviews
POST api/Products/<id>/Reviews?filters=stuff
GET api/Products/<id>/Reviews/<id>
... this would thus allow us to cache the result easily, does this seem reasonable to you?
目标:我们希望获得所有产品的评论
基本上,
GET /api/products/<id>
应返回一个 product
,而 GET /api/products/<id>/reviews
应返回给定 reviews
的所有 product
。
如果你想获取所有
products
,你将使用GET /api/products
。当您将 reviews
和 products
视为资源时,我建议您公开所有 reviews
:
GET /api/reviews
但是您可能想要
products
和他们的reviews
。这意味着使用 /api/products
(例如一组过滤器)获取一些产品,并使用 reviews
扩展结果集。这个
expand
术语来自 OData 协议:http://www.odata.org/documentation/uri-conventions#ExpandSystemQueryOption.
你可以这样做:
GET /api/products?$expand=reviews
GET /api/products?filter=foo&$expand=reviews
此 URI 的意思是:“标识
products
的集合以及与每个 reviews
关联的每个 product
。
参见 GraphQL 它可以暗示查询/选择功能