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

Pass lid from relationship data to get record data #7425

Merged
merged 1 commit into from
Apr 8, 2021
Merged
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 @@ -154,7 +154,7 @@ module('Integration | Identifiers - lid reflection', function(hooks) {

class Cake extends Model {
@attr name;
@hasMany('ingredient', { async: false }) ingredients;
@hasMany('ingredient', { inverse: null, async: false }) ingredients;
}

this.owner.register('model:ingredient', Ingredient);
Expand All @@ -165,8 +165,10 @@ module('Integration | Identifiers - lid reflection', function(hooks) {
return payload;
}
}

class TestAdapter extends Adapter {
createRecord(store, ModelClass, snapshot) {
const lid = recordIdentifierFor(snapshot.record.ingredients.firstObject).lid;
return resolve({
data: {
type: 'cake',
Expand All @@ -180,7 +182,7 @@ module('Integration | Identifiers - lid reflection', function(hooks) {
{
type: 'ingredient',
id: '2',
lid: cheeseIdentifier.lid,
lid,
},
],
},
Expand All @@ -190,7 +192,7 @@ module('Integration | Identifiers - lid reflection', function(hooks) {
{
type: 'ingredient',
id: '2',
lid: cheeseIdentifier.lid,
lid,
attributes: {
name: 'Cheese',
},
Expand All @@ -213,11 +215,81 @@ module('Integration | Identifiers - lid reflection', function(hooks) {
const cheese = store.createRecord('ingredient', { name: 'Cheese' });
const cake = store.createRecord('cake', { name: 'Cheesecake', ingredients: [cheese] });

const cheeseIdentifier = recordIdentifierFor(cheese);

await cake.save();

assert.deepEqual(cake.hasMany('ingredients').ids(), ['2']);
assert.equal(cake.ingredients.objectAt(0).name, 'Cheese');
});

test('belongsTo() has correct state after .save() on a newly created record with sideposted child record when lid is provided in the response payload', async function(assert) {
class Topping extends Model {
@attr name;
}

class Cake extends Model {
@attr name;
@belongsTo('topping', { inverse: null, async: false }) topping;
}

this.owner.register('model:topping', Topping);
this.owner.register('model:cake', Cake);

class TestSerializer extends Serializer {
normalizeResponse(_, __, payload) {
return payload;
}
}

class TestAdapter extends Adapter {
createRecord(store, ModelClass, snapshot) {
const lid = recordIdentifierFor(snapshot.record.topping).lid;
return resolve({
data: {
type: 'cake',
id: '1',
attributes: {
name: 'Cheesecake',
},
relationships: {
topping: {
data: {
type: 'topping',
id: '2',
lid,
},
},
},
},
included: [
{
type: 'topping',
id: '2',
lid,
attributes: {
name: 'Cheese',
},
relationships: {
cake: {
data: {
type: 'cake',
id: '1',
},
},
},
},
],
});
}
}
this.owner.register('serializer:application', TestSerializer);
this.owner.register('adapter:application', TestAdapter);

const cheese = store.createRecord('topping', { name: 'Cheese' });
const cake = store.createRecord('cake', { name: 'Cheesecake', topping: cheese });

await cake.save();

assert.deepEqual(cake.belongsTo('topping').id(), '2');
assert.equal(cake.topping.name, 'Cheese');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export default class BelongsToRelationship extends Relationship {
);

if (recordData !== null) {
recordData = this.recordData.storeWrapper.recordDataFor(data.type, data.id);
recordData = this.recordData.storeWrapper.recordDataFor(data.type, data.id, data.lid);
}
this.setCanonicalRecordData(recordData);
}
Expand Down