Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,48 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function(hooks)
assert.equal(posts.get('firstObject.title'), 'Ember.js rocks', 'Sets correct title to record');
});

test('find many polymorphic records', async function(assert) {
assert.expect(4);

ajaxResponse([
{
data: [
{
type: 'github-handles',
Copy link
Contributor

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

id: '1',
attributes: {
username: 'ykatz',
},
},
{
type: 'twitter-handles',
id: '2',
attributes: {
nickname: 'ykatz',
},
},
],
included: [],
},
]);

let handles = await store.findAll('handle');

let twitter_handles = store.peekAll('twitter-handles');
let github_handles = store.peekAll('github-handles');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be singular


//This should work b/c we loaded these types from the API, but it doesn't
assert.equal(twitter_handles.get('length'), 1);
assert.equal(github_handles.get('length'), 1);

assert.equal(passedUrl[0], '/handles', 'Builds correct URL');

//These won't work because we didn't load anything of the base type 'handles'
assert.equal(handles.get('length'), 2, 'Returns the correct number of records');
assert.equal(handles.get('firstObject.username'), 'ykatz', 'Sets correct username to record');
assert.equal(handles.get('secondObject.nickname'), 'ykatz', 'Sets correct username to record');
});

test('queryRecord - primary data being a single record', async function(assert) {
ajaxResponse([
{
Expand Down
8 changes: 0 additions & 8 deletions packages/store/addon/-private/identifiers/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,6 @@ function performRecordIdentifierUpdate(
updateFn: UpdateMethod
) {
let { id, lid } = data;
let type = data.type && normalizeModelName(data.type);

if (DEBUG) {
// get the mutable instance behind our proxy wrapper
Expand Down Expand Up @@ -526,13 +525,6 @@ function performRecordIdentifierUpdate(
}
}

// TODO consider just ignoring here to allow flexible polymorphic support
if (type && type !== identifier.type) {
throw new Error(
`The 'type' for a RecordIdentifier cannot be updated once it has been set. Attempted to set type for '${wrapper}' to '${type}'.`
);
}

Copy link
Contributor

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?

Copy link
Author

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!

Copy link
Contributor

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.

updateFn(wrapper, data, 'record');
} else {
updateFn(identifier, data, 'record');
Expand Down