@pothos/plugin-drizzle 0.0.1 → 0.2.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.
- package/CHANGELOG.md +12 -0
- package/drizzle/meta/_journal.json +1 -1
- package/dts/drizzle-field-builder.d.ts +9 -9
- package/dts/drizzle-field-builder.d.ts.map +1 -1
- package/dts/global-types.d.ts +7 -7
- package/dts/global-types.d.ts.map +1 -1
- package/dts/index.d.ts +2 -2
- package/dts/index.d.ts.map +1 -1
- package/dts/interface-ref.d.ts +2 -2
- package/dts/interface-ref.d.ts.map +1 -1
- package/dts/model-loader.d.ts +7 -5
- package/dts/model-loader.d.ts.map +1 -1
- package/dts/node-ref.d.ts +1 -1
- package/dts/node-ref.d.ts.map +1 -1
- package/dts/object-ref.d.ts +1 -1
- package/dts/object-ref.d.ts.map +1 -1
- package/dts/types.d.ts +21 -14
- package/dts/types.d.ts.map +1 -1
- package/dts/utils/config.d.ts +4 -2
- package/dts/utils/config.d.ts.map +1 -1
- package/dts/utils/cursors.d.ts +7 -7
- package/dts/utils/cursors.d.ts.map +1 -1
- package/dts/utils/deep-equal.d.ts.map +1 -1
- package/dts/utils/drizzle-graphql.d.ts +1 -1
- package/dts/utils/drizzle-graphql.d.ts.map +1 -1
- package/dts/utils/loader-map.d.ts +1 -1
- package/dts/utils/loader-map.d.ts.map +1 -1
- package/dts/utils/map-query.d.ts +3 -3
- package/dts/utils/map-query.d.ts.map +1 -1
- package/dts/utils/refs.d.ts +2 -2
- package/dts/utils/refs.d.ts.map +1 -1
- package/dts/utils/selections.d.ts +3 -3
- package/dts/utils/selections.d.ts.map +1 -1
- package/dts/utils/usage.d.ts.map +1 -1
- package/esm/drizzle-field-builder.d.ts +9 -9
- package/esm/drizzle-field-builder.d.ts.map +1 -1
- package/esm/global-types.d.ts +7 -7
- package/esm/global-types.d.ts.map +1 -1
- package/esm/index.d.ts +2 -2
- package/esm/index.d.ts.map +1 -1
- package/esm/interface-ref.d.ts +2 -2
- package/esm/interface-ref.d.ts.map +1 -1
- package/esm/model-loader.d.ts +7 -5
- package/esm/model-loader.d.ts.map +1 -1
- package/esm/node-ref.d.ts +1 -1
- package/esm/node-ref.d.ts.map +1 -1
- package/esm/object-ref.d.ts +1 -1
- package/esm/object-ref.d.ts.map +1 -1
- package/esm/src/drizzle-field-builder.js +10 -9
- package/esm/src/drizzle-field-builder.js.map +1 -1
- package/esm/src/field-builder.js +4 -4
- package/esm/src/field-builder.js.map +1 -1
- package/esm/src/global-types.js +1 -1
- package/esm/src/global-types.js.map +1 -1
- package/esm/src/index.js +4 -4
- package/esm/src/index.js.map +1 -1
- package/esm/src/interface-ref.js.map +1 -1
- package/esm/src/model-loader.js +9 -6
- package/esm/src/model-loader.js.map +1 -1
- package/esm/src/object-ref.js +4 -2
- package/esm/src/object-ref.js.map +1 -1
- package/esm/src/schema-builder.js +19 -18
- package/esm/src/schema-builder.js.map +1 -1
- package/esm/src/types.js.map +1 -1
- package/esm/src/utils/config.js +15 -2
- package/esm/src/utils/config.js.map +1 -1
- package/esm/src/utils/cursors.js +6 -6
- package/esm/src/utils/cursors.js.map +1 -1
- package/esm/src/utils/deep-equal.js +1 -1
- package/esm/src/utils/deep-equal.js.map +1 -1
- package/esm/src/utils/drizzle-graphql.js +44 -21
- package/esm/src/utils/drizzle-graphql.js.map +1 -1
- package/esm/src/utils/loader-map.js +4 -5
- package/esm/src/utils/loader-map.js.map +1 -1
- package/esm/src/utils/map-query.js +1 -3
- package/esm/src/utils/map-query.js.map +1 -1
- package/esm/src/utils/refs.js.map +1 -1
- package/esm/src/utils/selections.js +3 -3
- package/esm/src/utils/selections.js.map +1 -1
- package/esm/src/utils/usage.js +0 -1
- package/esm/src/utils/usage.js.map +1 -1
- package/esm/types.d.ts +21 -14
- package/esm/types.d.ts.map +1 -1
- package/esm/utils/config.d.ts +4 -2
- package/esm/utils/config.d.ts.map +1 -1
- package/esm/utils/cursors.d.ts +7 -7
- package/esm/utils/cursors.d.ts.map +1 -1
- package/esm/utils/deep-equal.d.ts.map +1 -1
- package/esm/utils/drizzle-graphql.d.ts +1 -1
- package/esm/utils/drizzle-graphql.d.ts.map +1 -1
- package/esm/utils/loader-map.d.ts +1 -1
- package/esm/utils/loader-map.d.ts.map +1 -1
- package/esm/utils/map-query.d.ts +3 -3
- package/esm/utils/map-query.d.ts.map +1 -1
- package/esm/utils/refs.d.ts +2 -2
- package/esm/utils/refs.d.ts.map +1 -1
- package/esm/utils/selections.d.ts +3 -3
- package/esm/utils/selections.d.ts.map +1 -1
- package/esm/utils/usage.d.ts.map +1 -1
- package/lib/src/drizzle-field-builder.js +9 -8
- package/lib/src/drizzle-field-builder.js.map +1 -1
- package/lib/src/field-builder.js +4 -4
- package/lib/src/field-builder.js.map +1 -1
- package/lib/src/global-types.js +1 -1
- package/lib/src/global-types.js.map +1 -1
- package/lib/src/index.js +4 -4
- package/lib/src/index.js.map +1 -1
- package/lib/src/interface-ref.js.map +1 -1
- package/lib/src/model-loader.js +10 -6
- package/lib/src/model-loader.js.map +1 -1
- package/lib/src/object-ref.js +3 -1
- package/lib/src/object-ref.js.map +1 -1
- package/lib/src/schema-builder.js +19 -18
- package/lib/src/schema-builder.js.map +1 -1
- package/lib/src/types.js.map +1 -1
- package/lib/src/utils/config.js +26 -5
- package/lib/src/utils/config.js.map +1 -1
- package/lib/src/utils/cursors.js +7 -6
- package/lib/src/utils/cursors.js.map +1 -1
- package/lib/src/utils/deep-equal.js +1 -1
- package/lib/src/utils/deep-equal.js.map +1 -1
- package/lib/src/utils/drizzle-graphql.js +53 -21
- package/lib/src/utils/drizzle-graphql.js.map +1 -1
- package/lib/src/utils/loader-map.js +4 -5
- package/lib/src/utils/loader-map.js.map +1 -1
- package/lib/src/utils/map-query.js +2 -4
- package/lib/src/utils/map-query.js.map +1 -1
- package/lib/src/utils/refs.js.map +1 -1
- package/lib/src/utils/selections.js +4 -4
- package/lib/src/utils/selections.js.map +1 -1
- package/lib/src/utils/usage.js +0 -1
- package/lib/src/utils/usage.js.map +1 -1
- package/package.json +11 -11
- package/src/drizzle-field-builder.ts +38 -27
- package/src/field-builder.ts +12 -13
- package/src/global-types.ts +28 -20
- package/src/index.ts +29 -36
- package/src/interface-ref.ts +2 -2
- package/src/model-loader.ts +15 -17
- package/src/node-ref.ts +1 -1
- package/src/object-ref.ts +5 -3
- package/src/schema-builder.ts +26 -25
- package/src/types.ts +54 -48
- package/src/utils/config.ts +35 -5
- package/src/utils/cursors.ts +21 -22
- package/src/utils/deep-equal.ts +0 -1
- package/src/utils/drizzle-graphql.ts +53 -14
- package/src/utils/loader-map.ts +5 -7
- package/src/utils/map-query.ts +20 -23
- package/src/utils/refs.ts +2 -2
- package/src/utils/selections.ts +8 -9
- package/src/utils/usage.ts +0 -2
package/src/schema-builder.ts
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
import SchemaBuilder, {
|
|
2
2
|
brandWithType,
|
|
3
3
|
completeValue,
|
|
4
|
-
InterfaceRef,
|
|
5
|
-
ObjectRef,
|
|
6
|
-
OutputType,
|
|
4
|
+
type InterfaceRef,
|
|
5
|
+
type ObjectRef,
|
|
6
|
+
type OutputType,
|
|
7
7
|
PothosError,
|
|
8
|
-
SchemaTypes,
|
|
9
|
-
FieldRef,
|
|
10
|
-
InputObjectRef,
|
|
8
|
+
type SchemaTypes,
|
|
9
|
+
type FieldRef,
|
|
10
|
+
type InputObjectRef,
|
|
11
11
|
} from '@pothos/core';
|
|
12
|
+
import type { GraphQLInputObjectType, GraphQLResolveInfo } from 'graphql';
|
|
12
13
|
import { DrizzleObjectFieldBuilder } from './drizzle-field-builder';
|
|
13
|
-
import {
|
|
14
|
+
import { DrizzleInterfaceRef } from './interface-ref';
|
|
15
|
+
import { ModelLoader } from './model-loader';
|
|
16
|
+
import { DrizzleNodeRef } from './node-ref';
|
|
14
17
|
import { DrizzleObjectRef } from './object-ref';
|
|
15
|
-
import { DrizzleGraphQLInputExtensions, DrizzleNodeOptions } from './types';
|
|
18
|
+
import type { DrizzleGraphQLInputExtensions, DrizzleNodeOptions } from './types';
|
|
19
|
+
import { getSchemaConfig } from './utils/config';
|
|
16
20
|
import { getColumnParser, getColumnSerializer } from './utils/cursors';
|
|
17
|
-
import {
|
|
18
|
-
import { GraphQLInputObjectType, GraphQLResolveInfo } from 'graphql';
|
|
19
|
-
import { ModelLoader } from './model-loader';
|
|
20
|
-
import { DrizzleInterfaceRef } from './interface-ref';
|
|
21
|
+
import { getRefFromModel } from './utils/refs';
|
|
21
22
|
|
|
22
23
|
const schemaBuilderProto = SchemaBuilder.prototype as PothosSchemaTypes.SchemaBuilder<SchemaTypes>;
|
|
23
24
|
|
|
@@ -35,7 +36,7 @@ schemaBuilderProto.drizzleObject = function drizzleObject(table, { select, field
|
|
|
35
36
|
extensions: {
|
|
36
37
|
...options.extensions,
|
|
37
38
|
pothosDrizzleModel: table,
|
|
38
|
-
pothosDrizzleTable: this.
|
|
39
|
+
pothosDrizzleTable: getSchemaConfig(this).schema?.[table],
|
|
39
40
|
pothosDrizzleSelect: select ?? {},
|
|
40
41
|
pothosDrizzleLoader: ModelLoader.forModel(table, this),
|
|
41
42
|
},
|
|
@@ -63,7 +64,7 @@ schemaBuilderProto.drizzleInterface = function drizzleInterface(
|
|
|
63
64
|
extensions: {
|
|
64
65
|
...options.extensions,
|
|
65
66
|
pothosDrizzleModel: table,
|
|
66
|
-
pothosDrizzleTable: this.
|
|
67
|
+
pothosDrizzleTable: getSchemaConfig(this).schema?.[table],
|
|
67
68
|
pothosDrizzleSelect: select ?? {},
|
|
68
69
|
pothosDrizzleLoader: ModelLoader.forModel(table, this),
|
|
69
70
|
},
|
|
@@ -88,7 +89,7 @@ schemaBuilderProto.drizzleNode = function drizzleNode(
|
|
|
88
89
|
...options
|
|
89
90
|
}: DrizzleNodeOptions<SchemaTypes, keyof SchemaTypes['DrizzleRelationSchema'], {}, {}, []>,
|
|
90
91
|
) {
|
|
91
|
-
const tableConfig = this.
|
|
92
|
+
const tableConfig = getSchemaConfig(this).schema![table];
|
|
92
93
|
const idColumn = typeof column === 'function' ? column(tableConfig.columns) : column;
|
|
93
94
|
const idColumns = Array.isArray(idColumn) ? idColumn : [idColumn];
|
|
94
95
|
const interfaceRef = this.nodeInterfaceRef?.();
|
|
@@ -139,12 +140,12 @@ schemaBuilderProto.drizzleNode = function drizzleNode(
|
|
|
139
140
|
globalID: (options: Record<string, unknown>) => FieldRef<SchemaTypes, unknown>;
|
|
140
141
|
}
|
|
141
142
|
).globalID({
|
|
142
|
-
...(this.options as { relayOptions?: { idFieldOptions?:
|
|
143
|
+
...(this.options as { relayOptions?: { idFieldOptions?: object } }).relayOptions
|
|
143
144
|
?.idFieldOptions,
|
|
144
145
|
...idOptions,
|
|
145
146
|
nullable: false,
|
|
146
147
|
args: {},
|
|
147
|
-
resolve: (parent: never
|
|
148
|
+
resolve: (parent: never) =>
|
|
148
149
|
completeValue(resolve(parent), (id) => ({
|
|
149
150
|
type: nodeConfig.name,
|
|
150
151
|
id,
|
|
@@ -198,7 +199,7 @@ schemaBuilderProto.drizzleGraphQLOrderBy = function drizzleGraphQLOrderBy(
|
|
|
198
199
|
this: PothosSchemaTypes.SchemaBuilder<SchemaTypes>,
|
|
199
200
|
table: string,
|
|
200
201
|
type: GraphQLInputObjectType,
|
|
201
|
-
options?: { extensions?:
|
|
202
|
+
options?: { extensions?: object },
|
|
202
203
|
) {
|
|
203
204
|
return (
|
|
204
205
|
this as typeof this & {
|
|
@@ -214,7 +215,7 @@ schemaBuilderProto.drizzleGraphQLOrderBy = function drizzleGraphQLOrderBy(
|
|
|
214
215
|
drizzleGraphQL: {
|
|
215
216
|
inputType: 'orderBy',
|
|
216
217
|
table,
|
|
217
|
-
tableConfig: this.
|
|
218
|
+
tableConfig: getSchemaConfig(this).schema?.[table]!,
|
|
218
219
|
} satisfies DrizzleGraphQLInputExtensions,
|
|
219
220
|
},
|
|
220
221
|
});
|
|
@@ -224,7 +225,7 @@ schemaBuilderProto.drizzleGraphQLFilters = function drizzleGraphQLFilters(
|
|
|
224
225
|
this: PothosSchemaTypes.SchemaBuilder<SchemaTypes>,
|
|
225
226
|
table: string,
|
|
226
227
|
type: GraphQLInputObjectType,
|
|
227
|
-
options?: { extensions?:
|
|
228
|
+
options?: { extensions?: object },
|
|
228
229
|
) {
|
|
229
230
|
return (
|
|
230
231
|
this as typeof this & {
|
|
@@ -240,7 +241,7 @@ schemaBuilderProto.drizzleGraphQLFilters = function drizzleGraphQLFilters(
|
|
|
240
241
|
drizzleGraphQL: {
|
|
241
242
|
inputType: 'filters',
|
|
242
243
|
table,
|
|
243
|
-
tableConfig: this.
|
|
244
|
+
tableConfig: getSchemaConfig(this).schema?.[table]!,
|
|
244
245
|
} satisfies DrizzleGraphQLInputExtensions,
|
|
245
246
|
},
|
|
246
247
|
});
|
|
@@ -250,7 +251,7 @@ schemaBuilderProto.drizzleGraphQLInsert = function drizzleGraphQLInsert(
|
|
|
250
251
|
this: PothosSchemaTypes.SchemaBuilder<SchemaTypes>,
|
|
251
252
|
table: string,
|
|
252
253
|
type: GraphQLInputObjectType,
|
|
253
|
-
options?: { extensions?:
|
|
254
|
+
options?: { extensions?: object },
|
|
254
255
|
) {
|
|
255
256
|
return (
|
|
256
257
|
this as typeof this & {
|
|
@@ -266,7 +267,7 @@ schemaBuilderProto.drizzleGraphQLInsert = function drizzleGraphQLInsert(
|
|
|
266
267
|
drizzleGraphQL: {
|
|
267
268
|
inputType: 'insert',
|
|
268
269
|
table,
|
|
269
|
-
tableConfig: this.
|
|
270
|
+
tableConfig: getSchemaConfig(this).schema?.[table]!,
|
|
270
271
|
} satisfies DrizzleGraphQLInputExtensions,
|
|
271
272
|
},
|
|
272
273
|
});
|
|
@@ -276,7 +277,7 @@ schemaBuilderProto.drizzleGraphQLUpdate = function drizzleGraphQLUpdate(
|
|
|
276
277
|
this: PothosSchemaTypes.SchemaBuilder<SchemaTypes>,
|
|
277
278
|
table: string,
|
|
278
279
|
type: GraphQLInputObjectType,
|
|
279
|
-
options?: { extensions?:
|
|
280
|
+
options?: { extensions?: object },
|
|
280
281
|
) {
|
|
281
282
|
return (
|
|
282
283
|
this as typeof this & {
|
|
@@ -292,7 +293,7 @@ schemaBuilderProto.drizzleGraphQLUpdate = function drizzleGraphQLUpdate(
|
|
|
292
293
|
drizzleGraphQL: {
|
|
293
294
|
inputType: 'update',
|
|
294
295
|
table,
|
|
295
|
-
tableConfig: this.
|
|
296
|
+
tableConfig: getSchemaConfig(this).schema?.[table]!,
|
|
296
297
|
} satisfies DrizzleGraphQLInputExtensions,
|
|
297
298
|
},
|
|
298
299
|
});
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,30 @@
|
|
|
1
1
|
import {
|
|
2
|
+
type ArgumentRef,
|
|
3
|
+
type FieldKind,
|
|
4
|
+
type FieldMap,
|
|
5
|
+
type FieldNullability,
|
|
6
|
+
type FieldOptionsFromKind,
|
|
7
|
+
type InferredFieldOptionKeys,
|
|
8
|
+
type InferredFieldOptionsByKind,
|
|
9
|
+
type InputFieldMap,
|
|
10
|
+
type InputFieldsFromShape,
|
|
11
|
+
type InputShapeFromFields,
|
|
12
|
+
type InterfaceParam,
|
|
13
|
+
type InterfaceRef,
|
|
14
|
+
type InterfaceTypeOptions,
|
|
15
|
+
type ListResolveValue,
|
|
16
|
+
type MaybePromise,
|
|
17
|
+
type Normalize,
|
|
18
|
+
type ObjectRef,
|
|
19
|
+
type ObjectTypeOptions,
|
|
20
|
+
type OutputShape,
|
|
21
|
+
type OutputType,
|
|
22
|
+
type SchemaTypes,
|
|
23
|
+
type ShapeFromTypeParam,
|
|
24
|
+
type TypeParam,
|
|
25
|
+
typeBrandKey,
|
|
26
|
+
} from '@pothos/core';
|
|
27
|
+
import type {
|
|
2
28
|
BuildQueryResult,
|
|
3
29
|
Column,
|
|
4
30
|
DBQueryConfig,
|
|
@@ -9,44 +35,30 @@ import {
|
|
|
9
35
|
TableRelationalConfig,
|
|
10
36
|
TablesRelationalConfig,
|
|
11
37
|
} from 'drizzle-orm';
|
|
12
|
-
import { FieldNode, GraphQLResolveInfo } from 'graphql';
|
|
13
|
-
import {
|
|
14
|
-
ArgumentRef,
|
|
15
|
-
FieldKind,
|
|
16
|
-
FieldMap,
|
|
17
|
-
FieldNullability,
|
|
18
|
-
FieldOptionsFromKind,
|
|
19
|
-
InferredFieldOptionKeys,
|
|
20
|
-
InferredFieldOptionsByKind,
|
|
21
|
-
InputFieldMap,
|
|
22
|
-
InputFieldsFromShape,
|
|
23
|
-
InputShapeFromFields,
|
|
24
|
-
InterfaceParam,
|
|
25
|
-
InterfaceRef,
|
|
26
|
-
InterfaceTypeOptions,
|
|
27
|
-
ListResolveValue,
|
|
28
|
-
MaybePromise,
|
|
29
|
-
Normalize,
|
|
30
|
-
ObjectRef,
|
|
31
|
-
ObjectTypeOptions,
|
|
32
|
-
OutputShape,
|
|
33
|
-
OutputType,
|
|
34
|
-
SchemaTypes,
|
|
35
|
-
ShapeFromTypeParam,
|
|
36
|
-
typeBrandKey,
|
|
37
|
-
TypeParam,
|
|
38
|
-
} from '@pothos/core';
|
|
38
|
+
import type { FieldNode, GraphQLResolveInfo } from 'graphql';
|
|
39
39
|
import type { DrizzleObjectFieldBuilder } from './drizzle-field-builder';
|
|
40
|
-
import {
|
|
41
|
-
import { IndirectInclude } from './utils/map-query';
|
|
42
|
-
import { SelectionMap } from './utils/selections';
|
|
40
|
+
import type { DrizzleRef } from './interface-ref';
|
|
41
|
+
import type { IndirectInclude } from './utils/map-query';
|
|
42
|
+
import type { SelectionMap } from './utils/selections';
|
|
43
|
+
|
|
44
|
+
export type DrizzleClient = {
|
|
45
|
+
_: Partial<RelationalSchemaConfig<TablesRelationalConfig>>;
|
|
46
|
+
query: {};
|
|
47
|
+
};
|
|
43
48
|
|
|
44
|
-
|
|
45
|
-
export interface DrizzlePluginOptions<Types extends SchemaTypes> {
|
|
46
|
-
client: { _: Partial<RelationalSchemaConfig<TablesRelationalConfig>>; query: {} };
|
|
49
|
+
export type DrizzlePluginOptions<Types extends SchemaTypes> = {
|
|
47
50
|
maxConnectionSize?: number;
|
|
48
51
|
defaultConnectionSize?: number;
|
|
49
|
-
}
|
|
52
|
+
} & (
|
|
53
|
+
| {
|
|
54
|
+
client: DrizzleClient;
|
|
55
|
+
schema?: Partial<RelationalSchemaConfig<TablesRelationalConfig>>;
|
|
56
|
+
}
|
|
57
|
+
| {
|
|
58
|
+
client: (ctx: Types['Context']) => DrizzleClient;
|
|
59
|
+
schema: Partial<RelationalSchemaConfig<TablesRelationalConfig>>;
|
|
60
|
+
}
|
|
61
|
+
);
|
|
50
62
|
|
|
51
63
|
export const drizzleTableName = Symbol.for('Pothos.drizzleTableName');
|
|
52
64
|
|
|
@@ -238,6 +250,7 @@ export type DrizzleObjectFieldOptions<
|
|
|
238
250
|
: BuildQueryResult<
|
|
239
251
|
Types['DrizzleRelationSchema'],
|
|
240
252
|
ExtractTable<Types, ParentShape>,
|
|
253
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
241
254
|
Record<string, unknown> & (Select extends (...args: any[]) => infer R ? R : Select)
|
|
242
255
|
> &
|
|
243
256
|
ParentShape,
|
|
@@ -338,6 +351,7 @@ export type RelatedFieldOptions<
|
|
|
338
351
|
export type VariantFieldOptions<
|
|
339
352
|
Types extends SchemaTypes,
|
|
340
353
|
Table extends keyof Types['DrizzleRelationSchema'],
|
|
354
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
341
355
|
Variant extends DrizzleRef<any, Table> | Table,
|
|
342
356
|
Args extends InputFieldMap,
|
|
343
357
|
isNull,
|
|
@@ -348,6 +362,7 @@ export type VariantFieldOptions<
|
|
|
348
362
|
FieldOptionsFromKind<
|
|
349
363
|
Types,
|
|
350
364
|
Shape,
|
|
365
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
351
366
|
Variant extends DrizzleRef<any> ? Variant : DrizzleRef<any, Table>,
|
|
352
367
|
unknown extends isNull ? false : true,
|
|
353
368
|
Args,
|
|
@@ -367,6 +382,7 @@ export type VariantFieldOptions<
|
|
|
367
382
|
Types['DrizzleRelationSchema'],
|
|
368
383
|
ExtractTable<Types, Shape>,
|
|
369
384
|
Record<string, unknown> &
|
|
385
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
370
386
|
(ResolveShape extends (...args: any[]) => infer R ? R : ResolveShape)
|
|
371
387
|
> &
|
|
372
388
|
Shape,
|
|
@@ -465,16 +481,14 @@ export type ListRelation<T extends TableRelationalConfig> = {
|
|
|
465
481
|
export type DrizzleConnectionFieldOptions<
|
|
466
482
|
Types extends SchemaTypes,
|
|
467
483
|
ParentShape,
|
|
468
|
-
Type extends
|
|
469
|
-
|
|
470
|
-
| keyof Types['DrizzleRelationSchema'],
|
|
484
|
+
Type extends // biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
485
|
+
DrizzleRef<any, keyof Types['DrizzleRelationSchema']> | keyof Types['DrizzleRelationSchema'],
|
|
471
486
|
TableConfig extends TableRelationalConfig,
|
|
472
487
|
Param extends OutputType<Types>,
|
|
473
488
|
Nullable extends boolean,
|
|
474
489
|
Args extends InputFieldMap,
|
|
475
490
|
ResolveReturnShape,
|
|
476
491
|
Kind extends FieldKind,
|
|
477
|
-
// eslint-disable-next-line @typescript-eslint/sort-type-constituents
|
|
478
492
|
> = Omit<
|
|
479
493
|
FieldOptionsFromKind<
|
|
480
494
|
Types,
|
|
@@ -510,15 +524,7 @@ export type DrizzleConnectionFieldOptions<
|
|
|
510
524
|
maxSize?: number | ((args: ConnectionArgs, ctx: Types['Context']) => number);
|
|
511
525
|
|
|
512
526
|
resolve: (
|
|
513
|
-
query: <
|
|
514
|
-
T extends QueryForRelatedConnection<
|
|
515
|
-
Types,
|
|
516
|
-
Types['DrizzleRelationSchema'][Type extends DrizzleRef<any, infer K>
|
|
517
|
-
? K
|
|
518
|
-
: Type & keyof Types['DrizzleRelationSchema']],
|
|
519
|
-
ConnectionArgs
|
|
520
|
-
>,
|
|
521
|
-
>(
|
|
527
|
+
query: <T extends QueryForRelatedConnection<Types, TableConfig, ConnectionArgs>>(
|
|
522
528
|
selection?: T,
|
|
523
529
|
) => Omit<T, 'orderBy'> & { orderBy: SQL },
|
|
524
530
|
parent: ParentShape,
|
|
@@ -542,7 +548,6 @@ export type RelatedConnectionOptions<
|
|
|
542
548
|
Args extends InputFieldMap,
|
|
543
549
|
NodeTable extends
|
|
544
550
|
TableRelationalConfig = Types['DrizzleRelationSchema'][Table['relations'][Field]['referencedTable']['_']['name']],
|
|
545
|
-
// eslint-disable-next-line @typescript-eslint/sort-type-constituents
|
|
546
551
|
> = Omit<
|
|
547
552
|
PothosSchemaTypes.ObjectFieldOptions<
|
|
548
553
|
Types,
|
|
@@ -584,6 +589,7 @@ export type RelatedConnectionOptions<
|
|
|
584
589
|
>
|
|
585
590
|
>;
|
|
586
591
|
query: QueryForRelatedConnection<Types, NodeTable, ConnectionArgs>;
|
|
592
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
587
593
|
type?: DrizzleRef<any, Table['relations'][Field]['referencedTable']['_']['name']>;
|
|
588
594
|
|
|
589
595
|
defaultSize?: number | ((args: ConnectionArgs, ctx: Types['Context']) => number);
|
package/src/utils/config.ts
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { type SchemaTypes, createContextCache } from '@pothos/core';
|
|
2
|
+
import type {
|
|
3
|
+
RelationalSchemaConfig,
|
|
4
|
+
TableRelationalConfig,
|
|
5
|
+
TablesRelationalConfig,
|
|
6
|
+
} from 'drizzle-orm';
|
|
7
|
+
import type { DrizzleClient } from '../types';
|
|
3
8
|
|
|
4
9
|
export interface PothosDrizzleSchemaConfig extends RelationalSchemaConfig<TablesRelationalConfig> {
|
|
5
10
|
dbToSchema: Record<string, TableRelationalConfig>;
|
|
6
11
|
}
|
|
7
|
-
const
|
|
8
|
-
(
|
|
12
|
+
const configCache = createContextCache(
|
|
13
|
+
(builder: PothosSchemaTypes.SchemaBuilder<SchemaTypes>): PothosDrizzleSchemaConfig => {
|
|
14
|
+
const config = (builder.options.drizzle.schema ??
|
|
15
|
+
(builder.options.drizzle.client as DrizzleClient)
|
|
16
|
+
._) as RelationalSchemaConfig<TablesRelationalConfig>;
|
|
17
|
+
|
|
9
18
|
const dbToSchema = Object.values(config.schema).reduce<Record<string, TableRelationalConfig>>(
|
|
10
19
|
(acc, table) => {
|
|
11
20
|
acc[table.dbName] = table;
|
|
@@ -21,8 +30,29 @@ const cache = createContextCache(
|
|
|
21
30
|
},
|
|
22
31
|
);
|
|
23
32
|
|
|
33
|
+
const clientCache = createContextCache((builder: PothosSchemaTypes.SchemaBuilder<SchemaTypes>) => {
|
|
34
|
+
const clientConfig = builder.options.drizzle.client;
|
|
35
|
+
const getClient =
|
|
36
|
+
typeof clientConfig === 'function'
|
|
37
|
+
? createContextCache((ctx) => clientConfig(ctx))
|
|
38
|
+
: (_ctx: object) => clientConfig;
|
|
39
|
+
|
|
40
|
+
return createContextCache((context: object) => {
|
|
41
|
+
const client = getClient(context);
|
|
42
|
+
|
|
43
|
+
return client;
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
24
47
|
export function getSchemaConfig<Types extends SchemaTypes>(
|
|
25
48
|
builder: PothosSchemaTypes.SchemaBuilder<Types>,
|
|
26
49
|
) {
|
|
27
|
-
return
|
|
50
|
+
return configCache(builder as never);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function getClient<Types extends SchemaTypes>(
|
|
54
|
+
builder: PothosSchemaTypes.SchemaBuilder<Types>,
|
|
55
|
+
context: object,
|
|
56
|
+
) {
|
|
57
|
+
return clientCache(builder as never)(context);
|
|
28
58
|
}
|
package/src/utils/cursors.ts
CHANGED
|
@@ -1,25 +1,23 @@
|
|
|
1
|
-
/* eslint-disable no-nested-ternary */
|
|
2
1
|
import {
|
|
2
|
+
type MaybePromise,
|
|
3
|
+
PothosValidationError,
|
|
4
|
+
type SchemaTypes,
|
|
3
5
|
decodeBase64,
|
|
4
6
|
encodeBase64,
|
|
5
|
-
MaybePromise,
|
|
6
|
-
PothosValidationError,
|
|
7
|
-
SchemaTypes,
|
|
8
7
|
} from '@pothos/core';
|
|
9
8
|
import {
|
|
10
|
-
Column,
|
|
9
|
+
type Column,
|
|
10
|
+
type DBQueryConfig,
|
|
11
|
+
type SQL,
|
|
12
|
+
type TableRelationalConfig,
|
|
11
13
|
getOperators,
|
|
12
14
|
getOrderByOperators,
|
|
13
|
-
RelationalSchemaConfig,
|
|
14
|
-
SQL,
|
|
15
|
-
TableRelationalConfig,
|
|
16
|
-
TablesRelationalConfig,
|
|
17
15
|
} from 'drizzle-orm';
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
16
|
+
import type { GraphQLResolveInfo } from 'graphql';
|
|
17
|
+
import type { ConnectionOrderBy, QueryForDrizzleConnection } from '../types';
|
|
18
|
+
import type { PothosDrizzleSchemaConfig } from './config';
|
|
21
19
|
import { queryFromInfo } from './map-query';
|
|
22
|
-
import {
|
|
20
|
+
import type { SelectionMap } from './selections';
|
|
23
21
|
|
|
24
22
|
const DEFAULT_MAX_SIZE = 100;
|
|
25
23
|
const DEFAULT_SIZE = 20;
|
|
@@ -224,7 +222,8 @@ function parseOrderBy(orderBy: ConnectionOrderBy, invert: boolean) {
|
|
|
224
222
|
direction: asc,
|
|
225
223
|
column: field.asc,
|
|
226
224
|
};
|
|
227
|
-
}
|
|
225
|
+
}
|
|
226
|
+
if (typeof field === 'object' && 'desc' in field) {
|
|
228
227
|
return {
|
|
229
228
|
direction: desc,
|
|
230
229
|
column: field.desc,
|
|
@@ -297,11 +296,11 @@ export function drizzleCursorConnectionQuery({
|
|
|
297
296
|
|
|
298
297
|
const columns: Record<string, boolean> = {};
|
|
299
298
|
|
|
300
|
-
parsedOrderBy.columns
|
|
299
|
+
for (const column of parsedOrderBy.columns) {
|
|
301
300
|
columns[column.name] = true;
|
|
302
|
-
}
|
|
301
|
+
}
|
|
303
302
|
|
|
304
|
-
|
|
303
|
+
const whereClauses: (SQL | undefined)[] = where ? [where] : [];
|
|
305
304
|
|
|
306
305
|
if (cursor) {
|
|
307
306
|
const cursorParser = getCursorParser(parsedOrderBy.columns);
|
|
@@ -345,7 +344,7 @@ export function wrapConnectionResult<T extends {}>(
|
|
|
345
344
|
args: PothosSchemaTypes.DefaultConnectionArguments,
|
|
346
345
|
limit: number,
|
|
347
346
|
cursor: (node: T) => string,
|
|
348
|
-
resolveNode?: (node: unknown) => unknown,
|
|
347
|
+
resolveNode?: (node: Record<string, unknown>) => unknown,
|
|
349
348
|
) {
|
|
350
349
|
const gotFullResults = results.length === Math.abs(limit);
|
|
351
350
|
const hasNextPage = args.before ? true : args.last ? false : gotFullResults;
|
|
@@ -365,7 +364,7 @@ export function wrapConnectionResult<T extends {}>(
|
|
|
365
364
|
},
|
|
366
365
|
};
|
|
367
366
|
|
|
368
|
-
const edges = nodes.map((value
|
|
367
|
+
const edges = nodes.map((value) =>
|
|
369
368
|
value == null
|
|
370
369
|
? null
|
|
371
370
|
: resolveNode
|
|
@@ -403,8 +402,8 @@ export async function resolveDrizzleCursorConnection<T extends {}>(
|
|
|
403
402
|
queryFn: (query: QueryForDrizzleConnection<SchemaTypes, TableRelationalConfig>) => SelectionMap,
|
|
404
403
|
) => MaybePromise<readonly T[]>,
|
|
405
404
|
) {
|
|
406
|
-
let query
|
|
407
|
-
let formatter;
|
|
405
|
+
let query: DBQueryConfig<'many', false>;
|
|
406
|
+
let formatter: (node: Record<string, unknown>) => string;
|
|
408
407
|
const results = await resolve((q) => {
|
|
409
408
|
const { cursorColumns, ...connectionQuery } = drizzleCursorConnectionQuery({
|
|
410
409
|
...options,
|
|
@@ -442,5 +441,5 @@ export async function resolveDrizzleCursorConnection<T extends {}>(
|
|
|
442
441
|
return results;
|
|
443
442
|
}
|
|
444
443
|
|
|
445
|
-
return wrapConnectionResult(results, options.args, query!.limit, formatter!);
|
|
444
|
+
return wrapConnectionResult(results, options.args, query!.limit as number, formatter!);
|
|
446
445
|
}
|
package/src/utils/deep-equal.ts
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
type Column,
|
|
3
|
+
type SQL,
|
|
4
|
+
Table,
|
|
5
|
+
type TableRelationalConfig,
|
|
6
|
+
asc,
|
|
7
|
+
desc,
|
|
8
|
+
getOperators,
|
|
9
|
+
} from 'drizzle-orm';
|
|
2
10
|
import { GraphQLError } from 'graphql';
|
|
3
11
|
|
|
12
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
4
13
|
export const remapFromGraphQLCore = (value: any, column: Column, columnName: string) => {
|
|
5
14
|
switch (column.dataType) {
|
|
6
15
|
case 'date': {
|
|
7
16
|
const formatted = new Date(value);
|
|
8
|
-
if (Number.isNaN(formatted.getTime()))
|
|
17
|
+
if (Number.isNaN(formatted.getTime())) {
|
|
9
18
|
throw new GraphQLError(`Field '${columnName}' is not a valid date!`);
|
|
19
|
+
}
|
|
10
20
|
|
|
11
21
|
return formatted;
|
|
12
22
|
}
|
|
@@ -20,7 +30,9 @@ export const remapFromGraphQLCore = (value: any, column: Column, columnName: str
|
|
|
20
30
|
}
|
|
21
31
|
|
|
22
32
|
case 'json': {
|
|
23
|
-
if (column.columnType === 'PgGeometryObject')
|
|
33
|
+
if (column.columnType === 'PgGeometryObject') {
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
24
36
|
|
|
25
37
|
try {
|
|
26
38
|
return JSON.parse(value);
|
|
@@ -48,7 +60,7 @@ export const remapFromGraphQLCore = (value: any, column: Column, columnName: str
|
|
|
48
60
|
case 'bigint': {
|
|
49
61
|
try {
|
|
50
62
|
return BigInt(value);
|
|
51
|
-
} catch (
|
|
63
|
+
} catch (_error) {
|
|
52
64
|
throw new GraphQLError(`Field '${columnName}' is not a BigInt!`);
|
|
53
65
|
}
|
|
54
66
|
}
|
|
@@ -60,6 +72,7 @@ export const remapFromGraphQLCore = (value: any, column: Column, columnName: str
|
|
|
60
72
|
};
|
|
61
73
|
|
|
62
74
|
export const remapFromGraphQLSingleInput = (
|
|
75
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
63
76
|
queryInput: Record<string, any>,
|
|
64
77
|
table: TableRelationalConfig,
|
|
65
78
|
) => {
|
|
@@ -68,7 +81,9 @@ export const remapFromGraphQLSingleInput = (
|
|
|
68
81
|
delete queryInput[key];
|
|
69
82
|
} else {
|
|
70
83
|
const column = table.columns[key];
|
|
71
|
-
if (!column)
|
|
84
|
+
if (!column) {
|
|
85
|
+
throw new GraphQLError(`Unknown column: ${key}`);
|
|
86
|
+
}
|
|
72
87
|
|
|
73
88
|
if (value === null && column.notNull) {
|
|
74
89
|
delete queryInput[key];
|
|
@@ -83,10 +98,13 @@ export const remapFromGraphQLSingleInput = (
|
|
|
83
98
|
};
|
|
84
99
|
|
|
85
100
|
export const remapFromGraphQLArrayInput = (
|
|
101
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
86
102
|
queryInput: Record<string, any>[],
|
|
87
103
|
table: TableRelationalConfig,
|
|
88
104
|
) => {
|
|
89
|
-
for (const entry of queryInput)
|
|
105
|
+
for (const entry of queryInput) {
|
|
106
|
+
remapFromGraphQLSingleInput(entry, table);
|
|
107
|
+
}
|
|
90
108
|
|
|
91
109
|
return queryInput;
|
|
92
110
|
};
|
|
@@ -100,6 +118,7 @@ export type OrderByArgs<TTable extends TableRelationalConfig> = {
|
|
|
100
118
|
|
|
101
119
|
export const extractOrderBy = <
|
|
102
120
|
TTable extends TableRelationalConfig,
|
|
121
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
103
122
|
TArgs extends OrderByArgs<any> = OrderByArgs<TTable>,
|
|
104
123
|
>(
|
|
105
124
|
table: TTable,
|
|
@@ -110,7 +129,9 @@ export const extractOrderBy = <
|
|
|
110
129
|
for (const [column, config] of Object.entries(orderArgs).sort(
|
|
111
130
|
(a, b) => (b[1]?.priority ?? 0) - (a[1]?.priority ?? 0),
|
|
112
131
|
)) {
|
|
113
|
-
if (!config)
|
|
132
|
+
if (!config) {
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
114
135
|
const { direction } = config;
|
|
115
136
|
|
|
116
137
|
res.push(direction === 'asc' ? asc(table.columns[column]!) : desc(table.columns[column]!));
|
|
@@ -126,7 +147,10 @@ export const extractFiltersColumn = <TColumn extends Column>(
|
|
|
126
147
|
columnName: string,
|
|
127
148
|
operators: Record<string, unknown> & { OR?: Record<string, unknown>[] },
|
|
128
149
|
): SQL | undefined => {
|
|
129
|
-
if (!operators.OR?.length)
|
|
150
|
+
if (!operators.OR?.length) {
|
|
151
|
+
// biome-ignore lint/performance/noDelete: <explanation>
|
|
152
|
+
delete operators.OR;
|
|
153
|
+
}
|
|
130
154
|
|
|
131
155
|
const entries = Object.entries(operators);
|
|
132
156
|
|
|
@@ -142,6 +166,7 @@ export const extractFiltersColumn = <TColumn extends Column>(
|
|
|
142
166
|
for (const variant of operators.OR) {
|
|
143
167
|
const extracted = extractFiltersColumn(column, columnName, variant);
|
|
144
168
|
|
|
169
|
+
// biome-ignore lint/style/useBlockStatements: <explanation>
|
|
145
170
|
if (extracted) variants.push(extracted);
|
|
146
171
|
}
|
|
147
172
|
|
|
@@ -150,6 +175,7 @@ export const extractFiltersColumn = <TColumn extends Column>(
|
|
|
150
175
|
|
|
151
176
|
const variants = [] as SQL[];
|
|
152
177
|
for (const [operatorName, operatorValue] of entries) {
|
|
178
|
+
// biome-ignore lint/style/useBlockStatements: <explanation>
|
|
153
179
|
if (operatorValue === null || operatorValue === false) continue;
|
|
154
180
|
|
|
155
181
|
switch (operatorName) {
|
|
@@ -158,11 +184,12 @@ export const extractFiltersColumn = <TColumn extends Column>(
|
|
|
158
184
|
case 'gt':
|
|
159
185
|
case 'gte':
|
|
160
186
|
case 'lt':
|
|
161
|
-
case 'lte':
|
|
187
|
+
case 'lte': {
|
|
162
188
|
const singleValue = remapFromGraphQLCore(operatorValue, column, columnName);
|
|
163
189
|
variants.push(ops[operatorName](column, singleValue));
|
|
164
190
|
|
|
165
191
|
break;
|
|
192
|
+
}
|
|
166
193
|
case 'like':
|
|
167
194
|
case 'notLike':
|
|
168
195
|
case 'ilike':
|
|
@@ -170,18 +197,21 @@ export const extractFiltersColumn = <TColumn extends Column>(
|
|
|
170
197
|
variants.push(ops[operatorName](column, operatorValue as string));
|
|
171
198
|
break;
|
|
172
199
|
case 'inArray':
|
|
173
|
-
case 'notInArray':
|
|
200
|
+
case 'notInArray': {
|
|
201
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
174
202
|
if (!(operatorValue as any[]).length) {
|
|
175
203
|
throw new GraphQLError(
|
|
176
204
|
`WHERE ${columnName}: Unable to use operator ${operatorName} with an empty array!`,
|
|
177
205
|
);
|
|
178
206
|
}
|
|
207
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
179
208
|
const arrayValue = (operatorValue as any[]).map((val) =>
|
|
180
209
|
remapFromGraphQLCore(val, column, columnName),
|
|
181
210
|
);
|
|
182
211
|
|
|
183
212
|
variants.push(ops[operatorName](column, arrayValue));
|
|
184
213
|
break;
|
|
214
|
+
}
|
|
185
215
|
case 'isNull':
|
|
186
216
|
case 'isNotNull':
|
|
187
217
|
variants.push(ops[operatorName](column));
|
|
@@ -201,10 +231,15 @@ export const extractFilters = <TTable extends TableRelationalConfig>(
|
|
|
201
231
|
OR?: Record<string, unknown>[];
|
|
202
232
|
},
|
|
203
233
|
): SQL | undefined => {
|
|
204
|
-
if (!filters.OR?.length)
|
|
234
|
+
if (!filters.OR?.length) {
|
|
235
|
+
// biome-ignore lint/performance/noDelete: <explanation>
|
|
236
|
+
delete filters.OR;
|
|
237
|
+
}
|
|
205
238
|
|
|
206
239
|
const entries = Object.entries(filters);
|
|
207
|
-
if (!entries.length)
|
|
240
|
+
if (!entries.length) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
208
243
|
|
|
209
244
|
if (filters.OR) {
|
|
210
245
|
if (entries.length > 1) {
|
|
@@ -217,7 +252,9 @@ export const extractFilters = <TTable extends TableRelationalConfig>(
|
|
|
217
252
|
|
|
218
253
|
for (const variant of filters.OR) {
|
|
219
254
|
const extracted = extractFilters(table, tableName, variant);
|
|
220
|
-
if (extracted)
|
|
255
|
+
if (extracted) {
|
|
256
|
+
variants.push(extracted);
|
|
257
|
+
}
|
|
221
258
|
}
|
|
222
259
|
|
|
223
260
|
return variants.length ? (variants.length > 1 ? ops.or(...variants) : variants[0]) : undefined;
|
|
@@ -225,7 +262,9 @@ export const extractFilters = <TTable extends TableRelationalConfig>(
|
|
|
225
262
|
|
|
226
263
|
const variants = [] as SQL[];
|
|
227
264
|
for (const [columnName, operators] of entries) {
|
|
228
|
-
if (operators === null)
|
|
265
|
+
if (operators === null) {
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
229
268
|
|
|
230
269
|
const column = table.columns[columnName]!;
|
|
231
270
|
variants.push(extractFiltersColumn(column, columnName, operators as Record<string, unknown>)!);
|