@pothos/plugin-drizzle 0.12.1 → 0.14.0

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.
Files changed (73) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +2 -2
  3. package/dts/drizzle-field-builder.d.ts +5 -13
  4. package/dts/drizzle-field-builder.d.ts.map +1 -1
  5. package/dts/global-types.d.ts +12 -4
  6. package/dts/global-types.d.ts.map +1 -1
  7. package/dts/interface-ref.d.ts +3 -1
  8. package/dts/interface-ref.d.ts.map +1 -1
  9. package/dts/object-ref.d.ts +3 -1
  10. package/dts/object-ref.d.ts.map +1 -1
  11. package/dts/types.d.ts +23 -6
  12. package/dts/types.d.ts.map +1 -1
  13. package/dts/utils/config.d.ts.map +1 -1
  14. package/dts/utils/drizzle-graphql.d.ts +1 -0
  15. package/dts/utils/drizzle-graphql.d.ts.map +1 -1
  16. package/dts/utils/usage.d.ts +1 -1
  17. package/esm/drizzle-field-builder.d.ts +8 -8
  18. package/esm/drizzle-field-builder.d.ts.map +1 -1
  19. package/esm/drizzle-field-builder.js +2 -1
  20. package/esm/drizzle-field-builder.js.map +1 -1
  21. package/esm/global-types.d.ts +12 -4
  22. package/esm/global-types.d.ts.map +1 -1
  23. package/esm/interface-ref.d.ts +3 -1
  24. package/esm/interface-ref.d.ts.map +1 -1
  25. package/esm/interface-ref.js.map +1 -1
  26. package/esm/model-loader.js +2 -2
  27. package/esm/model-loader.js.map +1 -1
  28. package/esm/object-ref.d.ts +3 -1
  29. package/esm/object-ref.d.ts.map +1 -1
  30. package/esm/object-ref.js.map +1 -1
  31. package/esm/types.d.ts +24 -7
  32. package/esm/types.d.ts.map +1 -1
  33. package/esm/types.js.map +1 -1
  34. package/esm/utils/config.d.ts.map +1 -1
  35. package/esm/utils/config.js +8 -12
  36. package/esm/utils/config.js.map +1 -1
  37. package/esm/utils/cursors.js +2 -2
  38. package/esm/utils/cursors.js.map +1 -1
  39. package/esm/utils/drizzle-graphql.d.ts +1 -0
  40. package/esm/utils/drizzle-graphql.d.ts.map +1 -1
  41. package/esm/utils/drizzle-graphql.js +1 -1
  42. package/esm/utils/drizzle-graphql.js.map +1 -1
  43. package/esm/utils/map-query.js +1 -2
  44. package/esm/utils/map-query.js.map +1 -1
  45. package/esm/utils/usage.d.ts +1 -1
  46. package/lib/drizzle-field-builder.js +2 -1
  47. package/lib/drizzle-field-builder.js.map +1 -1
  48. package/lib/interface-ref.js.map +1 -1
  49. package/lib/model-loader.js +1 -1
  50. package/lib/model-loader.js.map +1 -1
  51. package/lib/object-ref.js.map +1 -1
  52. package/lib/types.js.map +1 -1
  53. package/lib/utils/config.js +7 -11
  54. package/lib/utils/config.js.map +1 -1
  55. package/lib/utils/cursors.js +1 -1
  56. package/lib/utils/cursors.js.map +1 -1
  57. package/lib/utils/drizzle-graphql.js +233 -230
  58. package/lib/utils/drizzle-graphql.js.map +1 -1
  59. package/lib/utils/map-query.js +1 -2
  60. package/lib/utils/map-query.js.map +1 -1
  61. package/package.json +10 -10
  62. package/src/drizzle-field-builder.ts +22 -36
  63. package/src/field-builder.ts +1 -1
  64. package/src/global-types.ts +29 -16
  65. package/src/interface-ref.ts +8 -1
  66. package/src/model-loader.ts +2 -2
  67. package/src/object-ref.ts +8 -1
  68. package/src/types.ts +21 -9
  69. package/src/utils/config.ts +8 -12
  70. package/src/utils/cursors.ts +2 -2
  71. package/src/utils/drizzle-graphql.ts +1 -0
  72. package/src/utils/map-query.ts +1 -1
  73. package/src/utils/usage.ts +1 -1
@@ -83,41 +83,37 @@ export class DrizzleObjectFieldBuilder<
83
83
  Args extends InputFieldMap,
84
84
  const ConnectionInterfaces extends InterfaceParam<Types>[] = [],
85
85
  const EdgeInterfaces extends InterfaceParam<Types>[] = [],
86
+ Type = unknown,
87
+ Shape = // biome-ignore lint/suspicious/noExplicitAny: generic match
88
+ Type extends DrizzleRef<any, any>
89
+ ? Type['$inferType']
90
+ : TypesForRelation<Types, TableConfig['relations'][Field]>,
86
91
  >(
87
92
  field: Field,
88
93
  ...args: NormalizeArgs<
89
94
  [
90
- options: RelatedConnectionOptions<Types, Shape, TableConfig, Field, Nullable, Args>,
95
+ options: RelatedConnectionOptions<
96
+ Types,
97
+ Shape,
98
+ TableConfig,
99
+ Field,
100
+ Nullable,
101
+ Args,
102
+ Type
103
+ >,
91
104
  connectionOptions:
92
105
  | ObjectRef<
93
106
  Types,
94
- ShapeFromConnection<
95
- PothosSchemaTypes.ConnectionShapeHelper<
96
- Types,
97
- TypesForRelation<Types, TableConfig['relations'][Field]>,
98
- false
99
- >
100
- >
107
+ ShapeFromConnection<PothosSchemaTypes.ConnectionShapeHelper<Types, Shape, false>>
101
108
  >
102
109
  | PothosSchemaTypes.ConnectionObjectOptions<
103
110
  Types,
104
- ObjectRef<
105
- Types,
106
- ShapeFromTypeParam<
107
- Types,
108
- [ObjectRef<Types, TypesForRelation<Types, TableConfig['relations'][Field]>>],
109
- Nullable
110
- >
111
- >,
111
+ ObjectRef<Types, ShapeFromTypeParam<Types, [ObjectRef<Types, Shape>], Nullable>>,
112
112
  false,
113
113
  false,
114
114
  DrizzleConnectionShape<
115
115
  Types,
116
- ShapeFromTypeParam<
117
- Types,
118
- [ObjectRef<Types, TypesForRelation<Types, TableConfig['relations'][Field]>>],
119
- Nullable
120
- >,
116
+ ShapeFromTypeParam<Types, [ObjectRef<Types, Shape>], Nullable>,
121
117
  Shape,
122
118
  Args
123
119
  >,
@@ -128,27 +124,16 @@ export class DrizzleObjectFieldBuilder<
128
124
  Types,
129
125
  {
130
126
  cursor: string;
131
- node?: TypesForRelation<Types, TableConfig['relations'][Field]>;
127
+ node?: Shape | null | undefined;
132
128
  }
133
129
  >
134
130
  | PothosSchemaTypes.ConnectionEdgeObjectOptions<
135
131
  Types,
136
- ObjectRef<
137
- Types,
138
- ShapeFromTypeParam<
139
- Types,
140
- [ObjectRef<Types, TypesForRelation<Types, TableConfig['relations'][Field]>>],
141
- Nullable
142
- >
143
- >,
132
+ ObjectRef<Types, ShapeFromTypeParam<Types, [ObjectRef<Types, Shape>], Nullable>>,
144
133
  false,
145
134
  DrizzleConnectionShape<
146
135
  Types,
147
- ShapeFromTypeParam<
148
- Types,
149
- [ObjectRef<Types, TypesForRelation<Types, TableConfig['relations'][Field]>>],
150
- Nullable
151
- >,
136
+ ShapeFromTypeParam<Types, [ObjectRef<Types, Shape>], Nullable>,
152
137
  Shape,
153
138
  Args
154
139
  >,
@@ -168,7 +153,7 @@ export class DrizzleObjectFieldBuilder<
168
153
  maxSize = this.builder.options.drizzle?.maxConnectionSize,
169
154
  defaultSize = this.builder.options.drizzle?.defaultConnectionSize,
170
155
  query,
171
- resolve,
156
+ resolve: _,
172
157
  extensions,
173
158
  description,
174
159
  ...options
@@ -263,6 +248,7 @@ export class DrizzleObjectFieldBuilder<
263
248
  ...extensions,
264
249
  pothosDrizzleSelect: relationSelect,
265
250
  },
251
+ description,
266
252
  type: ref,
267
253
  resolve: (
268
254
  parent: unknown,
@@ -37,7 +37,7 @@ fieldBuilderProto.drizzleField = function drizzleField({ type, resolve, ...optio
37
37
  select,
38
38
  info,
39
39
  // withUsageCheck: !!this.builder.options.drizzle?.onUnusedQuery,
40
- }),
40
+ }) as never,
41
41
  parent,
42
42
  args as never,
43
43
  context,
@@ -37,10 +37,6 @@ import type {
37
37
  ShapeFromIdColumns,
38
38
  } from './types';
39
39
 
40
- type MapToDbName<T> = {
41
- [K in keyof T as T[K] extends { dbName: infer K2 extends string } ? K2 : K]: T[K];
42
- };
43
-
44
40
  declare global {
45
41
  export namespace PothosSchemaTypes {
46
42
  export interface Plugins<Types extends SchemaTypes> {
@@ -282,16 +278,26 @@ declare global {
282
278
  ? [
283
279
  ObjectRef<
284
280
  Types,
285
- BuildQueryResult<
286
- Types['DrizzleRelations'],
287
- Types['DrizzleRelations'][Table],
288
- true
289
- >
281
+ | BuildQueryResult<
282
+ Types['DrizzleRelations'],
283
+ Types['DrizzleRelations'][Table],
284
+ true
285
+ >
286
+ | {
287
+ $pothosQueryFor: Table | undefined;
288
+ }
290
289
  >,
291
290
  ]
292
291
  : ObjectRef<
293
292
  Types,
294
- BuildQueryResult<Types['DrizzleRelations'], Types['DrizzleRelations'][Table], true>
293
+ | BuildQueryResult<
294
+ Types['DrizzleRelations'],
295
+ Types['DrizzleRelations'][Table],
296
+ true
297
+ >
298
+ | {
299
+ $pothosQueryFor: Table | undefined;
300
+ }
295
301
  >,
296
302
  >(
297
303
  options: DrizzleFieldOptions<
@@ -321,11 +327,15 @@ declare global {
321
327
  infer S
322
328
  >
323
329
  ? S
324
- : BuildQueryResult<
325
- Types['DrizzleRelations'],
326
- Types['DrizzleRelations'][Type & keyof Types['DrizzleRelations']],
327
- true
328
- >,
330
+ :
331
+ | BuildQueryResult<
332
+ Types['DrizzleRelations'],
333
+ Types['DrizzleRelations'][Type & keyof Types['DrizzleRelations']],
334
+ true
335
+ >
336
+ | {
337
+ $pothosQueryFor: Type | undefined;
338
+ },
329
339
  const ConnectionInterfaces extends InterfaceParam<Types>[] = [],
330
340
  const EdgeInterfaces extends InterfaceParam<Types>[] = [],
331
341
  >(
@@ -408,7 +418,10 @@ declare global {
408
418
  : never,
409
419
  Type extends TypeParam<Types> = ObjectRef<
410
420
  Types,
411
- BuildQueryResult<Types['DrizzleRelations'], Types['DrizzleRelations'][Table], true>
421
+ | BuildQueryResult<Types['DrizzleRelations'], Types['DrizzleRelations'][Table], true>
422
+ | {
423
+ $pothosQueryFor: Table | undefined;
424
+ }
412
425
  >,
413
426
  >(
414
427
  options: DrizzleFieldWithInputOptions<
@@ -15,7 +15,14 @@ export class DrizzleInterfaceRef<
15
15
  Types extends SchemaTypes,
16
16
  Table extends keyof Types['DrizzleRelations'] = keyof Types['DrizzleRelations'],
17
17
  T = {},
18
- > extends InterfaceRef<Types, T> {
18
+ > extends InterfaceRef<
19
+ Types,
20
+ | T
21
+ | {
22
+ $pothosQueryFor: Table | undefined;
23
+ },
24
+ T
25
+ > {
19
26
  [drizzleTableKey]!: Types['DrizzleRelations'][Table];
20
27
 
21
28
  tableName: string;
@@ -2,7 +2,7 @@ import { createContextCache, type SchemaTypes } from '@pothos/core';
2
2
  import {
3
3
  type AnyTable,
4
4
  type Column,
5
- getTableColumns,
5
+ getColumns,
6
6
  inArray,
7
7
  type SQL,
8
8
  sql,
@@ -60,7 +60,7 @@ export class ModelLoader {
60
60
  this.columns = columns ?? this.primaryKey;
61
61
  this.selectSQL = (table) => {
62
62
  const columns = this.columns.map((column) =>
63
- Object.values(getTableColumns(table)).find(
63
+ Object.values(getColumns(table)).find(
64
64
  (tblColumn) => 'columnType' in tblColumn && tblColumn.name === column.name,
65
65
  ),
66
66
  );
package/src/object-ref.ts CHANGED
@@ -13,7 +13,14 @@ export class DrizzleObjectRef<
13
13
  Types extends SchemaTypes,
14
14
  Table extends keyof Types['DrizzleRelations'] = keyof Types['DrizzleRelations'],
15
15
  T = {},
16
- > extends ObjectRef<Types, T> {
16
+ > extends ObjectRef<
17
+ Types,
18
+ | T
19
+ | {
20
+ $pothosQueryFor: Table | undefined;
21
+ },
22
+ T
23
+ > {
17
24
  [drizzleTableKey]!: Types['DrizzleRelations'][Table];
18
25
 
19
26
  [abstractReturnShapeKey]!: WithBrand<T>;
package/src/types.ts CHANGED
@@ -33,6 +33,7 @@ import type {
33
33
  DBQueryConfig,
34
34
  Many,
35
35
  Relation,
36
+ SQL,
36
37
  Table,
37
38
  TableRelationalConfig,
38
39
  } from 'drizzle-orm';
@@ -214,9 +215,14 @@ export type DrizzleFieldOptions<
214
215
  > & {
215
216
  type: Param;
216
217
  resolve: (
217
- query: <T extends QueryForDrizzleField<Types, Param, Table>>(
218
+ query: <T extends QueryForDrizzleField<Types, Param, Table> = {}>(
218
219
  selection?: T,
219
- ) => Omit<T, 'columns'>,
220
+ ) => Omit<T, 'columns' | 'extra'> & {
221
+ columns: T extends { columns: infer C extends {} } ? C : {};
222
+ extras: {
223
+ $pothosQueryFor: SQL<Table | undefined>;
224
+ } & (T extends { extra: infer E } ? E : {});
225
+ },
220
226
  parent: ParentShape,
221
227
  args: InputShapeFromFields<Args>,
222
228
  ctx: Types['Context'],
@@ -460,9 +466,10 @@ export type QueryForDrizzleField<
460
466
  Types extends SchemaTypes,
461
467
  Param,
462
468
  Table extends keyof Types['DrizzleRelations'],
463
- > = Omit<
464
- DBQueryConfig<'many', Types['DrizzleRelations'], Types['DrizzleRelations'][Table]>,
465
- Param extends [unknown] ? never : 'limit'
469
+ > = DBQueryConfig<
470
+ Param extends [unknown] ? 'one' : 'many',
471
+ Types['DrizzleRelations'],
472
+ Types['DrizzleRelations'][Table]
466
473
  >;
467
474
 
468
475
  export type QueryForRelatedConnection<
@@ -538,14 +545,18 @@ export type DrizzleConnectionFieldOptions<
538
545
  maxSize?: number | ((args: ConnectionArgs, ctx: Types['Context']) => number);
539
546
 
540
547
  resolve: (
541
- query: <T extends QueryForRelatedConnection<Types, TableConfig, ConnectionArgs>>(
542
- selection?: T,
543
- ) => Omit<T, 'orderBy' | 'columns'> & {
548
+ query: <T = {}>(
549
+ selection?: T & QueryForDrizzleConnection<Types, TableConfig>,
550
+ ) => Omit<T, 'orderBy' | 'columns' | 'extra'> & {
551
+ columns: T extends { columns: infer C extends {} } ? C : {};
544
552
  orderBy: {
545
553
  [K in TableConfig['table']['_'] extends { columns: infer Columns }
546
554
  ? keyof Columns
547
555
  : never]?: 'asc' | 'desc' | undefined;
548
556
  };
557
+ extras: {
558
+ $pothosQueryFor: () => SQL<TableConfig['name'] | undefined>;
559
+ } & (T extends { extra: infer E } ? E : {});
549
560
  },
550
561
  parent: ParentShape,
551
562
  args: ConnectionArgs,
@@ -566,6 +577,7 @@ export type RelatedConnectionOptions<
566
577
  Field extends keyof Table['relations'],
567
578
  Nullable extends boolean,
568
579
  Args extends InputFieldMap,
580
+ Type = unknown,
569
581
  NodeTable extends
570
582
  TableRelationalConfig = Types['DrizzleRelations'][Table['relations'][Field]['targetTable']['_']['name']],
571
583
  > = Omit<
@@ -610,7 +622,7 @@ export type RelatedConnectionOptions<
610
622
  >;
611
623
  query?: QueryForRelatedConnection<Types, NodeTable, ConnectionArgs>;
612
624
  // biome-ignore lint/suspicious/noExplicitAny: this is fine
613
- type?: DrizzleRef<any, Table['relations'][Field]['targetTable']['_']['name']>;
625
+ type?: Type & DrizzleRef<any, Table['relations'][Field]['targetTable']['_']['name']>;
614
626
 
615
627
  defaultSize?: number | ((args: ConnectionArgs, ctx: Types['Context']) => number);
616
628
  maxSize?: number | ((args: ConnectionArgs, ctx: Types['Context']) => number);
@@ -1,5 +1,5 @@
1
1
  import { createContextCache, type SchemaTypes } from '@pothos/core';
2
- import { type AnyRelations, type Column, getTableColumns, isTable, type Table } from 'drizzle-orm';
2
+ import { type AnyRelations, type Column, getColumns, isTable, type Table } from 'drizzle-orm';
3
3
  import type { DrizzleClient } from '../types';
4
4
 
5
5
  export interface PothosDrizzleSchemaConfig {
@@ -17,14 +17,12 @@ const configCache = createContextCache(
17
17
  relations = (builder.options.drizzle.client as DrizzleClient)._.relations;
18
18
  }
19
19
 
20
- const columnNameMappings: Record<string, string> = {};
20
+ const columnNameMappings = new Map<Column, string>();
21
21
 
22
22
  Object.values(relations).forEach(({ table }) => {
23
23
  if (isTable(table)) {
24
- Object.entries(getTableColumns(table)).forEach(([tsName, col]) => {
25
- if (col.uniqueName) {
26
- columnNameMappings[col.uniqueName] = tsName;
27
- }
24
+ Object.entries(getColumns(table)).forEach(([tsName, col]) => {
25
+ columnNameMappings.set(col, tsName);
28
26
  });
29
27
  }
30
28
  });
@@ -32,15 +30,13 @@ const configCache = createContextCache(
32
30
  return {
33
31
  skipDeferredFragments: builder.options.drizzle.skipDeferredFragments ?? true,
34
32
  columnToTsName: (column) => {
35
- if (!column.uniqueName) {
36
- throw new Error(`Column ${String(column.name)} has no uniqueName`);
37
- }
33
+ const tsName = columnNameMappings.get(column);
38
34
 
39
- if (!(column.uniqueName in columnNameMappings)) {
40
- throw new Error(`Typescript name not found for ${column.uniqueName}`);
35
+ if (!tsName) {
36
+ throw new Error(`Typescript name not found for column ${String(column.name)}`);
41
37
  }
42
38
 
43
- return columnNameMappings[column.uniqueName];
39
+ return tsName;
44
40
  },
45
41
  getPrimaryKey: (tableName) => {
46
42
  const tableConfig = builder.options.drizzle.getTableConfig(
@@ -8,7 +8,7 @@ import {
8
8
  import {
9
9
  type Column,
10
10
  type DBQueryConfig,
11
- getTableColumns,
11
+ getColumns,
12
12
  type SQL,
13
13
  type Table,
14
14
  type TableRelationalConfig,
@@ -322,7 +322,7 @@ function parseOrderBy(
322
322
  normalized.push({ direction: 'asc', column: field });
323
323
  }
324
324
  } else {
325
- const tableColumns = getTableColumns(table.table as Table);
325
+ const tableColumns = getColumns(table.table as Table);
326
326
  Object.entries(
327
327
  orderBy as {
328
328
  [k: string]: 'asc' | 'desc' | undefined;
@@ -1,3 +1,4 @@
1
+ export {};
1
2
  // import {
2
3
  // type Column,
3
4
  // type SQL,
@@ -567,7 +567,7 @@ export function getIndirectType(type: GraphQLNamedType, info: GraphQLResolveInfo
567
567
 
568
568
  while (targetType.extensions?.pothosIndirectInclude) {
569
569
  targetType = info.schema.getType(
570
- (targetType.extensions?.pothosIndirectInclude as IndirectInclude).getType(),
570
+ (targetType.extensions.pothosIndirectInclude as IndirectInclude).getType(),
571
571
  )!;
572
572
  }
573
573
 
@@ -1,6 +1,6 @@
1
1
  export const usageSymbol = Symbol.for('Pothos.isUsed');
2
2
 
3
- export function wrapWithUsageCheck<T extends Object>(obj: T): T {
3
+ export function wrapWithUsageCheck<T extends object>(obj: T): T {
4
4
  const result = {};
5
5
  let used = true;
6
6