drizzle-graphql-plus 0.8.22 → 0.8.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs +49 -9
- package/index.cjs.map +1 -1
- package/index.js +49 -9
- package/index.js.map +1 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -2469,6 +2469,7 @@ var generateTypeDefs = (tables, relations) => {
|
|
|
2469
2469
|
}
|
|
2470
2470
|
fields.push(` ${columnName}: ${typeStr}`);
|
|
2471
2471
|
}
|
|
2472
|
+
fields.push(` _operation: OPERATION!`);
|
|
2472
2473
|
const tableRelations = relations[tableName];
|
|
2473
2474
|
if (tableRelations) {
|
|
2474
2475
|
for (const [relationName, relationInfo] of Object.entries(
|
|
@@ -2478,8 +2479,21 @@ var generateTypeDefs = (tables, relations) => {
|
|
|
2478
2479
|
const targetTableName = relationInfo.targetTableName;
|
|
2479
2480
|
const targetTypeName = capitalize(targetTableName);
|
|
2480
2481
|
if (isOne) {
|
|
2482
|
+
const relation = relationInfo.relation;
|
|
2483
|
+
const config = relation.config;
|
|
2484
|
+
let isNonNullable = false;
|
|
2485
|
+
if (config?.fields && config.fields.length > 0) {
|
|
2486
|
+
isNonNullable = config.fields.every((field) => {
|
|
2487
|
+
const fieldColumnName = field.name;
|
|
2488
|
+
const column = Object.values(tableInfo.columns).find(
|
|
2489
|
+
(col) => col.name === fieldColumnName
|
|
2490
|
+
);
|
|
2491
|
+
return column && column.notNull;
|
|
2492
|
+
});
|
|
2493
|
+
}
|
|
2494
|
+
const nullableModifier = isNonNullable ? "!" : "";
|
|
2481
2495
|
fields.push(
|
|
2482
|
-
` ${relationName}(where: ${targetTypeName}Filters): ${targetTypeName}`
|
|
2496
|
+
` ${relationName}(where: ${targetTypeName}Filters): ${targetTypeName}${nullableModifier}`
|
|
2483
2497
|
);
|
|
2484
2498
|
} else {
|
|
2485
2499
|
fields.push(
|
|
@@ -2552,6 +2566,12 @@ ${orderByFields.join("\n")}
|
|
|
2552
2566
|
}`);
|
|
2553
2567
|
}
|
|
2554
2568
|
const allDefs = [];
|
|
2569
|
+
allDefs.push(`enum OPERATION {
|
|
2570
|
+
READ
|
|
2571
|
+
INSERTED
|
|
2572
|
+
UPDATED
|
|
2573
|
+
DELETED
|
|
2574
|
+
}`);
|
|
2555
2575
|
if (customScalars.size > 0) {
|
|
2556
2576
|
for (const scalarName of Array.from(customScalars).sort()) {
|
|
2557
2577
|
allDefs.push(`scalar ${scalarName}`);
|
|
@@ -2880,7 +2900,10 @@ var createQueryResolver = (queryBase, tableInfo, tables, relations) => {
|
|
|
2880
2900
|
allFields
|
|
2881
2901
|
)
|
|
2882
2902
|
});
|
|
2883
|
-
return result
|
|
2903
|
+
return result.map((row) => ({
|
|
2904
|
+
...row,
|
|
2905
|
+
_operation: "READ"
|
|
2906
|
+
}));
|
|
2884
2907
|
} catch (e) {
|
|
2885
2908
|
if (typeof e === "object" && e !== null && "message" in e) {
|
|
2886
2909
|
throw new GraphQLError6(String(e.message));
|
|
@@ -2953,6 +2976,7 @@ var buildWhereClause2 = (tableInfo, where) => {
|
|
|
2953
2976
|
};
|
|
2954
2977
|
var generateMutations = (db, tables, relations) => {
|
|
2955
2978
|
const mutations = {};
|
|
2979
|
+
const deleteResultResolvers = {};
|
|
2956
2980
|
for (const [tableName, tableInfo] of Object.entries(tables)) {
|
|
2957
2981
|
const capitalizedName = capitalize(tableName);
|
|
2958
2982
|
const queryBase = db.query[tableName];
|
|
@@ -2989,7 +3013,7 @@ var generateMutations = (db, tables, relations) => {
|
|
|
2989
3013
|
const insertedIds = insertedRows.map(
|
|
2990
3014
|
(row) => row[primaryKeyColumn.name]
|
|
2991
3015
|
);
|
|
2992
|
-
|
|
3016
|
+
const result = await queryResolver(
|
|
2993
3017
|
parent,
|
|
2994
3018
|
{
|
|
2995
3019
|
where: {
|
|
@@ -2999,6 +3023,10 @@ var generateMutations = (db, tables, relations) => {
|
|
|
2999
3023
|
context,
|
|
3000
3024
|
info
|
|
3001
3025
|
);
|
|
3026
|
+
return result.map((row) => ({
|
|
3027
|
+
...row,
|
|
3028
|
+
_operation: "INSERTED"
|
|
3029
|
+
}));
|
|
3002
3030
|
} catch (e) {
|
|
3003
3031
|
if (typeof e === "object" && e !== null && "message" in e) {
|
|
3004
3032
|
throw new GraphQLError7(String(e.message));
|
|
@@ -3022,7 +3050,7 @@ var generateMutations = (db, tables, relations) => {
|
|
|
3022
3050
|
const updatedIds = updatedRows.map(
|
|
3023
3051
|
(row) => row[primaryKeyColumn.name]
|
|
3024
3052
|
);
|
|
3025
|
-
|
|
3053
|
+
const result = await queryResolver(
|
|
3026
3054
|
parent,
|
|
3027
3055
|
{
|
|
3028
3056
|
where: {
|
|
@@ -3032,6 +3060,10 @@ var generateMutations = (db, tables, relations) => {
|
|
|
3032
3060
|
context,
|
|
3033
3061
|
info
|
|
3034
3062
|
);
|
|
3063
|
+
return result.map((row) => ({
|
|
3064
|
+
...row,
|
|
3065
|
+
_operation: "UPDATED"
|
|
3066
|
+
}));
|
|
3035
3067
|
} catch (e) {
|
|
3036
3068
|
if (typeof e === "object" && e !== null && "message" in e) {
|
|
3037
3069
|
throw new GraphQLError7(String(e.message));
|
|
@@ -3047,8 +3079,11 @@ var generateMutations = (db, tables, relations) => {
|
|
|
3047
3079
|
if (whereClause) {
|
|
3048
3080
|
deleteQuery = deleteQuery.where(whereClause);
|
|
3049
3081
|
}
|
|
3050
|
-
const
|
|
3051
|
-
return
|
|
3082
|
+
const deletedRows = await deleteQuery.returning();
|
|
3083
|
+
return deletedRows.map((row) => ({
|
|
3084
|
+
...row,
|
|
3085
|
+
_operation: "DELETED"
|
|
3086
|
+
}));
|
|
3052
3087
|
} catch (e) {
|
|
3053
3088
|
if (typeof e === "object" && e !== null && "message" in e) {
|
|
3054
3089
|
throw new GraphQLError7(String(e.message));
|
|
@@ -3057,7 +3092,7 @@ var generateMutations = (db, tables, relations) => {
|
|
|
3057
3092
|
}
|
|
3058
3093
|
};
|
|
3059
3094
|
}
|
|
3060
|
-
return mutations;
|
|
3095
|
+
return { mutations, deleteResultResolvers: {} };
|
|
3061
3096
|
};
|
|
3062
3097
|
|
|
3063
3098
|
// src/buildSchemaSDL/index.ts
|
|
@@ -3080,12 +3115,17 @@ var buildSchemaSDL = (db, config) => {
|
|
|
3080
3115
|
typeDefsArray.push(generateMutationTypeDefs(tables));
|
|
3081
3116
|
const typeDefs = typeDefsArray.join("\n\n");
|
|
3082
3117
|
const queries = generateQueries(db, tables, relations);
|
|
3083
|
-
const mutations = generateMutations(
|
|
3118
|
+
const { mutations, deleteResultResolvers } = generateMutations(
|
|
3119
|
+
db,
|
|
3120
|
+
tables,
|
|
3121
|
+
relations
|
|
3122
|
+
);
|
|
3084
3123
|
return {
|
|
3085
3124
|
typeDefs,
|
|
3086
3125
|
resolvers: {
|
|
3087
3126
|
Query: queries,
|
|
3088
|
-
Mutation: mutations
|
|
3127
|
+
Mutation: mutations,
|
|
3128
|
+
...deleteResultResolvers
|
|
3089
3129
|
}
|
|
3090
3130
|
};
|
|
3091
3131
|
};
|