Skip to content

Commit

Permalink
fix: add optional chaining (#1156)
Browse files Browse the repository at this point in the history
* fix: add optional chaining

---------

Co-authored-by: Jacob Logan <[email protected]>
  • Loading branch information
jacoblogan and Jacob Logan authored Feb 26, 2024
1 parent 575cbcc commit 70799cb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32139,7 +32139,8 @@ export default function UpdateOrgForm(props) {
const queryData = async () => {
const record = idProp ? await DataStore.query(Org, idProp) : orgModelProp;
setOrgRecord(record);
const linkedComments = record ? await record.comments.toArray() : [];
const linkedComments =
(record && (await record.comments?.toArray())) || [];
setLinkedComments(linkedComments);
};
queryData();
Expand Down Expand Up @@ -32758,15 +32759,12 @@ export default function UpdateCompositeDogForm(props) {
? await record.CompositeOwner
: undefined;
setCompositeOwner(CompositeOwnerRecord);
const linkedCompositeToys = record
? await record.CompositeToys.toArray()
: [];
const linkedCompositeToys =
(record && (await record.CompositeToys?.toArray())) || [];
setLinkedCompositeToys(linkedCompositeToys);
const linkedCompositeVets = record
? await Promise.all(
(
await record.CompositeVets.toArray()
).map((r) => {
((await record.CompositeVets?.toArray()) || []).map((r) => {
return r.compositeVet;
})
)
Expand Down Expand Up @@ -33887,17 +33885,14 @@ export default function UpdateCPKTeacherForm(props) {
setCPKStudent(CPKStudentRecord);
const linkedCPKClasses = record
? await Promise.all(
(
await record.CPKClasses.toArray()
).map((r) => {
((await record.CPKClasses?.toArray()) || []).map((r) => {
return r.cpkClass;
})
)
: [];
setLinkedCPKClasses(linkedCPKClasses);
const linkedCPKProjects = record
? await record.CPKProjects.toArray()
: [];
const linkedCPKProjects =
(record && (await record.CPKProjects?.toArray())) || [];
setLinkedCPKProjects(linkedCPKProjects);
};
queryData();
Expand Down Expand Up @@ -37256,15 +37251,12 @@ export default function UpdateCompositeDogForm(props) {
? await record.CompositeOwner
: undefined;
setCompositeOwner(CompositeOwnerRecord);
const linkedCompositeToys = record
? await record.CompositeToys.toArray()
: [];
const linkedCompositeToys =
(record && (await record.CompositeToys?.toArray())) || [];
setLinkedCompositeToys(linkedCompositeToys);
const linkedCompositeVets = record
? await Promise.all(
(
await record.CompositeVets.toArray()
).map((r) => {
((await record.CompositeVets?.toArray()) || []).map((r) => {
return r.compositeVet;
})
)
Expand Down Expand Up @@ -46067,7 +46059,8 @@ export default function SchoolUpdateForm(props) {
? await DataStore.query(School, idProp)
: schoolModelProp;
setSchoolRecord(record);
const linkedStudents = record ? await record.Students.toArray() : [];
const linkedStudents =
(record && (await record.Students?.toArray())) || [];
setLinkedStudents(linkedStudents);
};
queryData();
Expand Down Expand Up @@ -46643,7 +46636,7 @@ export default function SchoolUpdateForm(props) {
? await DataStore.query(School, idProp)
: schoolModelProp;
setSchoolRecord(record);
const linkedStudent = record ? await record.Student.toArray() : [];
const linkedStudent = (record && (await record.Student?.toArray())) || [];
setLinkedStudent(linkedStudent);
};
queryData();
Expand Down Expand Up @@ -47890,9 +47883,7 @@ export default function TagUpdateForm(props) {
setTagRecord(record);
const linkedPosts = record
? await Promise.all(
(
await record.Posts.toArray()
).map((r) => {
((await record.Posts?.toArray()) || []).map((r) => {
return r.post;
})
)
Expand Down Expand Up @@ -55329,7 +55320,7 @@ export default function UpdateDealershipForm(props) {
? await DataStore.query(Dealership, idProp)
: dealershipModelProp;
setDealershipRecord(record);
const linkedCars = record ? await record.cars.toArray() : [];
const linkedCars = (record && (await record.cars?.toArray())) || [];
setLinkedCars(linkedCars);
};
queryData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe('amplify form renderer tests', () => {
expect(componentText).toContain('const postRecords = useDataStoreBinding({');

// check lazy load linked data
expect(componentText).toContain('await record.Posts.toArray()');
expect(componentText).toContain('((await record.Posts?.toArray()) || []).map((r)');

// check custom display value is set
expect(componentText).toContain('Posts: (r) => r?.title');
Expand Down Expand Up @@ -228,7 +228,7 @@ describe('amplify form renderer tests', () => {
expect(componentText).toContain('const studentRecords = useDataStoreBinding({');

// check lazy load linked data
expect(componentText).toContain('const linkedStudents = record ? await record.Students.toArray() : [];');
expect(componentText).toContain('(record && (await record.Students?.toArray())) || [];');

// check custom display value is set
expect(componentText).toContain('Students: (r) => r?.name');
Expand Down Expand Up @@ -257,7 +257,7 @@ describe('amplify form renderer tests', () => {
expect(componentText).toContain('const studentRecords = useDataStoreBinding({');

// check lazy load linked data
expect(componentText).toContain('const linkedStudent = record ? await record.Student.toArray() : [];');
expect(componentText).toContain('const linkedStudent = (record && (await record.Student?.toArray())) || [];');

// check custom display value is set
expect(componentText).toContain('Student: (r) => `${r?.name ? r?.name + " - " : ""}${r?.id}`,');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1231,18 +1231,24 @@ export const buildGetRelationshipModels = (
factory.createCallExpression(
factory.createPropertyAccessExpression(
factory.createParenthesizedExpression(
factory.createAwaitExpression(
factory.createCallExpression(
factory.createPropertyAccessExpression(
factory.createPropertyAccessExpression(
factory.createIdentifier('record'),
factory.createIdentifier(fieldName),
factory.createBinaryExpression(
factory.createAwaitExpression(
factory.createCallChain(
factory.createPropertyAccessChain(
factory.createPropertyAccessExpression(
factory.createIdentifier('record'),
factory.createIdentifier(fieldName),
),
factory.createToken(SyntaxKind.QuestionDotToken),
factory.createIdentifier('toArray'),
),
factory.createIdentifier('toArray'),
undefined,
undefined,
[],
),
undefined,
[],
),
factory.createToken(SyntaxKind.BarBarToken),
factory.createArrayLiteralExpression([], false),
),
),
factory.createIdentifier('map'),
Expand Down Expand Up @@ -1304,20 +1310,26 @@ export const buildGetRelationshipModels = (
undefined,
dataApi === 'GraphQL'
? graphqlLinkedRecordsFallback(fieldName)
: factory.createConditionalExpression(
recordIdentifier,
factory.createToken(SyntaxKind.QuestionToken),
factory.createAwaitExpression(
factory.createCallExpression(
factory.createPropertyAccessExpression(
factory.createPropertyAccessExpression(recordIdentifier, factory.createIdentifier(fieldName)),
factory.createIdentifier('toArray'),
: factory.createBinaryExpression(
factory.createBinaryExpression(
recordIdentifier,
factory.createToken(SyntaxKind.AmpersandAmpersandToken),
factory.createAwaitExpression(
factory.createCallExpression(
factory.createPropertyAccessChain(
factory.createPropertyAccessExpression(
recordIdentifier,
factory.createIdentifier(fieldName),
),
factory.createToken(SyntaxKind.QuestionDotToken),
factory.createIdentifier('toArray'),
),
undefined,
[],
),
undefined,
[],
),
),
factory.createToken(SyntaxKind.ColonToken),
factory.createToken(SyntaxKind.BarBarToken),
factory.createArrayLiteralExpression([], false),
),
),
Expand Down

0 comments on commit 70799cb

Please sign in to comment.