Skip to content

Commit

Permalink
fix: add fallback to record items
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Shih authored and awinberg-aws committed Aug 4, 2023
1 parent 5b553e9 commit 4d95d36
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4444,7 +4444,7 @@ export default function PostUpdateForm(props) {
})
)?.data?.getPost
: postModelProp;
const linkedComments = record ? record.Comment?.items : [];
const linkedComments = record?.Comment?.items ?? [];
setLinkedComments(linkedComments);
setPostRecord(record);
};
Expand Down Expand Up @@ -8783,7 +8783,7 @@ export default function UpdateCPKTeacherForm(props) {
)
: [];
setLinkedCPKClasses(linkedCPKClasses);
const linkedCPKProjects = record ? record.CPKProject?.items : [];
const linkedCPKProjects = record?.CPKProject?.items ?? [];
setLinkedCPKProjects(linkedCPKProjects);
setCPKTeacherRecord(record);
};
Expand Down Expand Up @@ -12994,7 +12994,7 @@ export default function UpdatePostForm(props) {
})
)?.data?.getPost
: postModelProp;
const linkedComments = record ? record.Comment?.items : [];
const linkedComments = record?.Comment?.items ?? [];
setLinkedComments(linkedComments);
setPostRecord(record);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,22 @@ function createHasManyUpdateRelatedModelBlock({
return factory.createBlock(statements, true);
}

function graphqlLinkedRecordsFallback(modelName: string) {
return factory.createBinaryExpression(
factory.createPropertyAccessChain(
factory.createPropertyAccessChain(
factory.createIdentifier('record'),
factory.createToken(SyntaxKind.QuestionDotToken),
factory.createIdentifier(modelName),
),
factory.createToken(SyntaxKind.QuestionDotToken),
factory.createIdentifier('items'),
),
factory.createToken(SyntaxKind.QuestionQuestionToken),
factory.createArrayLiteralExpression([], false),
);
}

export const buildManyToManyRelationshipStatements = (
dataStoreActionType: 'update' | 'create',
modelName: string,
Expand Down Expand Up @@ -1248,19 +1264,12 @@ export const buildGetRelationshipModels = (
factory.createIdentifier(linkedDataName),
undefined,
undefined,
factory.createConditionalExpression(
recordIdentifier,
factory.createToken(SyntaxKind.QuestionToken),
dataApi === 'GraphQL'
? factory.createPropertyAccessChain(
factory.createPropertyAccessExpression(
factory.createIdentifier('record'),
factory.createIdentifier(relatedModelName),
),
factory.createToken(SyntaxKind.QuestionDotToken),
factory.createIdentifier('items'),
)
: factory.createAwaitExpression(
dataApi === 'GraphQL'
? graphqlLinkedRecordsFallback(relatedModelName)
: factory.createConditionalExpression(
recordIdentifier,
factory.createToken(SyntaxKind.QuestionToken),
factory.createAwaitExpression(
factory.createCallExpression(
factory.createPropertyAccessExpression(
factory.createPropertyAccessExpression(recordIdentifier, factory.createIdentifier(fieldName)),
Expand All @@ -1270,9 +1279,9 @@ export const buildGetRelationshipModels = (
[],
),
),
factory.createToken(SyntaxKind.ColonToken),
factory.createArrayLiteralExpression([], false),
),
factory.createToken(SyntaxKind.ColonToken),
factory.createArrayLiteralExpression([], false),
),
),
],
NodeFlags.Const,
Expand Down

0 comments on commit 4d95d36

Please sign in to comment.