@pothos/plugin-drizzle 0.19.2 → 0.20.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.
- package/CHANGELOG.md +16 -0
- package/README.md +50 -0
- package/dts/connection-helpers.d.ts.map +1 -1
- package/dts/global-types.d.ts +10 -1
- package/dts/global-types.d.ts.map +1 -1
- package/dts/index.d.ts +2 -1
- package/dts/index.d.ts.map +1 -1
- package/dts/model-loader.d.ts.map +1 -1
- package/dts/utils/map-query.d.ts +16 -4
- package/dts/utils/map-query.d.ts.map +1 -1
- package/esm/connection-helpers.d.ts.map +1 -1
- package/esm/connection-helpers.js +2 -1
- package/esm/connection-helpers.js.map +1 -1
- package/esm/field-builder.js +10 -1
- package/esm/field-builder.js.map +1 -1
- package/esm/global-types.d.ts +10 -1
- package/esm/global-types.d.ts.map +1 -1
- package/esm/global-types.js.map +1 -1
- package/esm/index.d.ts +2 -1
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +2 -1
- package/esm/index.js.map +1 -1
- package/esm/model-loader.d.ts.map +1 -1
- package/esm/model-loader.js +1 -1
- package/esm/model-loader.js.map +1 -1
- package/esm/utils/map-query.d.ts +16 -4
- package/esm/utils/map-query.d.ts.map +1 -1
- package/esm/utils/map-query.js +11 -13
- package/esm/utils/map-query.js.map +1 -1
- package/lib/connection-helpers.js +2 -1
- package/lib/connection-helpers.js.map +1 -1
- package/lib/field-builder.js +9 -0
- package/lib/field-builder.js.map +1 -1
- package/lib/index.js +7 -0
- package/lib/index.js.map +1 -1
- package/lib/model-loader.js +1 -1
- package/lib/model-loader.js.map +1 -1
- package/lib/utils/map-query.js +11 -13
- package/lib/utils/map-query.js.map +1 -1
- package/package.json +4 -4
- package/src/connection-helpers.ts +2 -1
- package/src/field-builder.ts +12 -1
- package/src/global-types.ts +30 -0
- package/src/index.ts +7 -1
- package/src/model-loader.ts +4 -0
- package/src/utils/map-query.ts +42 -21
package/src/utils/map-query.ts
CHANGED
|
@@ -1,39 +1,60 @@
|
|
|
1
|
-
import { isThenable } from '@pothos/core';
|
|
1
|
+
import { isThenable, type MaybePromise } from '@pothos/core';
|
|
2
2
|
import { type PathSegment, Plan } from '@pothos/selection-mapper';
|
|
3
|
+
import type { DBQueryConfig } from 'drizzle-orm';
|
|
3
4
|
import type { GraphQLResolveInfo } from 'graphql';
|
|
4
5
|
import { type DrizzlePlan, drizzleAdapter } from './adapter.js';
|
|
6
|
+
import { checkAwaitSelections } from './await-selections.js';
|
|
5
7
|
import type { PothosDrizzleSchemaConfig } from './config.js';
|
|
6
8
|
import type { SelectionMap } from './selections.js';
|
|
7
9
|
|
|
8
|
-
export interface QueryFromInfoOptions<
|
|
10
|
+
export interface QueryFromInfoOptions<Await extends boolean = false> {
|
|
9
11
|
config: PothosDrizzleSchemaConfig;
|
|
10
12
|
context: object;
|
|
11
13
|
info: GraphQLResolveInfo;
|
|
12
14
|
typeName?: string;
|
|
13
|
-
select?: T;
|
|
14
15
|
path?: PathSegment[];
|
|
15
16
|
paths?: PathSegment[][];
|
|
17
|
+
/** Whether the caller will await the query. Async selections otherwise throw. */
|
|
18
|
+
awaitSelections?: Await;
|
|
16
19
|
}
|
|
17
20
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}: QueryFromInfoOptions<T>): T {
|
|
23
|
-
const plan = Plan.fromInfo(drizzleAdapter(config), {
|
|
24
|
-
...options,
|
|
25
|
-
// A `select` without `columns` merges as "no columns yet", not "every column".
|
|
26
|
-
initial: select ? { columns: {}, ...select } : undefined,
|
|
27
|
-
});
|
|
21
|
+
/** Only explicitly selected columns are guaranteed; planning metadata is not part of the query. */
|
|
22
|
+
export type QueryFromInfoResult<T> = Omit<T, keyof QueryFromInfoOptions<boolean> | 'columns'> & {
|
|
23
|
+
columns: T extends { columns: infer Columns extends {} } ? Columns : {};
|
|
24
|
+
};
|
|
28
25
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
/** A boolean async option must account for promises, just like Prisma's helper. */
|
|
27
|
+
export type QueryFromInfoReturn<T, Await extends boolean> = [Await] extends [false]
|
|
28
|
+
? QueryFromInfoResult<T>
|
|
29
|
+
: MaybePromise<QueryFromInfoResult<T>>;
|
|
30
|
+
|
|
31
|
+
/** Merge query options with the GraphQL selection and return a query ready for Drizzle. */
|
|
32
|
+
export function queryFromInfo<const T extends object, Await extends boolean = false>({
|
|
33
|
+
config,
|
|
34
|
+
context,
|
|
35
|
+
info,
|
|
36
|
+
typeName,
|
|
37
|
+
path,
|
|
38
|
+
paths,
|
|
39
|
+
awaitSelections,
|
|
40
|
+
...selection
|
|
41
|
+
}: QueryFromInfoOptions<Await> &
|
|
42
|
+
T &
|
|
43
|
+
Record<
|
|
44
|
+
Exclude<keyof T, keyof QueryFromInfoOptions<Await> | keyof DBQueryConfig<'many'>>,
|
|
45
|
+
never
|
|
46
|
+
>): QueryFromInfoReturn<T, Await> {
|
|
47
|
+
const plan = planFromInfo({ config, context, info, typeName, path, paths });
|
|
48
|
+
const query = isThenable(plan)
|
|
49
|
+
? plan.then((settled) => queryFromPlan(settled as DrizzlePlan | undefined, selection))
|
|
50
|
+
: queryFromPlan(plan, selection);
|
|
33
51
|
|
|
34
|
-
return (
|
|
35
|
-
|
|
36
|
-
|
|
52
|
+
return checkAwaitSelections(
|
|
53
|
+
query,
|
|
54
|
+
awaitSelections,
|
|
55
|
+
'queryFromInfo',
|
|
56
|
+
`${info.parentType.name}.${info.fieldName}`,
|
|
57
|
+
) as QueryFromInfoReturn<T, Await>;
|
|
37
58
|
}
|
|
38
59
|
|
|
39
60
|
/**
|
|
@@ -45,7 +66,7 @@ export function queryFromInfo<T extends SelectionMap>({
|
|
|
45
66
|
export function planFromInfo({
|
|
46
67
|
config,
|
|
47
68
|
...options
|
|
48
|
-
}: Omit<QueryFromInfoOptions
|
|
69
|
+
}: Omit<QueryFromInfoOptions, 'awaitSelections'>): DrizzlePlan | undefined {
|
|
49
70
|
return Plan.fromInfo(drizzleAdapter(config), options);
|
|
50
71
|
}
|
|
51
72
|
|