JSON API Compound Documents & Sparse Fieldsets


#1

As Ember data 1.13 supports now JSON API 1.0 I’m wondering if it will be possible to take advantage of the Compound Documents defined by the JSON API to reduce the number of HTTP requests.

I’m still a very beginner regarding Ember data but I’d like to know if a JSON API request like

GET /articles?include=author

as shown here can be somehow modeled with Ember data. With the new Ember data 1.13 naively I’d do something like the following (sorry if the code is wrong but I hope you get what I mean)

// Issue HTTP request to fetch the first page of articles
let articles = this.store.findAll('articles');
articles.then(() ->
  let article = articles.objectsAt(0);
  // Will cause a second HTTP request to fetch the related author?
  article.get('author');
)

… or would I need to use store.query('articles', query) and define within the query the includes? Honestly so far I’ve no clue how to tell the store beforehand that it shall include the related author when fetching an article.

Also in order to reduce traffic it’d be awesome if you could declare that you’re only interested in a sub set of properties from a resource instead of all (see sparse fieldsets of JSON API). Similar to the example above I could be interested only in the title of the articles and the name of the related author. Such a request could be expressed with JSON API like:

GET /articles?include=author&fields[articles]=title&fields[people]=name

Is there any way to declare that you’re interested only in a sub set of properties so that (if supported by the Adapter) the request gets optimized?

Sorry in case I’m asking nonsense here.

Thanks a lot!


#2

Hi @nevson!

Ember Data 1.13 supports compound documents with resources located in the included key out of the box. This should work for all adapter methods.

There’s however currently no sugar for specifying what to include (via the include parameter) but that is planned for the 2.x version of Ember Data (so is Pagination, Filtering and Sparse Fieldsets). IIRC I think all find/save methods have the option of passing adapterOptions to the adapter so it should be fairly simple to use that in the meanwhile.


#3

Quick question: is specifying what to include now supported (can’t find … )?