Skip to content

Commit

Permalink
fix: duplicate field #292
Browse files Browse the repository at this point in the history
  • Loading branch information
dineug committed Nov 9, 2024
1 parent 2064697 commit 555d15c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
1 change: 0 additions & 1 deletion packages/erd-editor/src/utils/draw-relationship/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const directionNameToDirection: Record<string, number> = {
[DirectionName.right]: Direction.right,
};

// TODO: concurrent
export function relationshipSort(state: RootState) {
const {
doc: { tableIds, relationshipIds },
Expand Down
35 changes: 22 additions & 13 deletions packages/erd-editor/src/utils/generator-code/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ function formatRelation(
.collection('relationshipEntities')
.selectByIds(relationshipIds);

// TODO: https://github.com/dineug/erd-editor/issues/292
const startFieldNames = new Set<string>();
const endFieldNames = new Set<string>();

relationships
.filter(relationship => relationship.end.tableId === table.id)
Expand All @@ -137,11 +138,16 @@ function formatRelation(
if (startTable) {
const typeName = getNameCase(startTable.name, tableNameCase);
const fieldName = getNameCase(startTable.name, columnNameCase);
const field = ` ${fieldName}: ${typeName}`;
if (startFieldNames.has(field)) {
return;
}

if (startTable.comment.trim() !== '') {
buffer.push(` # ${startTable.comment}`);
}
buffer.push(` ${fieldName}: ${typeName}`);
buffer.push(field);
startFieldNames.add(field);
}
});

Expand All @@ -153,21 +159,24 @@ function formatRelation(
if (endTable) {
const typeName = getNameCase(endTable.name, tableNameCase);
const fieldName = getNameCase(endTable.name, columnNameCase);
const field = hasOneRelationship(relationship.relationshipType)
? ` ${fieldName}: ${typeName}`
: hasNRelationship(relationship.relationshipType)
? ` ${getNameCase(
`${fieldName}List`,
columnNameCase
)}: [${typeName}!]!`
: null;

if (field === null || endFieldNames.has(field)) {
return;
}

if (endTable.comment.trim() !== '') {
buffer.push(` # ${endTable.comment}`);
}

if (hasOneRelationship(relationship.relationshipType)) {
buffer.push(` ${fieldName}: ${typeName}`);
} else if (hasNRelationship(relationship.relationshipType)) {
buffer.push(
` ${getNameCase(
`${fieldName}List`,
columnNameCase
)}: [${typeName}!]!`
);
}
buffer.push(field);
endFieldNames.add(field);
}
});
}

0 comments on commit 555d15c

Please sign in to comment.