@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.
Files changed (46) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +50 -0
  3. package/dts/connection-helpers.d.ts.map +1 -1
  4. package/dts/global-types.d.ts +10 -1
  5. package/dts/global-types.d.ts.map +1 -1
  6. package/dts/index.d.ts +2 -1
  7. package/dts/index.d.ts.map +1 -1
  8. package/dts/model-loader.d.ts.map +1 -1
  9. package/dts/utils/map-query.d.ts +16 -4
  10. package/dts/utils/map-query.d.ts.map +1 -1
  11. package/esm/connection-helpers.d.ts.map +1 -1
  12. package/esm/connection-helpers.js +2 -1
  13. package/esm/connection-helpers.js.map +1 -1
  14. package/esm/field-builder.js +10 -1
  15. package/esm/field-builder.js.map +1 -1
  16. package/esm/global-types.d.ts +10 -1
  17. package/esm/global-types.d.ts.map +1 -1
  18. package/esm/global-types.js.map +1 -1
  19. package/esm/index.d.ts +2 -1
  20. package/esm/index.d.ts.map +1 -1
  21. package/esm/index.js +2 -1
  22. package/esm/index.js.map +1 -1
  23. package/esm/model-loader.d.ts.map +1 -1
  24. package/esm/model-loader.js +1 -1
  25. package/esm/model-loader.js.map +1 -1
  26. package/esm/utils/map-query.d.ts +16 -4
  27. package/esm/utils/map-query.d.ts.map +1 -1
  28. package/esm/utils/map-query.js +11 -13
  29. package/esm/utils/map-query.js.map +1 -1
  30. package/lib/connection-helpers.js +2 -1
  31. package/lib/connection-helpers.js.map +1 -1
  32. package/lib/field-builder.js +9 -0
  33. package/lib/field-builder.js.map +1 -1
  34. package/lib/index.js +7 -0
  35. package/lib/index.js.map +1 -1
  36. package/lib/model-loader.js +1 -1
  37. package/lib/model-loader.js.map +1 -1
  38. package/lib/utils/map-query.js +11 -13
  39. package/lib/utils/map-query.js.map +1 -1
  40. package/package.json +4 -4
  41. package/src/connection-helpers.ts +2 -1
  42. package/src/field-builder.ts +12 -1
  43. package/src/global-types.ts +30 -0
  44. package/src/index.ts +7 -1
  45. package/src/model-loader.ts +4 -0
  46. package/src/utils/map-query.ts +42 -21
@@ -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<T extends SelectionMap> {
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
- export function queryFromInfo<T extends SelectionMap>({
19
- config,
20
- select,
21
- ...options
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
- if (!plan) {
30
- // Nothing is planned under the paths: the caller gets its own selection back untouched.
31
- return (select ?? {}) as T;
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
- isThenable(plan) ? plan.then((settled) => (settled as DrizzlePlan).query()) : plan.query()
36
- ) as T;
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<SelectionMap>, 'select'>): DrizzlePlan | undefined {
69
+ }: Omit<QueryFromInfoOptions, 'awaitSelections'>): DrizzlePlan | undefined {
49
70
  return Plan.fromInfo(drizzleAdapter(config), options);
50
71
  }
51
72