-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove type check that prevents polymorphic types in API responses #7421
Conversation
c43a553
to
11e3cf3
Compare
`The 'type' for a RecordIdentifier cannot be updated once it has been set. Attempted to set type for '${wrapper}' to '${type}'.` | ||
); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @ryandoherty! Do you have a test addition we can verify this results in added behaviour?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not, I can give it a whirl. First time working on anything Ember, happy to take pointers to where/how I should do this. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd note that we do have tests for single-table polymorphism support in place already. We might be able to improve our default implementation but the intent with identifiers is that you'll configure the cache in your own app to handle edge cases around one record having multiple potential identities.
Update on this: I am writing tests, my test case is failing because the way the store updates it passes the type that corresponds to the url and not the type that is returned to some functions to update from the api response. Will have an update in a few days. |
So I spent more than a few hours trying to get tests working only to learn that doing this will require a substantial rewrite of the store. The primary reason is the problem of how the store returns data from a findAll (and I'm assuming other calls). It loads up the RecordArray of the type of model it thinks it's fetching and returns it after the store has been populate with the response. But with this change, we could get back many types of objects, meaning we need to somehow merge all the RecordArrays together (I don't think this is possible b/c RecordArrays contain their type) or return a set of them. I get the feeling that this change would require a substantial rewrite and new API to handle the case of "I asked the store to find items of typeA and it returned typeB and typeC that are subclasses of typeA." I'm pretty new to Ember so unless I'm missing something I think this is as far as I can take this. I'd love to hear if I've understood all this correctly though, it was an interesting dive into Ember. This is the code I'm looking at
Thanks! |
@ryandoherty Great job digging in! What version of e-d are you running? We do have some relevant tests. It looks like we do have a test for "changing type". Do you happen to have a minimal reproduction (new ember app that reproduces this problem) we can use to make the test case? Or perhaps more insight on how these tests don't cover your scenario? |
11e3cf3
to
cd78e7d
Compare
cd78e7d
to
9e86443
Compare
@snewcomer I just pushed up my test case I created. Any pointers is appreciated, but if I'm correct then this request may be a big rewrite beyond a simple PR. |
@ryandoherty in your app/tests you want |
let handles = await store.findAll('handle'); | ||
|
||
let twitter_handles = store.peekAll('twitter-handles'); | ||
let github_handles = store.peekAll('github-handles'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be singular
{ | ||
data: [ | ||
{ | ||
type: 'github-handles', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be singular too, though this may not matter for the test
@ryandoherty I'm interested in getting this across the line. Do you want to pick it back up or should I take the baton? Happy to do whatever. |
Thanks Scott! You should pick it up, it's a bit more than I can handle at
the moment. Thanks!
-Ryan
…On Sun, Feb 6, 2022 at 6:50 PM Scott Newcomer ***@***.***> wrote:
@ryandoherty <https://github.com/ryandoherty> I'm interested in getting
this across the line. Do you want to pick it back up or should I take the
baton? Happy to do whatever.
—
Reply to this email directly, view it on GitHub
<#7421 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAALEPA5BX6UYM545OSIKOTUZ4XOZANCNFSM4WQ6FMQQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Closing in favor of #7955 for relationships and further RFC work for anything else. I'd note |
This is a PR for the request (#7357) to remove the assertion that prevents different types from being returned in API responses. Tests pass but I think there might need to be some tests added that assert Ember Data does 'the right thing' with varying types returned. (I have no idea how to do that :) )
Details:
tldr; This block:
data/packages/store/addon/-private/identifiers/cache.ts
Line 518 in ff4f911
Example:
Given the JSON api:
In our backend, 'dogs' and 'cats' are a subclass of 'animal', and queries to the /api/animals endpoint returns instances of cats and dogs. (This is via flask-restless & SQLAlchemy using the JSONAPI spec). Our Adapter is DS.JSONAPIAdapter
/api/animals returns:
We have setup our ember models to have a parent type of 'Animal', 'Dog' and 'Cat' extend it. When we query the store for all animals like so:
this.store.fetchAll('animals')
, the reference error above is thrown because ember data expects only the type 'animal', but received 'dog' and 'cat'.What we'd like ember to do is handle this and return an array (or individual object) of the types received. There are many use cases where we want to query and display all animals in the database, but we have to query each subtype separately. We've done some hacks to our API and ember app to support this (forcing all types for /api/animals to be 'animal' and jumping through some hoops after loaded to recreate the actual type on the frontend)
There are also situations when we know the id of the animal, but not type and would like to use
this.store.findRecord('animal', id)
and get back an instance of whatever subtype it is.I hope this is clear. From the comment in ember-data's code where the exception is thrown, it appears there is awareness that it would help to remove the check. Thank you!