Skip to content

Commit 76cde5a

Browse files
committed
chore: update logic to return empty list and check for length
1 parent abae276 commit 76cde5a

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

packages/appsync-modelgen-plugin/src/utils/fieldUtils.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ export function getOtherSideBelongsToField(type: string, otherSideModel: CodeGen
3232
*/
3333
export function getModelPrimaryKeyComponentFields(model: CodeGenModel): CodeGenField[] {
3434
const primaryKeyField = model.fields.find(field => field.primaryKeyInfo);
35-
if (primaryKeyField && primaryKeyField?.primaryKeyInfo) {
36-
const { sortKeyFields } = primaryKeyField.primaryKeyInfo;
37-
return [ primaryKeyField, ...sortKeyFields ];
35+
const keyFields: CodeGenField[] = [];
36+
if (primaryKeyField) {
37+
keyFields.push(primaryKeyField);
38+
if ( primaryKeyField?.primaryKeyInfo?.sortKeyFields ) {
39+
keyFields.push(...primaryKeyField.primaryKeyInfo.sortKeyFields);
40+
};
3841
}
39-
40-
throw new Error(`Unable to get the primary key component fields for model ${model?.name}`);
42+
return keyFields;
4143
}

packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -941,15 +941,17 @@ export class AppSyncModelVisitor<
941941
} else if (connectionInfo.kind === CodeGenConnectionType.HAS_ONE) {
942942
if (isCustomPKEnabled) {
943943
const connectedModelFields = getModelPrimaryKeyComponentFields(connectionInfo.connectedModel);
944-
connectionInfo.targetNames.forEach((target, index) => {
945-
addFieldToModel(model, {
946-
name: target,
947-
directives: [],
948-
type: connectedModelFields[index].type,
949-
isList: false,
950-
isNullable: field.isNullable,
944+
if (connectedModelFields?.length > 0) {
945+
connectionInfo.targetNames.forEach((target, index) => {
946+
addFieldToModel(model, {
947+
name: target,
948+
directives: [],
949+
type: connectedModelFields[index].type,
950+
isList: false,
951+
isNullable: field.isNullable,
952+
});
951953
});
952-
});
954+
}
953955
} else {
954956
addFieldToModel(model, {
955957
name: connectionInfo.targetName,
@@ -962,15 +964,17 @@ export class AppSyncModelVisitor<
962964
} else if (connectionInfo.kind === CodeGenConnectionType.BELONGS_TO) {
963965
if (isCustomPKEnabled) {
964966
const connectedModelFields = getModelPrimaryKeyComponentFields(connectionInfo.connectedModel);
965-
connectionInfo.targetNames.forEach((target, index) => {
966-
addFieldToModel(model, {
967-
name: target,
968-
directives: [],
969-
type: connectedModelFields[index].type,
970-
isList: false,
971-
isNullable: field.isNullable,
967+
if (connectedModelFields?.length > 0) {
968+
connectionInfo.targetNames.forEach((target, index) => {
969+
addFieldToModel(model, {
970+
name: target,
971+
directives: [],
972+
type: connectedModelFields[index].type,
973+
isList: false,
974+
isNullable: field.isNullable,
975+
});
972976
});
973-
});
977+
}
974978
} else {
975979
addFieldToModel(model, {
976980
name: connectionInfo.targetName,

0 commit comments

Comments
 (0)