@pothos/plugin-drizzle 0.0.1

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 (179) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +6 -0
  3. package/README.md +437 -0
  4. package/TODO.md +21 -0
  5. package/drizzle/meta/_journal.json +1 -0
  6. package/drizzle.config.ts +10 -0
  7. package/dts/drizzle-field-builder.d.ts +124 -0
  8. package/dts/drizzle-field-builder.d.ts.map +1 -0
  9. package/dts/field-builder.d.ts +2 -0
  10. package/dts/field-builder.d.ts.map +1 -0
  11. package/dts/global-types.d.ts +92 -0
  12. package/dts/global-types.d.ts.map +1 -0
  13. package/dts/index.d.ts +13 -0
  14. package/dts/index.d.ts.map +1 -0
  15. package/dts/interface-ref.d.ts +9 -0
  16. package/dts/interface-ref.d.ts.map +1 -0
  17. package/dts/model-loader.d.ts +44 -0
  18. package/dts/model-loader.d.ts.map +1 -0
  19. package/dts/node-ref.d.ts +5 -0
  20. package/dts/node-ref.d.ts.map +1 -0
  21. package/dts/object-ref.d.ts +14 -0
  22. package/dts/object-ref.d.ts.map +1 -0
  23. package/dts/schema-builder.d.ts +2 -0
  24. package/dts/schema-builder.d.ts.map +1 -0
  25. package/dts/types.d.ts +147 -0
  26. package/dts/types.d.ts.map +1 -0
  27. package/dts/utils/config.d.ts +7 -0
  28. package/dts/utils/config.d.ts.map +1 -0
  29. package/dts/utils/cursors.d.ts +58 -0
  30. package/dts/utils/cursors.d.ts.map +1 -0
  31. package/dts/utils/deep-equal.d.ts +2 -0
  32. package/dts/utils/deep-equal.d.ts.map +1 -0
  33. package/dts/utils/drizzle-graphql.d.ts +18 -0
  34. package/dts/utils/drizzle-graphql.d.ts.map +1 -0
  35. package/dts/utils/loader-map.d.ts +11 -0
  36. package/dts/utils/loader-map.d.ts.map +1 -0
  37. package/dts/utils/map-query.d.ts +30 -0
  38. package/dts/utils/map-query.d.ts.map +1 -0
  39. package/dts/utils/refs.d.ts +5 -0
  40. package/dts/utils/refs.d.ts.map +1 -0
  41. package/dts/utils/selections.d.ts +22 -0
  42. package/dts/utils/selections.d.ts.map +1 -0
  43. package/dts/utils/usage.d.ts +5 -0
  44. package/dts/utils/usage.d.ts.map +1 -0
  45. package/esm/drizzle-field-builder.d.ts +216 -0
  46. package/esm/drizzle-field-builder.d.ts.map +1 -0
  47. package/esm/field-builder.d.ts +2 -0
  48. package/esm/field-builder.d.ts.map +1 -0
  49. package/esm/global-types.d.ts +124 -0
  50. package/esm/global-types.d.ts.map +1 -0
  51. package/esm/index.d.ts +13 -0
  52. package/esm/index.d.ts.map +1 -0
  53. package/esm/interface-ref.d.ts +9 -0
  54. package/esm/interface-ref.d.ts.map +1 -0
  55. package/esm/model-loader.d.ts +45 -0
  56. package/esm/model-loader.d.ts.map +1 -0
  57. package/esm/node-ref.d.ts +5 -0
  58. package/esm/node-ref.d.ts.map +1 -0
  59. package/esm/object-ref.d.ts +14 -0
  60. package/esm/object-ref.d.ts.map +1 -0
  61. package/esm/package.json +3 -0
  62. package/esm/schema-builder.d.ts +2 -0
  63. package/esm/schema-builder.d.ts.map +1 -0
  64. package/esm/src/drizzle-field-builder.js +215 -0
  65. package/esm/src/drizzle-field-builder.js.map +1 -0
  66. package/esm/src/field-builder.js +77 -0
  67. package/esm/src/field-builder.js.map +1 -0
  68. package/esm/src/global-types.js +2 -0
  69. package/esm/src/global-types.js.map +1 -0
  70. package/esm/src/index.js +107 -0
  71. package/esm/src/index.js.map +1 -0
  72. package/esm/src/interface-ref.js +26 -0
  73. package/esm/src/interface-ref.js.map +1 -0
  74. package/esm/src/model-loader.js +168 -0
  75. package/esm/src/model-loader.js.map +1 -0
  76. package/esm/src/node-ref.js +4 -0
  77. package/esm/src/node-ref.js.map +1 -0
  78. package/esm/src/object-ref.js +38 -0
  79. package/esm/src/object-ref.js.map +1 -0
  80. package/esm/src/schema-builder.js +190 -0
  81. package/esm/src/schema-builder.js.map +1 -0
  82. package/esm/src/types.js +2 -0
  83. package/esm/src/types.js.map +1 -0
  84. package/esm/src/utils/config.js +15 -0
  85. package/esm/src/utils/config.js.map +1 -0
  86. package/esm/src/utils/cursors.js +316 -0
  87. package/esm/src/utils/cursors.js.map +1 -0
  88. package/esm/src/utils/deep-equal.js +44 -0
  89. package/esm/src/utils/deep-equal.js.map +1 -0
  90. package/esm/src/utils/drizzle-graphql.js +176 -0
  91. package/esm/src/utils/drizzle-graphql.js.map +1 -0
  92. package/esm/src/utils/loader-map.js +35 -0
  93. package/esm/src/utils/loader-map.js.map +1 -0
  94. package/esm/src/utils/map-query.js +284 -0
  95. package/esm/src/utils/map-query.js.map +1 -0
  96. package/esm/src/utils/refs.js +14 -0
  97. package/esm/src/utils/refs.js.map +1 -0
  98. package/esm/src/utils/selections.js +136 -0
  99. package/esm/src/utils/selections.js.map +1 -0
  100. package/esm/src/utils/usage.js +59 -0
  101. package/esm/src/utils/usage.js.map +1 -0
  102. package/esm/types.d.ts +161 -0
  103. package/esm/types.d.ts.map +1 -0
  104. package/esm/utils/config.d.ts +7 -0
  105. package/esm/utils/config.d.ts.map +1 -0
  106. package/esm/utils/cursors.d.ts +58 -0
  107. package/esm/utils/cursors.d.ts.map +1 -0
  108. package/esm/utils/deep-equal.d.ts +2 -0
  109. package/esm/utils/deep-equal.d.ts.map +1 -0
  110. package/esm/utils/drizzle-graphql.d.ts +18 -0
  111. package/esm/utils/drizzle-graphql.d.ts.map +1 -0
  112. package/esm/utils/loader-map.d.ts +11 -0
  113. package/esm/utils/loader-map.d.ts.map +1 -0
  114. package/esm/utils/map-query.d.ts +30 -0
  115. package/esm/utils/map-query.d.ts.map +1 -0
  116. package/esm/utils/refs.d.ts +5 -0
  117. package/esm/utils/refs.d.ts.map +1 -0
  118. package/esm/utils/selections.d.ts +22 -0
  119. package/esm/utils/selections.d.ts.map +1 -0
  120. package/esm/utils/usage.d.ts +5 -0
  121. package/esm/utils/usage.d.ts.map +1 -0
  122. package/lib/src/drizzle-field-builder.js +225 -0
  123. package/lib/src/drizzle-field-builder.js.map +1 -0
  124. package/lib/src/field-builder.js +82 -0
  125. package/lib/src/field-builder.js.map +1 -0
  126. package/lib/src/global-types.js +6 -0
  127. package/lib/src/global-types.js.map +1 -0
  128. package/lib/src/index.js +180 -0
  129. package/lib/src/index.js.map +1 -0
  130. package/lib/src/interface-ref.js +36 -0
  131. package/lib/src/interface-ref.js.map +1 -0
  132. package/lib/src/model-loader.js +176 -0
  133. package/lib/src/model-loader.js.map +1 -0
  134. package/lib/src/node-ref.js +15 -0
  135. package/lib/src/node-ref.js.map +1 -0
  136. package/lib/src/object-ref.js +56 -0
  137. package/lib/src/object-ref.js.map +1 -0
  138. package/lib/src/schema-builder.js +236 -0
  139. package/lib/src/schema-builder.js.map +1 -0
  140. package/lib/src/types.js +13 -0
  141. package/lib/src/types.js.map +1 -0
  142. package/lib/src/utils/config.js +26 -0
  143. package/lib/src/utils/config.js.map +1 -0
  144. package/lib/src/utils/cursors.js +362 -0
  145. package/lib/src/utils/cursors.js.map +1 -0
  146. package/lib/src/utils/deep-equal.js +55 -0
  147. package/lib/src/utils/deep-equal.js.map +1 -0
  148. package/lib/src/utils/drizzle-graphql.js +192 -0
  149. package/lib/src/utils/drizzle-graphql.js.map +1 -0
  150. package/lib/src/utils/loader-map.js +57 -0
  151. package/lib/src/utils/loader-map.js.map +1 -0
  152. package/lib/src/utils/map-query.js +308 -0
  153. package/lib/src/utils/map-query.js.map +1 -0
  154. package/lib/src/utils/refs.js +33 -0
  155. package/lib/src/utils/refs.js.map +1 -0
  156. package/lib/src/utils/selections.js +163 -0
  157. package/lib/src/utils/selections.js.map +1 -0
  158. package/lib/src/utils/usage.js +84 -0
  159. package/lib/src/utils/usage.js.map +1 -0
  160. package/package.json +66 -0
  161. package/src/drizzle-field-builder.ts +500 -0
  162. package/src/field-builder.ts +168 -0
  163. package/src/global-types.ts +481 -0
  164. package/src/index.ts +175 -0
  165. package/src/interface-ref.ts +24 -0
  166. package/src/model-loader.ts +235 -0
  167. package/src/node-ref.ts +8 -0
  168. package/src/object-ref.ts +51 -0
  169. package/src/schema-builder.ts +299 -0
  170. package/src/types.ts +640 -0
  171. package/src/utils/config.ts +28 -0
  172. package/src/utils/cursors.ts +446 -0
  173. package/src/utils/deep-equal.ts +58 -0
  174. package/src/utils/drizzle-graphql.ts +235 -0
  175. package/src/utils/loader-map.ts +51 -0
  176. package/src/utils/map-query.ts +582 -0
  177. package/src/utils/refs.ts +25 -0
  178. package/src/utils/selections.ts +209 -0
  179. package/src/utils/usage.ts +64 -0
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ extendWithUsage: function() {
13
+ return extendWithUsage;
14
+ },
15
+ isUsed: function() {
16
+ return isUsed;
17
+ },
18
+ usageSymbol: function() {
19
+ return usageSymbol;
20
+ },
21
+ wrapWithUsageCheck: function() {
22
+ return wrapWithUsageCheck;
23
+ }
24
+ });
25
+ const usageSymbol = Symbol.for('Pothos.isUsed');
26
+ function wrapWithUsageCheck(obj) {
27
+ const result = {};
28
+ let used = true;
29
+ Object.defineProperty(result, usageSymbol, {
30
+ get () {
31
+ return used;
32
+ },
33
+ set (value) {
34
+ used = value;
35
+ },
36
+ enumerable: false
37
+ });
38
+ for (const key of Object.keys(obj)){
39
+ // only set to false if the object has keys
40
+ used = false;
41
+ Object.defineProperty(result, key, {
42
+ enumerable: true,
43
+ configurable: true,
44
+ // eslint-disable-next-line @typescript-eslint/no-loop-func
45
+ get () {
46
+ used = true;
47
+ return obj[key];
48
+ }
49
+ });
50
+ }
51
+ return result;
52
+ }
53
+ function isUsed(obj) {
54
+ return !(usageSymbol in obj) || obj[usageSymbol];
55
+ }
56
+ function extendWithUsage(original, extension) {
57
+ if (!(usageSymbol in original)) {
58
+ return {
59
+ ...original,
60
+ ...extension
61
+ };
62
+ }
63
+ const result = {
64
+ ...extension
65
+ };
66
+ for (const key of [
67
+ usageSymbol,
68
+ ...Object.keys(original)
69
+ ]){
70
+ if (key in result) {
71
+ continue;
72
+ }
73
+ Object.defineProperty(result, key, {
74
+ enumerable: key !== usageSymbol,
75
+ configurable: key !== usageSymbol,
76
+ get () {
77
+ return original[key];
78
+ }
79
+ });
80
+ }
81
+ return result;
82
+ }
83
+
84
+ //# sourceMappingURL=usage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/utils/usage.ts"],"names":["extendWithUsage","isUsed","usageSymbol","wrapWithUsageCheck","Symbol","for","obj","result","used","Object","defineProperty","get","set","value","enumerable","key","keys","configurable","original","extension"],"mappings":";;;;;;;;;;;IAqCgBA,eAAe;eAAfA;;IAJAC,MAAM;eAANA;;IAjCHC,WAAW;eAAXA;;IAEGC,kBAAkB;eAAlBA;;;AAFT,MAAMD,cAAcE,OAAOC,GAAG,CAAC;AAE/B,SAASF,mBAAqCG,GAAM;IACzD,MAAMC,SAAS,CAAC;IAChB,IAAIC,OAAO;IAEXC,OAAOC,cAAc,CAACH,QAAQL,aAAa;QACzCS;YACE,OAAOH;QACT;QACAI,KAAIC,KAAc;YAChBL,OAAOK;QACT;QACAC,YAAY;IACd;IAEA,KAAK,MAAMC,OAAON,OAAOO,IAAI,CAACV,KAAM;QAClC,2CAA2C;QAC3CE,OAAO;QACPC,OAAOC,cAAc,CAACH,QAAQQ,KAAK;YACjCD,YAAY;YACZG,cAAc;YACd,2DAA2D;YAC3DN;gBACEH,OAAO;gBACP,OAAOF,GAAG,CAACS,IAAe;YAC5B;QACF;IACF;IAEA,OAAOR;AACT;AAEO,SAASN,OAAOK,GAAW;IAChC,OAAO,CAAEJ,CAAAA,eAAeI,GAAE,KAAM,AAACA,GAAkC,CAACJ,YAAY;AAClF;AAEO,SAASF,gBACdkB,QAAW,EACXC,SAAY;IAEZ,IAAI,CAAEjB,CAAAA,eAAegB,QAAO,GAAI;QAC9B,OAAO;YAAE,GAAGA,QAAQ;YAAE,GAAGC,SAAS;QAAC;IACrC;IAEA,MAAMZ,SAAS;QAAE,GAAGY,SAAS;IAAC;IAE9B,KAAK,MAAMJ,OAAO;QAACb;WAAgBO,OAAOO,IAAI,CAACE;KAAU,CAAE;QACzD,IAAIH,OAAOR,QAAQ;YAEjB;QACF;QAEAE,OAAOC,cAAc,CAACH,QAAQQ,KAAK;YACjCD,YAAYC,QAAQb;YACpBe,cAAcF,QAAQb;YACtBS;gBACE,OAAOO,QAAQ,CAACH,IAAe;YACjC;QACF;IACF;IAEA,OAAOR;AACT"}
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@pothos/plugin-drizzle",
3
+ "version": "0.0.1",
4
+ "description": "A Pothos plugin for drizzle",
5
+ "main": "./lib/index.js",
6
+ "types": "./dts/index.d.ts",
7
+ "module": "./esm/index.js",
8
+ "exports": {
9
+ "import": {
10
+ "default": "./esm/index.js"
11
+ },
12
+ "require": {
13
+ "types": "./dts/index.d.ts",
14
+ "default": "./lib/index.js"
15
+ }
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/hayes/pothos.git",
20
+ "directory": "packages/plugin-relay"
21
+ },
22
+ "author": "Michael Hayes",
23
+ "license": "ISC",
24
+ "keywords": [
25
+ "pothos",
26
+ "graphql",
27
+ "schema",
28
+ "typescript",
29
+ "drizzle",
30
+ "plugin"
31
+ ],
32
+ "publishConfig": {
33
+ "access": "public",
34
+ "provenance": true
35
+ },
36
+ "peerDependencies": {
37
+ "@pothos/core": "*",
38
+ "graphql": "^16.8.1"
39
+ },
40
+ "devDependencies": {
41
+ "@libsql/client": "^0.7.0",
42
+ "drizzle-graphql": "^0.8.4",
43
+ "drizzle-kit": "^0.23.0",
44
+ "drizzle-orm": "^0.32.0",
45
+ "graphql": "^16.8.1",
46
+ "graphql-scalars": "^1.23.0",
47
+ "graphql-tag": "^2.12.6",
48
+ "postgres": "^3.4.4",
49
+ "@pothos/core": "4.0.2",
50
+ "@pothos/plugin-add-graphql": "4.0.2",
51
+ "@pothos/plugin-relay": "4.0.2",
52
+ "@pothos/plugin-scope-auth": "4.0.2",
53
+ "@pothos/plugin-with-input": "4.0.2",
54
+ "@pothos/test-utils": "2.0.1"
55
+ },
56
+ "scripts": {
57
+ "type": "tsc --project tsconfig.type.json",
58
+ "build": "pnpm build:clean && pnpm build:cjs && pnpm build:dts && pnpm build:esm",
59
+ "build:clean": "git clean -dfX esm lib",
60
+ "build:cjs": "swc src -d lib --config-file ../../.swcrc -C module.type=commonjs",
61
+ "build:esm": "cp -r dts/* esm/ && swc src -d esm --config-file ../../.swcrc -C module.type=es6 && pnpm esm:extensions",
62
+ "build:dts": "tsc",
63
+ "esm:extensions": "TS_NODE_PROJECT=../../tsconfig.json node -r @swc-node/register ../../scripts/esm-transformer.ts",
64
+ "test": "pnpm vitest --run"
65
+ }
66
+ }
@@ -0,0 +1,500 @@
1
+ import { getOperators, InferModelFromColumns, Many, TableRelationalConfig } from 'drizzle-orm';
2
+ import {
3
+ CompatibleTypes,
4
+ ExposeNullability,
5
+ FieldKind,
6
+ FieldRef,
7
+ InputFieldMap,
8
+ InterfaceParam,
9
+ isThenable,
10
+ MaybePromise,
11
+ NormalizeArgs,
12
+ ObjectRef,
13
+ PluginName,
14
+ PothosSchemaError,
15
+ RootFieldBuilder,
16
+ SchemaTypes,
17
+ ShapeFromTypeParam,
18
+ TypeParam,
19
+ } from '@pothos/core';
20
+ import {
21
+ DrizzleConnectionShape,
22
+ ListRelation,
23
+ RelatedConnectionOptions,
24
+ RelatedFieldOptions,
25
+ ShapeFromConnection,
26
+ TypesForRelation,
27
+ VariantFieldOptions,
28
+ } from './types';
29
+ import { getRefFromModel } from './utils/refs';
30
+ import {
31
+ drizzleCursorConnectionQuery,
32
+ getCursorFormatter,
33
+ wrapConnectionResult,
34
+ } from './utils/cursors';
35
+ import { SelectionMap } from './utils/selections';
36
+ import { DrizzleRef } from './interface-ref';
37
+
38
+ // Workaround for FieldKind not being extended on Builder classes
39
+ const RootBuilder: {
40
+ // eslint-disable-next-line @typescript-eslint/prefer-function-type
41
+ new <Types extends SchemaTypes, Shape, Kind extends FieldKind>(
42
+ builder: PothosSchemaTypes.SchemaBuilder<Types>,
43
+ kind: FieldKind,
44
+ graphqlKind: PothosSchemaTypes.PothosKindToGraphQLType[FieldKind],
45
+ ): PothosSchemaTypes.RootFieldBuilder<Types, Shape, Kind>;
46
+ } = RootFieldBuilder as never;
47
+
48
+ const ops = getOperators();
49
+
50
+ export class DrizzleObjectFieldBuilder<
51
+ Types extends SchemaTypes,
52
+ TableConfig extends TableRelationalConfig,
53
+ Shape,
54
+ ExposableShape = InferModelFromColumns<TableConfig['columns']>,
55
+ > extends RootBuilder<Types, Shape, 'DrizzleObject'> {
56
+ exposeBoolean = this.createExpose('Boolean');
57
+
58
+ exposeFloat = this.createExpose('Float');
59
+
60
+ exposeInt = this.createExpose('Int');
61
+
62
+ exposeID = this.createExpose('ID');
63
+
64
+ exposeString = this.createExpose('String');
65
+
66
+ exposeBooleanList = this.createExpose(['Boolean']);
67
+
68
+ exposeFloatList = this.createExpose(['Float']);
69
+
70
+ exposeIntList = this.createExpose(['Int']);
71
+
72
+ exposeIDList = this.createExpose(['ID']);
73
+
74
+ exposeStringList = this.createExpose(['String']);
75
+
76
+ table: string;
77
+
78
+ typename: string;
79
+
80
+ relatedConnection: 'relay' extends PluginName
81
+ ? <
82
+ Field extends ListRelation<TableConfig>,
83
+ Nullable extends boolean,
84
+ Args extends InputFieldMap,
85
+ ConnectionInterfaces extends InterfaceParam<Types>[] = [],
86
+ EdgeInterfaces extends InterfaceParam<Types>[] = [],
87
+ >(
88
+ field: Field,
89
+ options: RelatedConnectionOptions<Types, Shape, TableConfig, Field, Nullable, Args>,
90
+ ...args: NormalizeArgs<
91
+ [
92
+ connectionOptions:
93
+ | ObjectRef<
94
+ Types,
95
+ ShapeFromConnection<
96
+ PothosSchemaTypes.ConnectionShapeHelper<
97
+ Types,
98
+ TypesForRelation<Types, TableConfig['relations'][Field]>,
99
+ false
100
+ >
101
+ >
102
+ >
103
+ | PothosSchemaTypes.ConnectionObjectOptions<
104
+ Types,
105
+ ObjectRef<
106
+ Types,
107
+ ShapeFromTypeParam<
108
+ Types,
109
+ [ObjectRef<Types, TypesForRelation<Types, TableConfig['relations'][Field]>>],
110
+ Nullable
111
+ >
112
+ >,
113
+ false,
114
+ false,
115
+ DrizzleConnectionShape<
116
+ Types,
117
+ ShapeFromTypeParam<
118
+ Types,
119
+ [ObjectRef<Types, TypesForRelation<Types, TableConfig['relations'][Field]>>],
120
+ Nullable
121
+ >,
122
+ Shape,
123
+ Args
124
+ >,
125
+ ConnectionInterfaces
126
+ >,
127
+ edgeOptions:
128
+ | ObjectRef<
129
+ Types,
130
+ {
131
+ cursor: string;
132
+ node?: TypesForRelation<Types, TableConfig['relations'][Field]>;
133
+ }
134
+ >
135
+ | PothosSchemaTypes.ConnectionEdgeObjectOptions<
136
+ Types,
137
+ ObjectRef<
138
+ Types,
139
+ ShapeFromTypeParam<
140
+ Types,
141
+ [ObjectRef<Types, TypesForRelation<Types, TableConfig['relations'][Field]>>],
142
+ Nullable
143
+ >
144
+ >,
145
+ false,
146
+ DrizzleConnectionShape<
147
+ Types,
148
+ ShapeFromTypeParam<
149
+ Types,
150
+ [ObjectRef<Types, TypesForRelation<Types, TableConfig['relations'][Field]>>],
151
+ Nullable
152
+ >,
153
+ Shape,
154
+ Args
155
+ >,
156
+ EdgeInterfaces
157
+ >,
158
+ ],
159
+ 0
160
+ >
161
+ ) => FieldRef<
162
+ Types,
163
+ ShapeFromConnection<PothosSchemaTypes.ConnectionShapeHelper<Types, Shape, Nullable>>
164
+ >
165
+ : '@pothos/plugin-relay is required to use this method' = function relatedConnection(
166
+ this: DrizzleObjectFieldBuilder<SchemaTypes, TableConfig, boolean>,
167
+ name: string,
168
+ {
169
+ maxSize = this.builder.options.drizzle?.maxConnectionSize,
170
+ defaultSize = this.builder.options.drizzle?.defaultConnectionSize,
171
+ query,
172
+ resolve,
173
+ extensions,
174
+ description,
175
+ ...options
176
+ }: {
177
+ type?: ObjectRef<Types, unknown, unknown>;
178
+ maxSize?: number | ((args: {}, ctx: {}) => number);
179
+ defaultSize?: number | ((args: {}, ctx: {}) => number);
180
+ extensions: {};
181
+ description?: string;
182
+ query: ((args: {}, ctx: {}) => {}) | {};
183
+ resolve: (
184
+ query: {},
185
+ parent: unknown,
186
+ args: {},
187
+ ctx: {},
188
+ info: {},
189
+ ) => MaybePromise<readonly {}[]>;
190
+ },
191
+ connectionOptions = {},
192
+ edgeOptions = {},
193
+ ) {
194
+ const relationField =
195
+ this.builder.options.drizzle.client._.schema?.[this.table].relations[name as string];
196
+ const relatedModel =
197
+ this.builder.options.drizzle.client._.schema?.[relationField?.referencedTableName!]!;
198
+
199
+ if (!relationField) {
200
+ throw new PothosSchemaError(
201
+ `Could not find relation ${name as string} on table ${this.table}`,
202
+ );
203
+ }
204
+
205
+ const ref = options.type ?? getRefFromModel(relationField.referencedTableName, this.builder);
206
+ let typeName: string | undefined;
207
+
208
+ const getQuery = (args: PothosSchemaTypes.DefaultConnectionArguments, ctx: {}) => {
209
+ const { limit, offset, orderBy, where, ...fieldQuery } =
210
+ (typeof query === 'function' ? query(args, ctx) : query) ?? {};
211
+
212
+ const { cursorColumns, columns, ...connectionQuery } = drizzleCursorConnectionQuery({
213
+ ctx,
214
+ maxSize,
215
+ defaultSize,
216
+ args,
217
+ orderBy: orderBy ? orderBy(relatedModel.columns) : relatedModel.primaryKey,
218
+ where: where ? where(relatedModel.columns, ops) : undefined,
219
+ });
220
+
221
+ return {
222
+ select: {
223
+ ...fieldQuery,
224
+ ...connectionQuery,
225
+ columns: {
226
+ ...fieldQuery.columns,
227
+ ...columns,
228
+ },
229
+ limit: Math.abs(limit ?? connectionQuery.limit),
230
+ offset: offset ?? connectionQuery.offset,
231
+ },
232
+ cursorColumns,
233
+ };
234
+ };
235
+
236
+ const relationSelect = (
237
+ args: object,
238
+ context: object,
239
+ nestedQuery: (query: unknown, path?: unknown) => { select?: {} },
240
+ ) => {
241
+ typeName ??= this.builder.configStore.getTypeConfig(ref).name;
242
+ const nested = nestedQuery(getQuery(args, context).select, {
243
+ getType: () => typeName!,
244
+ paths: [[{ name: 'nodes' }], [{ name: 'edges' }, { name: 'node' }]],
245
+ }) as SelectionMap;
246
+
247
+ return {
248
+ columns: {},
249
+ with: {
250
+ [name]: nested,
251
+ },
252
+ };
253
+ };
254
+ const fieldRef = (
255
+ this as unknown as {
256
+ connection: (...args: unknown[]) => FieldRef<Types, unknown>;
257
+ }
258
+ ).connection(
259
+ {
260
+ ...options,
261
+ extensions: {
262
+ ...extensions,
263
+ pothosDrizzleSelect: relationSelect,
264
+ },
265
+ type: ref,
266
+ resolve: (
267
+ parent: unknown,
268
+ args: PothosSchemaTypes.DefaultConnectionArguments,
269
+ context: {},
270
+ ) => {
271
+ const { select, cursorColumns } = getQuery(args, context);
272
+ return wrapConnectionResult(
273
+ (parent as Record<string, never>)[name],
274
+ args,
275
+ select.limit,
276
+ getCursorFormatter(cursorColumns),
277
+ );
278
+ },
279
+ },
280
+ connectionOptions instanceof ObjectRef
281
+ ? connectionOptions
282
+ : {
283
+ ...connectionOptions,
284
+ },
285
+ edgeOptions,
286
+ );
287
+ return fieldRef;
288
+ } as never;
289
+
290
+ constructor(
291
+ typename: string,
292
+ builder: PothosSchemaTypes.SchemaBuilder<Types>,
293
+ table: string,
294
+ graphqlKind: PothosSchemaTypes.PothosKindToGraphQLType[FieldKind] = 'Object',
295
+ ) {
296
+ super(builder, 'DrizzleObject', graphqlKind);
297
+
298
+ this.table = table;
299
+ this.typename = typename;
300
+ }
301
+
302
+ variant<
303
+ Variant extends DrizzleRef<Types, TableConfig['tsName']> | TableConfig['tsName'],
304
+ Args extends InputFieldMap,
305
+ Nullable,
306
+ ResolveShape,
307
+ ResolveReturn,
308
+ >(
309
+ variant: Variant,
310
+ ...allArgs: NormalizeArgs<
311
+ [
312
+ options: VariantFieldOptions<
313
+ Types,
314
+ TableConfig['tsName'] & keyof Types['DrizzleRelationSchema'],
315
+ Variant,
316
+ Args,
317
+ Nullable,
318
+ Shape,
319
+ ResolveShape,
320
+ ResolveReturn
321
+ >,
322
+ ]
323
+ >
324
+ ): FieldRef<Types, ResolveReturn, 'Object'> {
325
+ const [{ isNull, nullable, ...options } = {} as never] = allArgs;
326
+ const ref: DrizzleRef<Types> =
327
+ typeof variant === 'string' ? getRefFromModel(variant, this.builder) : variant;
328
+
329
+ const selfSelect = (args: object, context: object, nestedQuery: (query: unknown) => unknown) =>
330
+ nestedQuery({});
331
+
332
+ return this.field({
333
+ ...(options as {}),
334
+ type: ref,
335
+ extensions: {
336
+ ...options?.extensions,
337
+ pothosDrizzleSelect: selfSelect,
338
+ },
339
+ nullable: nullable ?? !!isNull,
340
+ resolve: isNull
341
+ ? (parent, args, context, info) => {
342
+ const parentIsNull = isNull(parent as never, args as never, context, info);
343
+ if (parentIsNull) {
344
+ if (isThenable(parentIsNull)) {
345
+ return parentIsNull.then((resolved) => (resolved ? null : parent)) as never;
346
+ }
347
+ return null as never;
348
+ }
349
+ return parent as never;
350
+ }
351
+ : (parent) => parent as never,
352
+ }) as FieldRef<Types, ResolveReturn, 'Object'>;
353
+ }
354
+
355
+ relation<
356
+ Field extends keyof TableConfig['relations'],
357
+ Nullable extends boolean,
358
+ Args extends InputFieldMap,
359
+ ResolveReturnShape,
360
+ >(
361
+ name: Field,
362
+ ...allArgs: NormalizeArgs<
363
+ [
364
+ options: RelatedFieldOptions<
365
+ Types,
366
+ TableConfig,
367
+ Field,
368
+ Nullable,
369
+ Args,
370
+ ResolveReturnShape,
371
+ Shape
372
+ >,
373
+ ]
374
+ >
375
+ ): FieldRef<Types, TypesForRelation<Types, TableConfig['relations'][Field]>, 'Object'> {
376
+ const [options = {} as never] = allArgs;
377
+ const relationField =
378
+ this.builder.options.drizzle.client._.schema?.[this.table].relations[name as string];
379
+
380
+ if (!relationField) {
381
+ throw new PothosSchemaError(
382
+ `Could not find relation ${name as string} on table ${this.table}`,
383
+ );
384
+ }
385
+
386
+ const ref = options.type ?? getRefFromModel(relationField.referencedTableName, this.builder);
387
+
388
+ const { query = {}, extensions, ...rest } = options;
389
+
390
+ const relationSelect = (args: object, context: object, nestedQuery: (query: unknown) => {}) => {
391
+ const relQuery = {
392
+ columns: {},
393
+ with: {
394
+ [name]: {
395
+ ...nestedQuery(query),
396
+ ...((typeof query === 'function'
397
+ ? (query as (args: {}, context: {}) => {})(args, context)
398
+ : query) as {}),
399
+ },
400
+ },
401
+ };
402
+
403
+ return relQuery;
404
+ };
405
+
406
+ return this.field({
407
+ ...(rest as {}),
408
+ type: relationField instanceof Many ? [ref] : ref,
409
+ extensions: {
410
+ ...extensions,
411
+ pothosDrizzleSelect: relationSelect as never,
412
+ },
413
+ resolve: (parent: Record<string, never>) => parent[name as string],
414
+ } as never) as never;
415
+ }
416
+
417
+ expose<
418
+ Type extends TypeParam<Types>,
419
+ Nullable extends boolean,
420
+ ResolveReturnShape,
421
+ Name extends CompatibleTypes<Types, ExposableShape, Type, Nullable>,
422
+ >(
423
+ ...args: NormalizeArgs<
424
+ [
425
+ name: Name,
426
+ options: ExposeNullability<Types, Type, ExposableShape, Name, Nullable> &
427
+ Omit<
428
+ PothosSchemaTypes.ObjectFieldOptions<
429
+ Types,
430
+ Shape,
431
+ Type,
432
+ Nullable,
433
+ {},
434
+ ResolveReturnShape
435
+ >,
436
+ 'nullable' | 'resolve' | 'select'
437
+ >,
438
+ ]
439
+ >
440
+ ) {
441
+ const [name, options = {} as never] = args;
442
+
443
+ const typeConfig = this.builder.configStore.getTypeConfig(this.typename, 'Object');
444
+ const usingSelect = !!typeConfig.extensions?.pothosDrizzleSelect;
445
+
446
+ return this.exposeField(name as never, {
447
+ ...options,
448
+ extensions: {
449
+ ...options.extensions,
450
+ pothosDrizzleVariant: name,
451
+ pothosDrizzleSelect: usingSelect && {
452
+ columns: { [name as string]: true },
453
+ },
454
+ },
455
+ });
456
+ }
457
+
458
+ private createExpose<Type extends TypeParam<Types>>(type: Type) {
459
+ return <
460
+ Nullable extends boolean,
461
+ ResolveReturnShape,
462
+ Name extends CompatibleTypes<
463
+ Types,
464
+ ExposableShape,
465
+ Type,
466
+ Type extends [unknown] ? { list: true; items: true } : true
467
+ >,
468
+ >(
469
+ ...args: NormalizeArgs<
470
+ [
471
+ name: Name,
472
+ options: ExposeNullability<Types, Type, ExposableShape, Name, Nullable> &
473
+ Omit<
474
+ PothosSchemaTypes.ObjectFieldOptions<
475
+ Types,
476
+ ExposableShape,
477
+ Type,
478
+ Nullable,
479
+ {},
480
+ ResolveReturnShape
481
+ >,
482
+ 'nullable' | 'resolve' | 'select' | 'type'
483
+ > & {
484
+ description?: string | false;
485
+ },
486
+ ]
487
+ >
488
+ ): FieldRef<Types, ShapeFromTypeParam<Types, Type, Nullable>, 'DrizzleObject'> => {
489
+ const [name, { description, ...options } = {} as never] = args;
490
+
491
+ return this.expose(
492
+ name as never,
493
+ {
494
+ ...options,
495
+ type,
496
+ } as never,
497
+ ) as never;
498
+ };
499
+ }
500
+ }