Fetch deep-nested embedded relationships

refs https://github.com/TryGhost/Ghost/pull/9426/files
- when using `?include=foo` for a related model that itself normally has embedded relationships we need to add the nested relationship to the `include` param
- eg. `/posts?include=authors,authors.roles`
- this is necessary to ensure we don't introduce partial models into the Ember Data store by missing the embedded relationships on some requests
This commit is contained in:
Kevin Ansfield 2018-03-12 19:15:03 +00:00
parent b79946979e
commit 8d8dbb7479

View File

@ -115,17 +115,27 @@ export default BaseAdapter.extend({
getEmbeddedRelations(store, modelName) { getEmbeddedRelations(store, modelName) {
let model = store.modelFor(modelName); let model = store.modelFor(modelName);
let ret = []; let ret = [];
let embedded = [];
// Iterate through the model's relationships and build a list // Iterate through the model's relationships and build a list
// of those that need to be pulled in via "include" from the API // of those that need to be pulled in via "include" from the API
model.eachRelationship(function (name, meta) { model.eachRelationship((name, meta) => {
if (meta.kind === 'hasMany' if (
meta.kind === 'hasMany'
&& Object.prototype.hasOwnProperty.call(meta.options, 'embedded') && Object.prototype.hasOwnProperty.call(meta.options, 'embedded')
&& meta.options.embedded === 'always') { && meta.options.embedded === 'always'
) {
ret.push(name); ret.push(name);
embedded.push([name, meta.type]);
} }
}); });
embedded.forEach(([relName, modelName]) => {
this.getEmbeddedRelations(store, modelName).forEach((name) => {
ret.push(`${relName}.${name}`);
});
});
return ret; return ret;
} }
}); });