Deprecation Guide for Store.queryRecord Array Response with RESTSerializer
When using
DS.RESTSerializer
with previous versions of Ember Data,
store.queryRecord
provided support for normalizing payloads containing an array of primary data.
This behavior has been deprecated because it is basically the same as using
store.query
and returning the first model.
Deprecated payload example with an array as the primary data:
// GET /users?username="GummyBear"
{
"users": [{
"id": "1",
"username": "GummyBear"
}]
}
Expected payload example with a single object as the primary data:
// GET /users?username="GummyBear"
{
"user": {
"id": "1",
"username": "GummyBear",
}
}
If you need to support an API that responds with an array as the primary data,
you have a few options. The simplest option is to use
store.query
instead of
store.queryRecord
:
this.store.query('user', { username: 'GummyBear' }).then((users) => {
return users.objectAt(0);
});
Another option is to override
normalizeQueryRecordResponse
in your serializer, manipulating the payload so it matches the expected format:
import DS from 'ember-data';
export default DS.RESTSerializer.extend({
normalizeQueryRecordResponse(store, primaryModelClass, payload) {
if (payload.users) {
payload.user = payload.users[0];
delete payload.users;
}
return this._super(...arguments);
}
});
Another option is to customize the URL of the request made by
store.queryRecord
so that it makes a request that returns the expected payload with a single
object as its primary data. This can be done by overriding
urlForQueryRecord
in your adapter:
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
urlForQueryRecord() {
let baseURL = this.buildURL();
return `${baseURL}/user-query`;
}
});