Skip to content

Commit 068f8af

Browse files
authored
fix: fix wrong accumulator return type for initializeModel function (#396)
1 parent fe1943e commit 068f8af

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

.changeset/polite-melons-build.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"integration-tests": minor
3+
"@aws-amplify/data-schema": minor
4+
---
5+
6+
Fix: change accumulator return type for initializeModel function

packages/data-schema/src/runtime/internals/APIClient.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,9 @@ export function initializeModel(
243243
// eslint-disable-next-line array-callback-return
244244
(acc: Record<string, any>, curVal) => {
245245
if (record[curVal]) {
246-
return (acc[curVal] = record[curVal]);
246+
acc[curVal] = record[curVal];
247247
}
248+
return acc;
248249
},
249250
{},
250251
);

packages/integration-tests/__tests__/defined-behavior/2-expected-use/lazy-loaders-edges.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
buildAmplifyConfig,
66
mockedGenerateClient,
77
optionsAndHeaders,
8+
expectGraphqlMatches,
89
useState,
910
} from '../../utils';
1011

@@ -35,6 +36,103 @@ describe('lazy loaders edges', () => {
3536

3637
type Schema = ClientSchema<typeof schema>;
3738

39+
const compositeKeySchema = a.schema({
40+
Order: a.model({
41+
id: a.id(),
42+
customerFirstName: a.string().required(),
43+
customerLastName: a.string().required(),
44+
customerRegion: a.string().required(),
45+
customer: a.belongsTo('Customer', ['customerFirstName','customerLastName', 'customerRegion']),
46+
}).authorization(allow => [allow.publicApiKey()]),
47+
Customer: a.model({
48+
customerFirstName: a.string().required(),
49+
customerLastName: a.string().required(),
50+
customerRegion: a.string().required(),
51+
orders: a.hasMany('Order', ['customerFirstName','customerLastName', 'customerRegion']),
52+
}).identifier(['customerFirstName', 'customerLastName','customerRegion']).authorization(allow => [allow.publicApiKey()]),
53+
})
54+
55+
type CompositeKeySchema = ClientSchema<typeof compositeKeySchema>;
56+
57+
test('belongsTo: related data is queried with foreign keys', async() => {
58+
const { generateClient, spy} = mockedGenerateClient([
59+
{
60+
data: {
61+
getOrder: {
62+
__typeName: 'Order',
63+
id: 'order-id',
64+
customerFirstName: 'Some First Name',
65+
customerLastName: 'Some Last Name',
66+
customerRegion: 'Region A',
67+
updatedAt: '2024-03-01T19:05:44.536Z',
68+
createdAt: '2024-03-01T18:05:44.536Z',
69+
},
70+
},
71+
},
72+
{
73+
data: {
74+
getCustomer: {
75+
__typeName: 'Customer',
76+
customerFirstName: 'Some First Name',
77+
customerLastName: 'Some Last Name',
78+
region: 'Region A',
79+
orders: [
80+
{
81+
__typeName: 'Order',
82+
id: 'order-id',
83+
customerFirstName: 'Some First Name',
84+
customerLastName: 'Some Last Name',
85+
customerRegion: 'Region A',
86+
updatedAt: '2024-03-01T19:05:44.536Z',
87+
createdAt: '2024-03-01T18:05:44.536Z',
88+
},
89+
],
90+
updatedAt: '2024-03-01T19:05:44.536Z',
91+
createdAt: '2024-03-01T18:05:44.536Z',
92+
},
93+
},
94+
},
95+
{
96+
data: {
97+
getCustomer: {
98+
__typeName: 'Customer',
99+
customerFirstName: 'Another First Name',
100+
customerLastName: 'Another Last Name',
101+
customerRegion: 'Region B',
102+
orders: [
103+
],
104+
updatedAt: '2077-03-01T19:05:44.536Z',
105+
createdAt: '2077-03-01T18:05:44.536Z',
106+
},
107+
},
108+
},
109+
]);
110+
const config = await buildAmplifyConfig(compositeKeySchema);
111+
Amplify.configure(config);
112+
const client = generateClient<CompositeKeySchema>();
113+
114+
const { data: order } = await client.models.Order.get({
115+
id: 'order-id',
116+
});
117+
const { data: customer } = await order!.customer();
118+
119+
//both primary key and sort key are used in query
120+
expectGraphqlMatches(
121+
optionsAndHeaders(spy)[1][0].query,
122+
`query($customerFirstName: String!,$customerLastName: String!, $customerRegion: String!)
123+
{ getCustomer(customerFirstName: $customerFirstName,customerLastName: $customerLastName,
124+
customerRegion: $customerRegion)
125+
{ customerFirstName customerLastName customerRegion createdAt updatedAt } }`,
126+
);
127+
128+
//sort keys are processed by the reduce() correctly
129+
expect(optionsAndHeaders(spy)[1][0].variables).toEqual({
130+
customerFirstName: 'Some First Name',
131+
customerLastName: 'Some Last Name',
132+
customerRegion: 'Region A',
133+
})
134+
});
135+
38136
describe('when target data does not exist', () => {
39137
test('hasMany: returns []', async () => {
40138
const { generateClient } = mockedGenerateClient([

0 commit comments

Comments
 (0)