drizzle-graphql-plus 0.8.20 → 0.8.21

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.js CHANGED
@@ -2608,7 +2608,7 @@ var generateQueryTypeDefs = (tables) => {
2608
2608
  for (const tableName of Object.keys(tables)) {
2609
2609
  const typeName = capitalize(tableName);
2610
2610
  queryFields.push(
2611
- ` ${tableName}(where: ${typeName}Filters, orderBy: ${typeName}OrderBy, limit: Int, offset: Int): [${typeName}!]!`
2611
+ ` ${tableName}FindMany(where: ${typeName}Filters, orderBy: ${typeName}OrderBy, limit: Int, offset: Int): [${typeName}!]!`
2612
2612
  );
2613
2613
  }
2614
2614
  return `type Query {
@@ -2620,13 +2620,13 @@ var generateMutationTypeDefs = (tables) => {
2620
2620
  for (const tableName of Object.keys(tables)) {
2621
2621
  const typeName = capitalize(tableName);
2622
2622
  mutationFields.push(
2623
- ` insert${typeName}(values: [${typeName}InsertInput!]!): [${typeName}!]!`
2623
+ ` ${tableName}InsertMany(values: [${typeName}InsertInput!]!): [${typeName}!]!`
2624
2624
  );
2625
2625
  mutationFields.push(
2626
- ` update${typeName}(where: ${typeName}Filters, set: ${typeName}UpdateInput!): [${typeName}!]!`
2626
+ ` ${tableName}UpdateMany(where: ${typeName}Filters, set: ${typeName}UpdateInput!): [${typeName}!]!`
2627
2627
  );
2628
2628
  mutationFields.push(
2629
- ` delete${typeName}(where: ${typeName}Filters): [${typeName}!]!`
2629
+ ` ${tableName}DeleteMany(where: ${typeName}Filters): [${typeName}!]!`
2630
2630
  );
2631
2631
  }
2632
2632
  return `type Mutation {
@@ -2854,6 +2854,41 @@ var extractRelationsParams2 = (relationMap, tables, tableName, fields) => {
2854
2854
  }
2855
2855
  return Object.keys(args).length > 0 ? args : void 0;
2856
2856
  };
2857
+ var createQueryResolver = (queryBase, tableInfo, tables, relations) => {
2858
+ return async (parent, args, context, info) => {
2859
+ try {
2860
+ const { where, orderBy, limit, offset } = args;
2861
+ const parsedInfo = parseResolveInfo4(info, {
2862
+ deep: true
2863
+ });
2864
+ const allFields = {};
2865
+ if (parsedInfo.fieldsByTypeName) {
2866
+ for (const fields of Object.values(parsedInfo.fieldsByTypeName)) {
2867
+ Object.assign(allFields, fields);
2868
+ }
2869
+ }
2870
+ const result = await queryBase.findMany({
2871
+ columns: extractSelectedColumns(allFields, tableInfo),
2872
+ offset,
2873
+ limit,
2874
+ orderBy: buildOrderByClause(tableInfo, orderBy),
2875
+ where: buildWhereClause(tableInfo, where),
2876
+ with: extractRelationsParams2(
2877
+ relations,
2878
+ tables,
2879
+ tableInfo.name,
2880
+ allFields
2881
+ )
2882
+ });
2883
+ return result;
2884
+ } catch (e) {
2885
+ if (typeof e === "object" && e !== null && "message" in e) {
2886
+ throw new GraphQLError6(String(e.message));
2887
+ }
2888
+ throw e;
2889
+ }
2890
+ };
2891
+ };
2857
2892
  var generateQueries = (db, tables, relations) => {
2858
2893
  const queries = {};
2859
2894
  for (const [tableName, tableInfo] of Object.entries(tables)) {
@@ -2863,34 +2898,12 @@ var generateQueries = (db, tables, relations) => {
2863
2898
  `Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
2864
2899
  );
2865
2900
  }
2866
- queries[tableName] = async (parent, args, context, info) => {
2867
- try {
2868
- const { where, orderBy, limit, offset } = args;
2869
- const parsedInfo = parseResolveInfo4(info, {
2870
- deep: true
2871
- });
2872
- const allFields = {};
2873
- if (parsedInfo.fieldsByTypeName) {
2874
- for (const fields of Object.values(parsedInfo.fieldsByTypeName)) {
2875
- Object.assign(allFields, fields);
2876
- }
2877
- }
2878
- const result = await queryBase.findMany({
2879
- columns: extractSelectedColumns(allFields, tableInfo),
2880
- offset,
2881
- limit,
2882
- orderBy: buildOrderByClause(tableInfo, orderBy),
2883
- where: buildWhereClause(tableInfo, where),
2884
- with: extractRelationsParams2(relations, tables, tableName, allFields)
2885
- });
2886
- return result;
2887
- } catch (e) {
2888
- if (typeof e === "object" && e !== null && "message" in e) {
2889
- throw new GraphQLError6(String(e.message));
2890
- }
2891
- throw e;
2892
- }
2893
- };
2901
+ queries[`${tableName}FindMany`] = createQueryResolver(
2902
+ queryBase,
2903
+ tableInfo,
2904
+ tables,
2905
+ relations
2906
+ );
2894
2907
  }
2895
2908
  return queries;
2896
2909
  };
@@ -2938,11 +2951,31 @@ var buildWhereClause2 = (tableInfo, where) => {
2938
2951
  return conditions[0];
2939
2952
  return and3(...conditions);
2940
2953
  };
2941
- var generateMutations = (db, tables) => {
2954
+ var generateMutations = (db, tables, relations) => {
2942
2955
  const mutations = {};
2943
2956
  for (const [tableName, tableInfo] of Object.entries(tables)) {
2944
2957
  const capitalizedName = capitalize(tableName);
2945
- mutations[`insert${capitalizedName}`] = async (parent, args, context, info) => {
2958
+ const queryBase = db.query[tableName];
2959
+ if (!queryBase) {
2960
+ throw new Error(
2961
+ `Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
2962
+ );
2963
+ }
2964
+ const primaryKeyColumn = Object.values(tableInfo.columns).find(
2965
+ (col) => col.primary || col.name === "id"
2966
+ );
2967
+ if (!primaryKeyColumn) {
2968
+ throw new Error(
2969
+ `Drizzle-GraphQL Error: Table ${tableName} does not have a primary key column`
2970
+ );
2971
+ }
2972
+ const queryResolver = createQueryResolver(
2973
+ queryBase,
2974
+ tableInfo,
2975
+ tables,
2976
+ relations
2977
+ );
2978
+ mutations[`${tableName}InsertMany`] = async (parent, args, context, info) => {
2946
2979
  try {
2947
2980
  const { values } = args;
2948
2981
  if (!values || values.length === 0) {
@@ -2952,8 +2985,20 @@ var generateMutations = (db, tables) => {
2952
2985
  values,
2953
2986
  tableInfo.table
2954
2987
  );
2955
- const result = await db.insert(tableInfo.table).values(remappedValues).returning();
2956
- return result;
2988
+ const insertedRows = await db.insert(tableInfo.table).values(remappedValues).returning();
2989
+ const insertedIds = insertedRows.map(
2990
+ (row) => row[primaryKeyColumn.name]
2991
+ );
2992
+ return queryResolver(
2993
+ parent,
2994
+ {
2995
+ where: {
2996
+ [primaryKeyColumn.name]: { inArray: insertedIds }
2997
+ }
2998
+ },
2999
+ context,
3000
+ info
3001
+ );
2957
3002
  } catch (e) {
2958
3003
  if (typeof e === "object" && e !== null && "message" in e) {
2959
3004
  throw new GraphQLError7(String(e.message));
@@ -2961,7 +3006,7 @@ var generateMutations = (db, tables) => {
2961
3006
  throw e;
2962
3007
  }
2963
3008
  };
2964
- mutations[`update${capitalizedName}`] = async (parent, args, context, info) => {
3009
+ mutations[`${tableName}UpdateMany`] = async (parent, args, context, info) => {
2965
3010
  try {
2966
3011
  const { where, set } = args;
2967
3012
  if (!set || Object.keys(set).length === 0) {
@@ -2973,8 +3018,20 @@ var generateMutations = (db, tables) => {
2973
3018
  if (whereClause) {
2974
3019
  query = query.where(whereClause);
2975
3020
  }
2976
- const result = await query.returning();
2977
- return result;
3021
+ const updatedRows = await query.returning();
3022
+ const updatedIds = updatedRows.map(
3023
+ (row) => row[primaryKeyColumn.name]
3024
+ );
3025
+ return queryResolver(
3026
+ parent,
3027
+ {
3028
+ where: {
3029
+ [primaryKeyColumn.name]: { inArray: updatedIds }
3030
+ }
3031
+ },
3032
+ context,
3033
+ info
3034
+ );
2978
3035
  } catch (e) {
2979
3036
  if (typeof e === "object" && e !== null && "message" in e) {
2980
3037
  throw new GraphQLError7(String(e.message));
@@ -2982,7 +3039,7 @@ var generateMutations = (db, tables) => {
2982
3039
  throw e;
2983
3040
  }
2984
3041
  };
2985
- mutations[`delete${capitalizedName}`] = async (parent, args, context, info) => {
3042
+ mutations[`${tableName}DeleteMany`] = async (parent, args, context, info) => {
2986
3043
  try {
2987
3044
  const { where } = args;
2988
3045
  const whereClause = buildWhereClause2(tableInfo, where);
@@ -3023,7 +3080,7 @@ var buildSchemaSDL = (db, config) => {
3023
3080
  typeDefsArray.push(generateMutationTypeDefs(tables));
3024
3081
  const typeDefs = typeDefsArray.join("\n\n");
3025
3082
  const queries = generateQueries(db, tables, relations);
3026
- const mutations = generateMutations(db, tables);
3083
+ const mutations = generateMutations(db, tables, relations);
3027
3084
  return {
3028
3085
  typeDefs,
3029
3086
  resolvers: {