Skip to content

Commit

Permalink
fix: fix wrong accumulator return type for initializeModel function (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
zxl629 authored and vaisshnavi7 committed Nov 21, 2024
1 parent 5e4153d commit f8d115c
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/polite-melons-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"integration-tests": minor
"@aws-amplify/data-schema": minor
---

Fix: change accumulator return type for initializeModel function
3 changes: 2 additions & 1 deletion packages/data-schema/src/runtime/internals/APIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,9 @@ export function initializeModel(
// eslint-disable-next-line array-callback-return
(acc: Record<string, any>, curVal) => {
if (record[curVal]) {
return (acc[curVal] = record[curVal]);
acc[curVal] = record[curVal];
}
return acc;
},
{},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
buildAmplifyConfig,
mockedGenerateClient,
optionsAndHeaders,
expectGraphqlMatches,
useState,
} from '../../utils';

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

type Schema = ClientSchema<typeof schema>;

const compositeKeySchema = a.schema({
Order: a.model({
id: a.id(),
customerFirstName: a.string().required(),
customerLastName: a.string().required(),
customerRegion: a.string().required(),
customer: a.belongsTo('Customer', ['customerFirstName','customerLastName', 'customerRegion']),
}).authorization(allow => [allow.publicApiKey()]),
Customer: a.model({
customerFirstName: a.string().required(),
customerLastName: a.string().required(),
customerRegion: a.string().required(),
orders: a.hasMany('Order', ['customerFirstName','customerLastName', 'customerRegion']),
}).identifier(['customerFirstName', 'customerLastName','customerRegion']).authorization(allow => [allow.publicApiKey()]),
})

type CompositeKeySchema = ClientSchema<typeof compositeKeySchema>;

test('belongsTo: related data is queried with foreign keys', async() => {
const { generateClient, spy} = mockedGenerateClient([
{
data: {
getOrder: {
__typeName: 'Order',
id: 'order-id',
customerFirstName: 'Some First Name',
customerLastName: 'Some Last Name',
customerRegion: 'Region A',
updatedAt: '2024-03-01T19:05:44.536Z',
createdAt: '2024-03-01T18:05:44.536Z',
},
},
},
{
data: {
getCustomer: {
__typeName: 'Customer',
customerFirstName: 'Some First Name',
customerLastName: 'Some Last Name',
region: 'Region A',
orders: [
{
__typeName: 'Order',
id: 'order-id',
customerFirstName: 'Some First Name',
customerLastName: 'Some Last Name',
customerRegion: 'Region A',
updatedAt: '2024-03-01T19:05:44.536Z',
createdAt: '2024-03-01T18:05:44.536Z',
},
],
updatedAt: '2024-03-01T19:05:44.536Z',
createdAt: '2024-03-01T18:05:44.536Z',
},
},
},
{
data: {
getCustomer: {
__typeName: 'Customer',
customerFirstName: 'Another First Name',
customerLastName: 'Another Last Name',
customerRegion: 'Region B',
orders: [
],
updatedAt: '2077-03-01T19:05:44.536Z',
createdAt: '2077-03-01T18:05:44.536Z',
},
},
},
]);
const config = await buildAmplifyConfig(compositeKeySchema);
Amplify.configure(config);
const client = generateClient<CompositeKeySchema>();

const { data: order } = await client.models.Order.get({
id: 'order-id',
});
const { data: customer } = await order!.customer();

//both primary key and sort key are used in query
expectGraphqlMatches(
optionsAndHeaders(spy)[1][0].query,
`query($customerFirstName: String!,$customerLastName: String!, $customerRegion: String!)
{ getCustomer(customerFirstName: $customerFirstName,customerLastName: $customerLastName,
customerRegion: $customerRegion)
{ customerFirstName customerLastName customerRegion createdAt updatedAt } }`,
);

//sort keys are processed by the reduce() correctly
expect(optionsAndHeaders(spy)[1][0].variables).toEqual({
customerFirstName: 'Some First Name',
customerLastName: 'Some Last Name',
customerRegion: 'Region A',
})
});

describe('when target data does not exist', () => {
test('hasMany: returns []', async () => {
const { generateClient } = mockedGenerateClient([
Expand Down

0 comments on commit f8d115c

Please sign in to comment.