Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix auto-incremented columns marked as required fields #5882

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tasty-maps-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-mesh/mysql': patch
---

Fix fields wrongly marked as required when column has a default value or is auto-incremented
24 changes: 18 additions & 6 deletions packages/handlers/mysql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ async function getPromisifiedConnection(pool: Pool) {
const deleteRow = util.promisify(connection.delete.bind(connection));
const count = util.promisify(connection.count.bind(connection));
const release = connection.release.bind(connection);
const queryKeyValue = util.promisify(connection.queryKeyValue.bind(connection));

return {
connection,
Expand All @@ -123,6 +124,7 @@ async function getPromisifiedConnection(pool: Pool) {
update,
deleteRow,
count,
queryKeyValue,
};
}

Expand Down Expand Up @@ -266,6 +268,7 @@ export default class MySQLHandler implements MeshHandler {
pool.config.connectionConfig.database,
);
const tableNames = this.config.tables || Object.keys(tables);
const autoIncrementedColumns = await getAutoIncrementFields(introspectionConnection);
const typeMergingOptions: MeshSource['merge'] = {};
await Promise.all(
tableNames.map(async tableName => {
Expand Down Expand Up @@ -347,24 +350,26 @@ export default class MySQLHandler implements MeshHandler {
);
type = 'JSON';
}
if (tableField.Null.toLowerCase() === 'no') {
type += '!';
}
const isNullable = tableField.Null.toLowerCase() === 'yes';
const isRequired =
!isNullable &&
tableField.Default === null &&
autoIncrementedColumns[tableName] !== fieldName;
tableTC.addFields({
[fieldName]: {
type,
type: isNullable ? type : type + '!',
description: tableField.Comment || undefined,
},
});
tableInsertIC.addFields({
[fieldName]: {
type,
type: isRequired ? type + '!' : type,
description: tableField.Comment || undefined,
},
});
tableUpdateIC.addFields({
[fieldName]: {
type: type.replace('!', ''),
type,
description: tableField.Comment || undefined,
},
});
Expand Down Expand Up @@ -662,3 +667,10 @@ export default class MySQLHandler implements MeshHandler {
};
}
}

async function getAutoIncrementFields(connection: any): Promise<Record<string, string>> {
return connection.queryKeyValue(
'SELECT TABLE_NAME, COLUMN_NAME FROM information_schema.columns WHERE EXTRA LIKE "%auto_increment%"',
[],
);
}
1 change: 1 addition & 0 deletions packages/handlers/mysql/src/mysql.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,6 @@ declare module 'mysql' {
update(tableName: string, input: any, where: any, callback: Callback<{ affectedRows: any }>);
delete(tableName: string, where: any, callback: Callback<{ affectedRows: any }>);
count(tableName: string, where: any, callback: Callback<number>);
queryKeyValue(query: string, field: string[], callback: Callback<Record<string, any>>);
}
}
Loading