drizzle-graphql-plus 0.8.20 → 0.8.22

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 CHANGED
@@ -2553,7 +2553,7 @@ var generateQueryTypeDefs = (tables) => {
2553
2553
  for (const tableName of Object.keys(tables)) {
2554
2554
  const typeName = capitalize(tableName);
2555
2555
  queryFields.push(
2556
- ` ${tableName}(where: ${typeName}Filters, orderBy: ${typeName}OrderBy, limit: Int, offset: Int): [${typeName}!]!`
2556
+ ` ${tableName}FindMany(where: ${typeName}Filters, orderBy: ${typeName}OrderBy, limit: Int, offset: Int): [${typeName}!]!`
2557
2557
  );
2558
2558
  }
2559
2559
  return `type Query {
@@ -2565,13 +2565,13 @@ var generateMutationTypeDefs = (tables) => {
2565
2565
  for (const tableName of Object.keys(tables)) {
2566
2566
  const typeName = capitalize(tableName);
2567
2567
  mutationFields.push(
2568
- ` insert${typeName}(values: [${typeName}InsertInput!]!): [${typeName}!]!`
2568
+ ` ${tableName}InsertMany(values: [${typeName}InsertInput!]!): [${typeName}!]!`
2569
2569
  );
2570
2570
  mutationFields.push(
2571
- ` update${typeName}(where: ${typeName}Filters, set: ${typeName}UpdateInput!): [${typeName}!]!`
2571
+ ` ${tableName}UpdateMany(where: ${typeName}Filters, set: ${typeName}UpdateInput!): [${typeName}!]!`
2572
2572
  );
2573
2573
  mutationFields.push(
2574
- ` delete${typeName}(where: ${typeName}Filters): [${typeName}!]!`
2574
+ ` ${tableName}DeleteMany(where: ${typeName}Filters): [${typeName}!]!`
2575
2575
  );
2576
2576
  }
2577
2577
  return `type Mutation {
@@ -2780,6 +2780,41 @@ var extractRelationsParams2 = (relationMap, tables, tableName, fields) => {
2780
2780
  }
2781
2781
  return Object.keys(args).length > 0 ? args : void 0;
2782
2782
  };
2783
+ var createQueryResolver = (queryBase, tableInfo, tables, relations) => {
2784
+ return async (parent, args, context, info) => {
2785
+ try {
2786
+ const { where, orderBy, limit, offset } = args;
2787
+ const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
2788
+ deep: true
2789
+ });
2790
+ const allFields = {};
2791
+ if (parsedInfo.fieldsByTypeName) {
2792
+ for (const fields of Object.values(parsedInfo.fieldsByTypeName)) {
2793
+ Object.assign(allFields, fields);
2794
+ }
2795
+ }
2796
+ const result = await queryBase.findMany({
2797
+ columns: extractSelectedColumns(allFields, tableInfo),
2798
+ offset,
2799
+ limit,
2800
+ orderBy: buildOrderByClause(tableInfo, orderBy),
2801
+ where: buildWhereClause(tableInfo, where),
2802
+ with: extractRelationsParams2(
2803
+ relations,
2804
+ tables,
2805
+ tableInfo.name,
2806
+ allFields
2807
+ )
2808
+ });
2809
+ return result;
2810
+ } catch (e) {
2811
+ if (typeof e === "object" && e !== null && "message" in e) {
2812
+ throw new import_graphql8.GraphQLError(String(e.message));
2813
+ }
2814
+ throw e;
2815
+ }
2816
+ };
2817
+ };
2783
2818
  var generateQueries = (db, tables, relations) => {
2784
2819
  const queries = {};
2785
2820
  for (const [tableName, tableInfo] of Object.entries(tables)) {
@@ -2789,34 +2824,12 @@ var generateQueries = (db, tables, relations) => {
2789
2824
  `Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
2790
2825
  );
2791
2826
  }
2792
- queries[tableName] = async (parent, args, context, info) => {
2793
- try {
2794
- const { where, orderBy, limit, offset } = args;
2795
- const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
2796
- deep: true
2797
- });
2798
- const allFields = {};
2799
- if (parsedInfo.fieldsByTypeName) {
2800
- for (const fields of Object.values(parsedInfo.fieldsByTypeName)) {
2801
- Object.assign(allFields, fields);
2802
- }
2803
- }
2804
- const result = await queryBase.findMany({
2805
- columns: extractSelectedColumns(allFields, tableInfo),
2806
- offset,
2807
- limit,
2808
- orderBy: buildOrderByClause(tableInfo, orderBy),
2809
- where: buildWhereClause(tableInfo, where),
2810
- with: extractRelationsParams2(relations, tables, tableName, allFields)
2811
- });
2812
- return result;
2813
- } catch (e) {
2814
- if (typeof e === "object" && e !== null && "message" in e) {
2815
- throw new import_graphql8.GraphQLError(String(e.message));
2816
- }
2817
- throw e;
2818
- }
2819
- };
2827
+ queries[`${tableName}FindMany`] = createQueryResolver(
2828
+ queryBase,
2829
+ tableInfo,
2830
+ tables,
2831
+ relations
2832
+ );
2820
2833
  }
2821
2834
  return queries;
2822
2835
  };
@@ -2864,11 +2877,31 @@ var buildWhereClause2 = (tableInfo, where) => {
2864
2877
  return conditions[0];
2865
2878
  return (0, import_drizzle_orm10.and)(...conditions);
2866
2879
  };
2867
- var generateMutations = (db, tables) => {
2880
+ var generateMutations = (db, tables, relations) => {
2868
2881
  const mutations = {};
2869
2882
  for (const [tableName, tableInfo] of Object.entries(tables)) {
2870
2883
  const capitalizedName = capitalize(tableName);
2871
- mutations[`insert${capitalizedName}`] = async (parent, args, context, info) => {
2884
+ const queryBase = db.query[tableName];
2885
+ if (!queryBase) {
2886
+ throw new Error(
2887
+ `Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
2888
+ );
2889
+ }
2890
+ const primaryKeyColumn = Object.values(tableInfo.columns).find(
2891
+ (col) => col.primary || col.name === "id"
2892
+ );
2893
+ if (!primaryKeyColumn) {
2894
+ throw new Error(
2895
+ `Drizzle-GraphQL Error: Table ${tableName} does not have a primary key column`
2896
+ );
2897
+ }
2898
+ const queryResolver = createQueryResolver(
2899
+ queryBase,
2900
+ tableInfo,
2901
+ tables,
2902
+ relations
2903
+ );
2904
+ mutations[`${tableName}InsertMany`] = async (parent, args, context, info) => {
2872
2905
  try {
2873
2906
  const { values } = args;
2874
2907
  if (!values || values.length === 0) {
@@ -2878,8 +2911,20 @@ var generateMutations = (db, tables) => {
2878
2911
  values,
2879
2912
  tableInfo.table
2880
2913
  );
2881
- const result = await db.insert(tableInfo.table).values(remappedValues).returning();
2882
- return result;
2914
+ const insertedRows = await db.insert(tableInfo.table).values(remappedValues).returning();
2915
+ const insertedIds = insertedRows.map(
2916
+ (row) => row[primaryKeyColumn.name]
2917
+ );
2918
+ return queryResolver(
2919
+ parent,
2920
+ {
2921
+ where: {
2922
+ [primaryKeyColumn.name]: { inArray: insertedIds }
2923
+ }
2924
+ },
2925
+ context,
2926
+ info
2927
+ );
2883
2928
  } catch (e) {
2884
2929
  if (typeof e === "object" && e !== null && "message" in e) {
2885
2930
  throw new import_graphql9.GraphQLError(String(e.message));
@@ -2887,7 +2932,7 @@ var generateMutations = (db, tables) => {
2887
2932
  throw e;
2888
2933
  }
2889
2934
  };
2890
- mutations[`update${capitalizedName}`] = async (parent, args, context, info) => {
2935
+ mutations[`${tableName}UpdateMany`] = async (parent, args, context, info) => {
2891
2936
  try {
2892
2937
  const { where, set } = args;
2893
2938
  if (!set || Object.keys(set).length === 0) {
@@ -2899,8 +2944,20 @@ var generateMutations = (db, tables) => {
2899
2944
  if (whereClause) {
2900
2945
  query = query.where(whereClause);
2901
2946
  }
2902
- const result = await query.returning();
2903
- return result;
2947
+ const updatedRows = await query.returning();
2948
+ const updatedIds = updatedRows.map(
2949
+ (row) => row[primaryKeyColumn.name]
2950
+ );
2951
+ return queryResolver(
2952
+ parent,
2953
+ {
2954
+ where: {
2955
+ [primaryKeyColumn.name]: { inArray: updatedIds }
2956
+ }
2957
+ },
2958
+ context,
2959
+ info
2960
+ );
2904
2961
  } catch (e) {
2905
2962
  if (typeof e === "object" && e !== null && "message" in e) {
2906
2963
  throw new import_graphql9.GraphQLError(String(e.message));
@@ -2908,7 +2965,7 @@ var generateMutations = (db, tables) => {
2908
2965
  throw e;
2909
2966
  }
2910
2967
  };
2911
- mutations[`delete${capitalizedName}`] = async (parent, args, context, info) => {
2968
+ mutations[`${tableName}DeleteMany`] = async (parent, args, context, info) => {
2912
2969
  try {
2913
2970
  const { where } = args;
2914
2971
  const whereClause = buildWhereClause2(tableInfo, where);
@@ -2949,7 +3006,7 @@ var buildSchemaSDL = (db, config) => {
2949
3006
  typeDefsArray.push(generateMutationTypeDefs(tables));
2950
3007
  const typeDefs = typeDefsArray.join("\n\n");
2951
3008
  const queries = generateQueries(db, tables, relations);
2952
- const mutations = generateMutations(db, tables);
3009
+ const mutations = generateMutations(db, tables, relations);
2953
3010
  return {
2954
3011
  typeDefs,
2955
3012
  resolvers: {