[BUGFIX beta] Overhaul queryRecord#4300
[BUGFIX beta] Overhaul queryRecord#4300bmac merged 1 commit intowarp-drive-data:masterfrom pangratz:overhaul-queryRecord
Conversation
addon/-private/system/store.js
Outdated
| The request is made through the adapters' `queryRecord`: | ||
|
|
||
| ```javascript | ||
| // app/adapters/application.js |
There was a problem hiding this comment.
maybe pointing to app/adapters/user.js would be better here ?
There was a problem hiding this comment.
👍 You're absolutely right!
|
Thanks for the feedback @sly7-7! |
|
I think this is a good answer to related issues, disambiguate, and making the things consistent, I'm all for it |
|
This has been discussed in the team meeting and overall the taken approach looks good. I will take a look at #4310 to see if it can be addressed with this PR as well. |
addon/serializers/json-api.js
Outdated
| assert('Expected the primary data returned by the serializer for a `queryRecord` response to be a single object but instead it was an array.', false, { | ||
| id: 'ds.serializer.json-api.queryRecord-array-response' | ||
| }); | ||
| } |
There was a problem hiding this comment.
What are your thoughts on doing this in normalizeQueryRecordResponse?
|
I've updated this PR to address the following issues:
|
|
Just to clarify the clarification, does this PR ensure that |
@taras yes, |
|
Cool, thank you :) |
addon/serializers/json-api.js
Outdated
| if (Array.isArray(normalized.data)) { | ||
| assert('Expected the primary data returned by the serializer for a `queryRecord` response to be a single object but instead it was an array.', false, { | ||
| id: 'ds.serializer.json-api.queryRecord-array-response' | ||
| }); |
There was a problem hiding this comment.
Instead of wrapping this in an if block should we replace false with !Array.isArray(normalized.data) so it gets stripped correctly is a prod build?
There was a problem hiding this comment.
Absolutely! Don't know why I chose the if here 😕
Currently the use case for `store.queryRecord` is not communicated very well and there has been some confusion on what the expected server response should look like (array, or an object). There are several issues with the current code base: - in general, if the serializer returns an array for the primary data returned by `normalizeQueryRecordResponse`, then `store.queryRecord` resolves with an array - if rest serializer is used and an array is returned by the adapter, then the first entry of the array is used as primary data and `store.queryRecord` resolves with the first record - if json-api serializer is used and the primary data is an array, then `store.queryRecord` resolves with an array of the primary records - the API documentation for `queryRecord` is similar to `query` and doesn't indicate when this method should be used in contrast to `store.query` and getting the first record of the array - an assertion for the payload returned by the adapter for `queryRecord` not being an empty object has been added in 2.4, which is a regression to the behavior of 2.3 ----------------------------------------------------------------------- This commit addresses the above issues and makes the following changes: - add assertion that `store.queryRecord` never resolves with an array - add deprecation warning for the rest-serializers' `queryRecord`, if an array is returned instead of a single record - removes the assertion that the returned payload from the adapter for `queryRecord` is not an empty object - add assertion within json-api serializer that the primary data of the normalized response is not an array
|
Thanks @pangratz |
Currently the use case for
store.queryRecordis not communicated verywell and there has been some confusion on what the expected server
response should look like (array, or an object).
There are several issues with the current code base:
returned by
normalizeQueryRecordResponse, thenstore.queryRecordresolves with an array
then the first entry of the array is used as primary data and
store.queryRecordresolves with the first recordstore.queryRecordresolves with an array of the primary recordsqueryRecordis similar toqueryanddoesn't indicate when this method should be used in contrast to
store.queryand getting the first record of the arrayqueryRecordnot being an empty object has been added in 2.4, which is a regression
to the behavior of 2.3
This commit addresses the above issues and makes the following changes:
store.queryRecordnever resolves with an arrayqueryRecord, if anarray is returned instead of a single record
queryRecordis not an empty objectnormalized response is not an array
I am not so sure about the deprecation for the
rest-serializer, since it looks like currently alsofindRecordworks when an array is returned (see this test). But sincejson-api-adapterdoesn't allowdata: [...]responses forqueryRecord, this seems like an inconsistency which is a little surprising IMHO¯\_(ツ)_/¯.This addresses issues raised in #3977, #4227 and #4255.