Skip to content

Commit

Permalink
Added library name to error message for easier debugging, fixed lack …
Browse files Browse the repository at this point in the history
…of library's own error messages on empty schema in drizzle instance
  • Loading branch information
Sukairo-02 committed Jun 13, 2024
1 parent e34eea8 commit 3173e12
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "drizzle-graphql",
"type": "module",
"author": "Drizzle Team",
"version": "0.8.3",
"version": "0.8.4",
"description": "Automatically generate GraphQL schema or customizable schema config fields from Drizzle ORM schema",
"scripts": {
"build": "pnpm tsx scripts/build.ts",
Expand Down
12 changes: 8 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ export const buildSchema = <TDbClient extends AnyDrizzleDB<any>>(
const schema = db._.fullSchema;
if (!schema) {
throw new Error(
"Schema not found in drizzle instance. Make sure you're using drizzle-orm v0.30.9 or above and schema is passed to drizzle constructor!",
"Drizzle-GraphQL Error: Schema not found in drizzle instance. Make sure you're using drizzle-orm v0.30.9 or above and schema is passed to drizzle constructor!",
);
}

if (typeof config?.relationsDepthLimit === 'number') {
if (config.relationsDepthLimit < 0) {
throw new Error('config.relationsDepthLimit is supposed to be nonnegative integer or undefined!');
throw new Error(
'Drizzle-GraphQL Error: config.relationsDepthLimit is supposed to be nonnegative integer or undefined!',
);
}
if (config.relationsDepthLimit !== ~~config.relationsDepthLimit) {
throw new Error('config.relationsDepthLimit is supposed to be nonnegative integer or undefined!');
throw new Error(
'Drizzle-GraphQL Error: config.relationsDepthLimit is supposed to be nonnegative integer or undefined!',
);
}
}

Expand All @@ -41,7 +45,7 @@ export const buildSchema = <TDbClient extends AnyDrizzleDB<any>>(
generatorOutput = generatePG(db, schema, config?.relationsDepthLimit);
} else if (is(db, BaseSQLiteDatabase)) {
generatorOutput = generateSQLite(db, schema, config?.relationsDepthLimit);
} else throw new Error('Unknown database instance type');
} else throw new Error('Drizzle-GraphQL Error: Unknown database instance type');

const { queries, mutations, inputs, types } = generatorOutput;

Expand Down
10 changes: 8 additions & 2 deletions src/util/builders/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const generateSelectArray = (
| undefined;
if (!queryBase) {
throw new Error(
`Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`,
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`,
);
}

Expand Down Expand Up @@ -116,7 +116,7 @@ const generateSelectSingle = (
| undefined;
if (!queryBase) {
throw new Error(
`Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`,
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`,
);
}

Expand Down Expand Up @@ -346,6 +346,12 @@ export const generateSchemaData = <
const tableEntries = schemaEntries.filter(([key, value]) => is(value, MySqlTable)) as [string, MySqlTable][];
const tables = Object.fromEntries(tableEntries);

if (!tableEntries.length) {
throw new Error(
"Drizzle-GraphQL Error: No tables detected in Drizzle-ORM's database instance. Did you forget to pass schema to drizzle constructor?",
);
}

const rawRelations = schemaEntries
.filter(([key, value]) => is(value, Relations))
.map<[string, Relations]>(([key, value]) => [
Expand Down
10 changes: 8 additions & 2 deletions src/util/builders/pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const generateSelectArray = (
| undefined;
if (!queryBase) {
throw new Error(
`Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`,
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`,
);
}

Expand Down Expand Up @@ -116,7 +116,7 @@ const generateSelectSingle = (
| undefined;
if (!queryBase) {
throw new Error(
`Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`,
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`,
);
}

Expand Down Expand Up @@ -360,6 +360,12 @@ export const generateSchemaData = <
PgTable
>;

if (!tableEntries.length) {
throw new Error(
"Drizzle-GraphQL Error: No tables detected in Drizzle-ORM's database instance. Did you forget to pass schema to drizzle constructor?",
);
}

const rawRelations = schemaEntries
.filter(([key, value]) => is(value, Relations))
.map<[string, Relations]>(([key, value]) => [
Expand Down
9 changes: 5 additions & 4 deletions src/util/builders/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const generateSelectArray = (
| undefined;
if (!queryBase) {
throw new Error(
`Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`,
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`,
);
}

Expand Down Expand Up @@ -116,7 +116,7 @@ const generateSelectSingle = (
| undefined;
if (!queryBase) {
throw new Error(
`Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`,
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`,
);
}

Expand Down Expand Up @@ -363,9 +363,10 @@ export const generateSchemaData = <
string,
SQLiteTable
>;
if (!tables || !Object.keys(tables).length) {

if (!tableEntries.length) {
throw new Error(
`Unable to extract tables from drizzle instance.\nDid you forget to pass tables to graphql schema constructor?`,
"Drizzle-GraphQL Error: No tables detected in Drizzle-ORM's database instance. Did you forget to pass schema to drizzle constructor?",
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/util/type-converter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const columnToGraphQLCore = (column: Column, columnName: string, tableName: stri
}
case 'custom':
default:
throw new Error(`Type ${column.dataType} is not implemented!`);
throw new Error(`Drizzle-GraphQL Error: Type ${column.dataType} is not implemented!`);
}
};

Expand Down

0 comments on commit 3173e12

Please sign in to comment.