Skip to content

Commit d949d42

Browse files
committed
Fix: make belongs to connected field work better, remove unneeded code
1 parent 7bfdaa9 commit d949d42

File tree

1 file changed

+8
-28
lines changed

1 file changed

+8
-28
lines changed

packages/appsync-modelgen-plugin/src/utils/process-belongs-to.ts

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -54,34 +54,14 @@ export function processBelongsToConnection(
5454
};
5555
}
5656

57-
export function getBelongsToConnectedField(field: CodeGenField, model: CodeGenModel, connectedModel: CodeGenModel, connectionInfo: CodeGenDirective): CodeGenField | undefined {
58-
if(connectionInfo.arguments.fields) {
59-
let indexDirective = flattenFieldDirectives(model).find(dir => {
60-
return dir.name === 'index' && dir.fieldName === connectionInfo.arguments.fields[0];
61-
});
62-
63-
if(indexDirective) {
64-
let theIndex = indexDirective;
65-
let otherSideConnected = flattenFieldDirectives(connectedModel).find(dir => {
66-
return (dir.name === 'hasOne' || dir.name === 'hasMany') && dir?.arguments?.indexName === theIndex.arguments.name;
67-
});
68-
if(otherSideConnected) {
69-
for(let connField of connectedModel.fields) {
70-
if (connField.name === otherSideConnected?.fieldName) {
71-
return connField;
72-
}
73-
}
74-
}
75-
}
76-
}
77-
else {
78-
let otherSideDirectives = flattenFieldDirectives(connectedModel).filter(dir => {
79-
let fieldType = connectedModel.fields.find(connField => { return connField.name === dir.fieldName; })?.type;
80-
return (dir.name === 'hasOne' || dir.name === 'hasMany') && model.name === fieldType;
81-
});
57+
export function getBelongsToConnectedField(field: CodeGenField, model: CodeGenModel, connectedModel: CodeGenModel): CodeGenField | undefined {
58+
let otherSideDirectives = flattenFieldDirectives(connectedModel).filter(dir => {
59+
const connectedField = connectedModel.fields.find(connField => { return connField.name === dir.fieldName; });
60+
const fieldType = connectedField?.type;
61+
return ((dir.name === 'hasOne' && !connectedField?.isList) || (dir.name === 'hasMany' && connectedField?.isList)) && model.name === fieldType;
62+
});
8263

83-
if (otherSideDirectives?.length === 1) {
84-
return connectedModel.fields.find(connField => { return connField.name === otherSideDirectives[0].fieldName; });
85-
}
64+
if (otherSideDirectives?.length === 1) {
65+
return connectedModel.fields.find(connField => { return connField.name === otherSideDirectives[0].fieldName; });
8666
}
8767
}

0 commit comments

Comments
 (0)