@pothos/plugin-prisma 0.18.0 → 0.19.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 (163) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +403 -308
  3. package/esm/field-builder.js +3 -3
  4. package/esm/field-builder.js.map +1 -1
  5. package/esm/generator.js +4 -0
  6. package/esm/generator.js.map +1 -1
  7. package/esm/global-types.d.ts +23 -5
  8. package/esm/global-types.d.ts.map +1 -1
  9. package/esm/index.d.ts +4 -1
  10. package/esm/index.d.ts.map +1 -1
  11. package/esm/index.js +46 -1
  12. package/esm/index.js.map +1 -1
  13. package/esm/model-loader.d.ts +5 -6
  14. package/esm/model-loader.d.ts.map +1 -1
  15. package/esm/model-loader.js +12 -69
  16. package/esm/model-loader.js.map +1 -1
  17. package/esm/prisma-field-builder.d.ts +25 -6
  18. package/esm/prisma-field-builder.d.ts.map +1 -1
  19. package/esm/prisma-field-builder.js +83 -116
  20. package/esm/prisma-field-builder.js.map +1 -1
  21. package/esm/schema-builder.js +14 -4
  22. package/esm/schema-builder.js.map +1 -1
  23. package/esm/types.d.ts +73 -77
  24. package/esm/types.d.ts.map +1 -1
  25. package/esm/types.js +1 -0
  26. package/esm/types.js.map +1 -1
  27. package/esm/{cursors.d.ts → util/cursors.d.ts} +1 -1
  28. package/esm/util/cursors.d.ts.map +1 -0
  29. package/esm/{cursors.js → util/cursors.js} +0 -0
  30. package/esm/util/cursors.js.map +1 -0
  31. package/{lib/refs.d.ts → esm/util/datamodel.d.ts} +4 -8
  32. package/esm/util/datamodel.d.ts.map +1 -0
  33. package/esm/{refs.js → util/datamodel.js} +2 -25
  34. package/esm/util/datamodel.js.map +1 -0
  35. package/esm/util/deep-equal.d.ts +2 -0
  36. package/esm/util/deep-equal.d.ts.map +1 -0
  37. package/esm/util/deep-equal.js +39 -0
  38. package/esm/util/deep-equal.js.map +1 -0
  39. package/esm/util/loader-map.d.ts +6 -0
  40. package/esm/util/loader-map.d.ts.map +1 -0
  41. package/esm/{loader-map.js → util/loader-map.js} +10 -12
  42. package/esm/util/loader-map.js.map +1 -0
  43. package/esm/util/map-query.d.ts +6 -0
  44. package/esm/util/map-query.d.ts.map +1 -0
  45. package/esm/util/map-query.js +169 -0
  46. package/esm/util/map-query.js.map +1 -0
  47. package/esm/util/relation-map.d.ts +9 -0
  48. package/esm/util/relation-map.d.ts.map +1 -0
  49. package/esm/util/relation-map.js +20 -0
  50. package/esm/util/relation-map.js.map +1 -0
  51. package/esm/util/selections.d.ts +20 -0
  52. package/esm/util/selections.d.ts.map +1 -0
  53. package/esm/util/selections.js +139 -0
  54. package/esm/util/selections.js.map +1 -0
  55. package/lib/field-builder.js +9 -9
  56. package/lib/field-builder.js.map +1 -1
  57. package/lib/generator.js +4 -0
  58. package/lib/generator.js.map +1 -1
  59. package/lib/global-types.d.ts +23 -5
  60. package/lib/global-types.d.ts.map +1 -1
  61. package/lib/index.d.ts +4 -1
  62. package/lib/index.d.ts.map +1 -1
  63. package/lib/index.js +45 -0
  64. package/lib/index.js.map +1 -1
  65. package/lib/model-loader.d.ts +5 -6
  66. package/lib/model-loader.d.ts.map +1 -1
  67. package/lib/model-loader.js +13 -70
  68. package/lib/model-loader.js.map +1 -1
  69. package/lib/prisma-field-builder.d.ts +25 -6
  70. package/lib/prisma-field-builder.d.ts.map +1 -1
  71. package/lib/prisma-field-builder.js +89 -122
  72. package/lib/prisma-field-builder.js.map +1 -1
  73. package/lib/schema-builder.js +17 -7
  74. package/lib/schema-builder.js.map +1 -1
  75. package/lib/types.d.ts +73 -77
  76. package/lib/types.d.ts.map +1 -1
  77. package/lib/types.js +2 -0
  78. package/lib/types.js.map +1 -1
  79. package/lib/{cursors.d.ts → util/cursors.d.ts} +1 -1
  80. package/lib/util/cursors.d.ts.map +1 -0
  81. package/lib/{cursors.js → util/cursors.js} +0 -0
  82. package/lib/util/cursors.js.map +1 -0
  83. package/{esm/refs.d.ts → lib/util/datamodel.d.ts} +4 -8
  84. package/lib/util/datamodel.d.ts.map +1 -0
  85. package/lib/{refs.js → util/datamodel.js} +3 -29
  86. package/lib/util/datamodel.js.map +1 -0
  87. package/lib/util/deep-equal.d.ts +2 -0
  88. package/lib/util/deep-equal.d.ts.map +1 -0
  89. package/lib/util/deep-equal.js +43 -0
  90. package/lib/util/deep-equal.js.map +1 -0
  91. package/lib/util/loader-map.d.ts +6 -0
  92. package/lib/util/loader-map.d.ts.map +1 -0
  93. package/lib/{loader-map.js → util/loader-map.js} +10 -12
  94. package/lib/util/loader-map.js.map +1 -0
  95. package/lib/util/map-query.d.ts +6 -0
  96. package/lib/util/map-query.d.ts.map +1 -0
  97. package/lib/util/map-query.js +175 -0
  98. package/lib/util/map-query.js.map +1 -0
  99. package/lib/util/relation-map.d.ts +9 -0
  100. package/lib/util/relation-map.d.ts.map +1 -0
  101. package/lib/util/relation-map.js +24 -0
  102. package/lib/util/relation-map.js.map +1 -0
  103. package/lib/util/selections.d.ts +20 -0
  104. package/lib/util/selections.d.ts.map +1 -0
  105. package/lib/util/selections.js +148 -0
  106. package/lib/util/selections.js.map +1 -0
  107. package/package.json +5 -5
  108. package/src/field-builder.ts +3 -3
  109. package/src/generator.ts +18 -0
  110. package/src/global-types.ts +59 -12
  111. package/src/index.ts +75 -1
  112. package/src/model-loader.ts +19 -92
  113. package/src/prisma-field-builder.ts +194 -152
  114. package/src/schema-builder.ts +28 -7
  115. package/src/types.ts +138 -102
  116. package/src/{cursors.ts → util/cursors.ts} +1 -1
  117. package/src/{refs.ts → util/datamodel.ts} +3 -44
  118. package/src/util/deep-equal.ts +51 -0
  119. package/src/{loader-map.ts → util/loader-map.ts} +13 -13
  120. package/src/util/map-query.ts +327 -0
  121. package/src/util/relation-map.ts +36 -0
  122. package/src/util/selections.ts +192 -0
  123. package/esm/cursors.d.ts.map +0 -1
  124. package/esm/cursors.js.map +0 -1
  125. package/esm/loader-map.d.ts +0 -6
  126. package/esm/loader-map.d.ts.map +0 -1
  127. package/esm/loader-map.js.map +0 -1
  128. package/esm/refs.d.ts.map +0 -1
  129. package/esm/refs.js.map +0 -1
  130. package/esm/util/index.d.ts +0 -5
  131. package/esm/util/index.d.ts.map +0 -1
  132. package/esm/util/index.js +0 -16
  133. package/esm/util/index.js.map +0 -1
  134. package/esm/util/map-includes.d.ts +0 -6
  135. package/esm/util/map-includes.d.ts.map +0 -1
  136. package/esm/util/map-includes.js +0 -184
  137. package/esm/util/map-includes.js.map +0 -1
  138. package/esm/util/merge-includes.d.ts +0 -3
  139. package/esm/util/merge-includes.d.ts.map +0 -1
  140. package/esm/util/merge-includes.js +0 -91
  141. package/esm/util/merge-includes.js.map +0 -1
  142. package/lib/cursors.d.ts.map +0 -1
  143. package/lib/cursors.js.map +0 -1
  144. package/lib/loader-map.d.ts +0 -6
  145. package/lib/loader-map.d.ts.map +0 -1
  146. package/lib/loader-map.js.map +0 -1
  147. package/lib/refs.d.ts.map +0 -1
  148. package/lib/refs.js.map +0 -1
  149. package/lib/util/index.d.ts +0 -5
  150. package/lib/util/index.d.ts.map +0 -1
  151. package/lib/util/index.js +0 -30
  152. package/lib/util/index.js.map +0 -1
  153. package/lib/util/map-includes.d.ts +0 -6
  154. package/lib/util/map-includes.d.ts.map +0 -1
  155. package/lib/util/map-includes.js +0 -189
  156. package/lib/util/map-includes.js.map +0 -1
  157. package/lib/util/merge-includes.d.ts +0 -3
  158. package/lib/util/merge-includes.d.ts.map +0 -1
  159. package/lib/util/merge-includes.js +0 -96
  160. package/lib/util/merge-includes.js.map +0 -1
  161. package/src/util/index.ts +0 -26
  162. package/src/util/map-includes.ts +0 -328
  163. package/src/util/merge-includes.ts +0 -121
package/src/types.ts CHANGED
@@ -10,6 +10,7 @@ import {
10
10
  InterfaceParam,
11
11
  ListResolveValue,
12
12
  MaybePromise,
13
+ Normalize,
13
14
  ObjectRef,
14
15
  OutputShape,
15
16
  OutputType,
@@ -26,52 +27,17 @@ export interface PrismaDelegate {
26
27
  findUnique: (...args: any[]) => Promise<unknown>;
27
28
  }
28
29
 
29
- type RelationKeys<T> = {
30
- [K in keyof T]: T[K] extends (args: {}) => {
31
- then: (cb: (result: unknown) => unknown) => unknown;
32
- }
33
- ? K
34
- : never;
35
- }[keyof T];
36
-
37
- export type ModelTypes<Model extends {}> = Model extends {
38
- findUnique: (
39
- options: infer UniqueOptions & {
40
- where?: infer Where | null | undefined;
41
- select?: infer Select | null | undefined;
42
- } & (
43
- | {
44
- include?: infer Include | null | undefined;
45
- }
46
- | {}
47
- ),
48
- ) => infer Chain & {
49
- then: (cb: (result: infer Shape | null) => unknown) => unknown;
50
- };
51
- }
52
- ? PrismaModelTypes & {
53
- Shape: Shape;
54
- Include: unknown extends Include ? never : Include;
55
- Where: Where;
56
- Fields: keyof Select;
57
- ListRelation: ListRelationFields<Include> & string;
58
- Relations: {
59
- [RelationName in RelationKeys<Chain>]: Chain[RelationName] extends (args: {}) => {
60
- then: (cb: (result: infer Relation) => unknown) => unknown;
61
- }
62
- ? { Shape: Relation; Types: PrismaModelTypes }
63
- : never;
64
- };
65
- }
66
- : never;
30
+ export const prismaModelName = Symbol.for('Pothos.prismaModelName');
67
31
 
68
32
  export interface PrismaModelTypes {
69
33
  Name: string;
70
34
  Shape: {};
71
35
  Include: unknown;
36
+ Select: unknown;
72
37
  Where: {};
73
38
  Fields: string;
74
39
  ListRelations: string;
40
+ RelationName: string;
75
41
  Relations: Record<
76
42
  string,
77
43
  {
@@ -81,52 +47,86 @@ export interface PrismaModelTypes {
81
47
  >;
82
48
  }
83
49
 
84
- export type ListRelationFields<T> = {
85
- [K in keyof T]: T[K] extends infer Option
86
- ? Option extends { orderBy?: unknown }
87
- ? K
50
+ type ExtractModel<Types extends SchemaTypes, ParentShape> = ParentShape extends {
51
+ [prismaModelName]?: infer Name;
52
+ }
53
+ ? Types['PrismaTypes'][Name & keyof Types['PrismaTypes']] extends infer Model
54
+ ? Model extends PrismaModelTypes
55
+ ? Model
88
56
  : never
89
- : never;
90
- }[keyof T];
57
+ : never
58
+ : never;
91
59
 
92
- export type PrismaObjectFieldsShape<
60
+ export type PrismaObjectFieldOptions<
61
+ Types extends SchemaTypes,
62
+ ParentShape,
63
+ Type extends TypeParam<Types>,
64
+ Nullable extends FieldNullability<Type>,
65
+ Args extends InputFieldMap,
66
+ Select,
67
+ ResolveReturnShape,
68
+ > = PothosSchemaTypes.ObjectFieldOptions<
69
+ Types,
70
+ unknown extends Select
71
+ ? ParentShape
72
+ : ParentShape & ShapeFromSelection<ExtractModel<Types, ParentShape>, { select: Select }>,
73
+ Type,
74
+ Nullable,
75
+ Args,
76
+ ResolveReturnShape
77
+ > & {
78
+ select?: ExtractModel<Types, ParentShape>['Select'] & Select;
79
+ };
80
+
81
+ type PrismaObjectFieldsShape<
93
82
  Types extends SchemaTypes,
94
83
  Model extends PrismaModelTypes,
95
84
  NeedsResolve extends boolean,
96
85
  Shape extends object,
97
- > = (t: PrismaObjectFieldBuilder<Types, Model, NeedsResolve, Shape>) => FieldMap;
98
-
99
- export type ShapeFromInclude<Model extends PrismaModelTypes, Include> = {} extends Include
100
- ? Model['Shape']
101
- : {
102
- [K in keyof Include &
103
- keyof Model['Relations']]: Model['Relations'][K]['Shape'] extends infer RelationShape
104
- ? RelationShape extends (infer ItemShape)[]
105
- ? (ItemShape &
106
- (Include[K] extends { include?: infer NestedInclude & object }
107
- ? ShapeFromInclude<Model['Relations'][K]['Types'], NestedInclude>
108
- : {}))[]
109
- : RelationShape &
110
- (
111
- | (Include[K] extends { include?: infer NestedInclude & object }
112
- ? ShapeFromInclude<Model['Relations'][K]['Types'], NestedInclude>
113
- : {})
114
- | null
115
- )
116
- : never;
117
- };
86
+ Select,
87
+ > = Model['Select'] extends Select
88
+ ? (t: PrismaObjectFieldBuilder<Types, Model, NeedsResolve, Shape>) => FieldMap
89
+ : (t: PrismaSelectionFieldBuilder<Types, Model, Shape>) => FieldMap;
118
90
 
119
- export type ShapeWithInclude<
91
+ type PrismaSelectionFieldBuilder<
92
+ Types extends SchemaTypes,
120
93
  Model extends PrismaModelTypes,
121
- Include extends Model['Include'],
122
- > = Model['Shape'] & ([Include] extends [never] ? {} : ShapeFromInclude<Model, Include>);
94
+ Shape extends object,
95
+ > = PrismaObjectFieldBuilder<Types, Model, false, Shape>;
96
+
97
+ interface BaseSelection {
98
+ include?: unknown;
99
+ select?: unknown;
100
+ }
101
+
102
+ export type SelectedKeys<T> = { [K in keyof T]: T[K] extends false ? never : K }[keyof T];
103
+
104
+ export type ShapeFromSelection<Model extends PrismaModelTypes, Selection> = Normalize<
105
+ Selection extends BaseSelection
106
+ ? unknown extends Selection['select']
107
+ ? Model['Shape'] & RelationShapeFromInclude<Model, Selection['include']>
108
+ : Pick<Model['Shape'], SelectedKeys<Selection['select']>> &
109
+ RelationShapeFromInclude<Model, Selection['select']>
110
+ : Model['Shape']
111
+ >;
112
+
113
+ type RelationShapeFromInclude<Model extends PrismaModelTypes, Include> = Normalize<{
114
+ [K in SelectedKeys<Include> as K extends Model['RelationName']
115
+ ? K
116
+ : never]: K extends keyof Model['Relations']
117
+ ? Model['Relations'][K]['Shape'] extends unknown[]
118
+ ? ShapeFromSelection<Model['Relations'][K]['Types'], Include[K]>[]
119
+ : ShapeFromSelection<Model['Relations'][K]['Types'], Include[K]>
120
+ : unknown;
121
+ }>;
123
122
 
124
123
  export type PrismaObjectTypeOptions<
125
124
  Types extends SchemaTypes,
126
125
  Model extends PrismaModelTypes,
127
126
  Interfaces extends InterfaceParam<Types>[],
128
127
  FindUnique,
129
- Include extends Model['Include'],
128
+ Include,
129
+ Select,
130
130
  Shape extends object,
131
131
  > = NameOrVariant &
132
132
  Omit<
@@ -134,13 +134,28 @@ export type PrismaObjectTypeOptions<
134
134
  | PothosSchemaTypes.ObjectTypeWithInterfaceOptions<Types, Shape, Interfaces>,
135
135
  'fields'
136
136
  > & {
137
- include?: Include;
138
- fields?: PrismaObjectFieldsShape<Types, Model, FindUnique extends null ? true : false, Shape>;
139
- findUnique: FindUnique &
140
- (((parent: Shape, context: Types['Context']) => Model['Where']) | null);
141
- };
137
+ fields?: PrismaObjectFieldsShape<
138
+ Types,
139
+ Model,
140
+ FindUnique extends null ? true : false,
141
+ Shape & (FindUnique extends null ? {} : { [prismaModelName]?: Model['Name'] }),
142
+ Select
143
+ >;
144
+ } & (
145
+ | {
146
+ include?: Include & Model['Include'];
147
+ select?: never;
148
+ findUnique: FindUnique &
149
+ (((parent: Shape, context: Types['Context']) => Model['Where']) | null);
150
+ }
151
+ | {
152
+ select: Model['Select'] & Select;
153
+ include?: never;
154
+ findUnique: (parent: Shape, context: Types['Context']) => Model['Where'];
155
+ }
156
+ );
142
157
 
143
- export type NameOrVariant =
158
+ type NameOrVariant =
144
159
  | {
145
160
  name?: never;
146
161
  variant?: string;
@@ -154,7 +169,8 @@ export type PrismaNodeOptions<
154
169
  Types extends SchemaTypes,
155
170
  Model extends PrismaModelTypes,
156
171
  Interfaces extends InterfaceParam<Types>[],
157
- Include extends Model['Include'],
172
+ Include,
173
+ Select,
158
174
  Shape extends object,
159
175
  > = NameOrVariant &
160
176
  Omit<
@@ -162,7 +178,6 @@ export type PrismaNodeOptions<
162
178
  | PothosSchemaTypes.ObjectTypeWithInterfaceOptions<Types, Shape, Interfaces>,
163
179
  'fields' | 'isTypeOf'
164
180
  > & {
165
- include?: Include;
166
181
  id: Omit<
167
182
  FieldOptionsFromKind<
168
183
  Types,
@@ -178,11 +193,26 @@ export type PrismaNodeOptions<
178
193
  > & {
179
194
  resolve: (parent: Shape, context: Types['Context']) => MaybePromise<OutputShape<Types, 'ID'>>;
180
195
  };
181
- fields?: PrismaObjectFieldsShape<Types, Model, false, Shape>;
196
+ fields?: PrismaObjectFieldsShape<
197
+ Types,
198
+ Model,
199
+ false,
200
+ Shape & { [prismaModelName]?: Model['Name'] },
201
+ Select
202
+ >;
182
203
  findUnique: (id: string, context: Types['Context']) => Model['Where'];
183
- };
204
+ } & (
205
+ | {
206
+ include?: Include & Model['Include'];
207
+ select?: never;
208
+ }
209
+ | {
210
+ select: Model['Select'] & Select;
211
+ include?: never;
212
+ }
213
+ );
184
214
 
185
- export type QueryForField<
215
+ type QueryForField<
186
216
  Types extends SchemaTypes,
187
217
  Args extends InputFieldMap,
188
218
  Include,
@@ -195,18 +225,19 @@ export type QueryForField<
195
225
  ) => Omit<Include, 'include' | 'select'>)
196
226
  : never;
197
227
 
198
- export type IncludeFromRelation<
228
+ type QueryFromRelation<
199
229
  Model extends PrismaModelTypes,
200
230
  Field extends keyof Model['Include'],
201
231
  > = Model['Include'][Field] extends infer Include
202
232
  ? Include extends {
203
- include?: infer T;
233
+ include?: infer I;
234
+ select?: infer S;
204
235
  }
205
- ? NonNullable<T>
236
+ ? { include?: NonNullable<I>; select?: NonNullable<S> }
206
237
  : never
207
238
  : never;
208
239
 
209
- export type CursorFromRelation<
240
+ type CursorFromRelation<
210
241
  Model extends PrismaModelTypes,
211
242
  Field extends Model['ListRelations'],
212
243
  > = Field extends keyof Model['Include']
@@ -217,7 +248,7 @@ export type CursorFromRelation<
217
248
  : never
218
249
  : never;
219
250
 
220
- export type RefForRelation<
251
+ type RefForRelation<
221
252
  Model extends PrismaModelTypes,
222
253
  Field extends keyof Model['Relations'],
223
254
  > = Model['Relations'][Field] extends unknown[]
@@ -247,7 +278,7 @@ export type RelatedFieldOptions<
247
278
  (NeedsResolve extends false
248
279
  ? {
249
280
  resolve?: (
250
- query: { include?: IncludeFromRelation<Model, Field & keyof Model['Include']> },
281
+ query: QueryFromRelation<Model, Field & keyof Model['Include']>,
251
282
  parent: Shape,
252
283
  args: InputShapeFromFields<Args>,
253
284
  context: Types['Context'],
@@ -258,7 +289,7 @@ export type RelatedFieldOptions<
258
289
  }
259
290
  : {
260
291
  resolve: (
261
- query: { include?: IncludeFromRelation<Model, Field & keyof Model['Include']> },
292
+ query: QueryFromRelation<Model, Field & keyof Model['Include']>,
262
293
  parent: Shape,
263
294
  args: InputShapeFromFields<Args>,
264
295
  context: Types['Context'],
@@ -472,31 +503,36 @@ export type RelatedConnectionOptions<
472
503
 
473
504
  export type WithBrand<T> = T & { [typeBrandKey]: string };
474
505
 
475
- export type IncludeMap = Record<string, Record<string, unknown> | boolean>;
506
+ export type IncludeMap = Record<string, SelectionMap | boolean>;
476
507
 
477
- export interface IncludeCounts {
478
- current: Record<string, boolean>;
479
- parent: Record<string, boolean>;
508
+ export interface SelectionMap {
509
+ select?: Record<string, SelectionMap | boolean>;
510
+ include?: Record<string, SelectionMap | boolean>;
511
+ where?: {};
480
512
  }
481
513
 
514
+ export type FieldSelection =
515
+ | Record<string, SelectionMap | boolean>
516
+ | ((
517
+ args: object,
518
+ context: object,
519
+ query: (
520
+ selection: SelectionMap | boolean | ((args: object, context: object) => SelectionMap),
521
+ ) => SelectionMap | boolean,
522
+ ) => SelectionMap);
523
+
482
524
  export type LoaderMappings = Record<
483
525
  string,
484
526
  {
485
527
  field: string;
486
- alias?: string;
487
528
  mappings: LoaderMappings;
488
529
  indirectPath: string[];
489
- }[]
530
+ }
490
531
  >;
491
532
 
492
- export interface SubFieldInclude {
493
- type?: string;
494
- name: string;
495
- }
496
-
497
- export interface IndirectLoadMap {
498
- subFields: SubFieldInclude[];
499
- path: string[];
533
+ export interface IndirectInclude {
534
+ getType: () => string;
535
+ path: { type?: string; name: string }[];
500
536
  }
501
537
 
502
538
  export type ShapeFromConnection<T> = T extends { shape: unknown } ? T['shape'] : never;
@@ -75,7 +75,7 @@ export function parseCompositeCursor(fields: string[]) {
75
75
  };
76
76
  }
77
77
 
78
- interface PrismaCursorConnectionQueryOptions {
78
+ export interface PrismaCursorConnectionQueryOptions {
79
79
  args: PothosSchemaTypes.DefaultConnectionArguments;
80
80
  defaultSize?: number;
81
81
  maxSize?: number;
@@ -1,9 +1,8 @@
1
1
  import { ObjectRef, SchemaTypes } from '@pothos/core';
2
- import { Prisma } from '../tests/client';
2
+ import { Prisma } from '../../tests/client';
3
+ import { PrismaObjectRef } from '../object-ref';
4
+ import { PrismaDelegate, PrismaModelTypes } from '../types';
3
5
  import { formatCursor, parseCompositeCursor, parseRawCursor } from './cursors';
4
- import { PrismaObjectRef } from './object-ref';
5
- import { PrismaDelegate } from './types';
6
- import { PrismaModelTypes } from '.';
7
6
 
8
7
  export const refMap = new WeakMap<object, Map<string, PrismaObjectRef<PrismaModelTypes>>>();
9
8
  export const findUniqueMap = new WeakMap<
@@ -32,36 +31,6 @@ export function getRefFromModel<Types extends SchemaTypes>(
32
31
  return cache.get(name)!;
33
32
  }
34
33
 
35
- export function getFindUniqueForRef<Types extends SchemaTypes>(
36
- ref: ObjectRef<unknown>,
37
- builder: PothosSchemaTypes.SchemaBuilder<Types>,
38
- ) {
39
- if (!findUniqueMap.has(builder)) {
40
- findUniqueMap.set(builder, new Map());
41
- }
42
- const cache = findUniqueMap.get(builder)!;
43
-
44
- if (!cache.has(ref)) {
45
- return null;
46
- }
47
-
48
- return cache.get(ref)! as (args: unknown, context: Types['Context']) => unknown;
49
- }
50
-
51
- export function setFindUniqueForRef<Types extends SchemaTypes>(
52
- ref: ObjectRef<unknown>,
53
- builder: PothosSchemaTypes.SchemaBuilder<Types>,
54
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
55
- findUnique: ((args: any, context: Types['Context']) => unknown) | null,
56
- ) {
57
- if (!findUniqueMap.has(builder)) {
58
- findUniqueMap.set(builder, new Map());
59
- }
60
- const cache = findUniqueMap.get(builder)!;
61
-
62
- cache.set(ref, findUnique);
63
- }
64
-
65
34
  export function getRelation<Types extends SchemaTypes>(
66
35
  name: string,
67
36
  builder: PothosSchemaTypes.SchemaBuilder<Types>,
@@ -126,16 +95,6 @@ export function getCursorParser<Types extends SchemaTypes>(
126
95
  });
127
96
  }
128
97
 
129
- export function getRelatedDelegate<Types extends SchemaTypes>(
130
- name: string,
131
- builder: PothosSchemaTypes.SchemaBuilder<Types>,
132
- relation: string,
133
- ) {
134
- const fieldData = getRelation(name, builder, relation);
135
-
136
- return fieldData.type;
137
- }
138
-
139
98
  export function getDelegateFromModel(client: Record<string, unknown>, model: string) {
140
99
  const lowerCase = `${model.slice(0, 1).toLowerCase()}${model.slice(1)}`;
141
100
 
@@ -0,0 +1,51 @@
1
+ /* eslint-disable no-continue */
2
+ export function deepEqual(left: unknown, right: unknown, ignore?: Set<string>) {
3
+ if (left === right) {
4
+ return true;
5
+ }
6
+
7
+ if (left && right && typeof left === 'object' && typeof right === 'object') {
8
+ if (Array.isArray(left)) {
9
+ if (!Array.isArray(right)) {
10
+ return false;
11
+ }
12
+
13
+ const { length } = left;
14
+
15
+ if (right.length !== length) {
16
+ return false;
17
+ }
18
+
19
+ for (let i = 0; i < length; i += 1) {
20
+ if (!deepEqual(left[i], right[i])) {
21
+ return false;
22
+ }
23
+ }
24
+
25
+ return true;
26
+ }
27
+
28
+ const keys = Object.keys(left);
29
+ const keyLength = keys.length;
30
+
31
+ if (keyLength !== Object.keys(right).length) {
32
+ return false;
33
+ }
34
+
35
+ for (const key of keys) {
36
+ if (ignore?.has(key)) {
37
+ continue;
38
+ }
39
+
40
+ if (
41
+ !deepEqual((left as Record<string, unknown>)[key], (right as Record<string, unknown>)[key])
42
+ ) {
43
+ return false;
44
+ }
45
+ }
46
+
47
+ return true;
48
+ }
49
+
50
+ return false;
51
+ }
@@ -1,10 +1,11 @@
1
- import { GraphQLResolveInfo } from 'graphql';
1
+ import { GraphQLNamedType, GraphQLResolveInfo } from 'graphql';
2
2
  import { createContextCache } from '@pothos/core';
3
- import { LoaderMappings } from './types';
3
+ import { LoaderMappings } from '../types';
4
+ import { getIndirectType } from './map-query';
4
5
 
5
6
  const cache = createContextCache((ctx) => new Map<string, LoaderMappings>());
6
7
 
7
- export function cacheKey(path: GraphQLResolveInfo['path'], subPath: string[]) {
8
+ export function cacheKey(type: string, path: GraphQLResolveInfo['path'], subPath: string[]) {
8
9
  let key = '';
9
10
  let current: GraphQLResolveInfo['path'] | undefined = path;
10
11
 
@@ -19,30 +20,29 @@ export function cacheKey(path: GraphQLResolveInfo['path'], subPath: string[]) {
19
20
  key = `${key}.${entry}`;
20
21
  }
21
22
 
22
- return key;
23
+ return `${type}@${key}`;
23
24
  }
24
25
 
25
26
  export function setLoaderMappings(
26
27
  ctx: object,
27
- path: GraphQLResolveInfo['path'],
28
+ info: GraphQLResolveInfo,
28
29
  value: LoaderMappings,
30
+ type: GraphQLNamedType,
29
31
  ) {
30
32
  Object.keys(value).forEach((field) => {
31
33
  const map = cache(ctx);
32
34
 
33
- for (const mapping of value[field]) {
34
- const selectionName = mapping.alias ?? mapping.field;
35
- const subPath = [...mapping.indirectPath, selectionName];
36
- const key = cacheKey(path, subPath);
35
+ const mapping = value[field];
36
+ const subPath = [...mapping.indirectPath, field];
37
+ const key = cacheKey(getIndirectType(type, info).name, info.path, subPath);
37
38
 
38
- map.set(key, mapping.mappings);
39
- }
39
+ map.set(key, mapping.mappings);
40
40
  });
41
41
  }
42
42
 
43
- export function getLoaderMapping(ctx: object, path: GraphQLResolveInfo['path']) {
43
+ export function getLoaderMapping(ctx: object, path: GraphQLResolveInfo['path'], type: string) {
44
44
  const map = cache(ctx);
45
- const key = cacheKey(path, []);
45
+ const key = cacheKey(type, path, []);
46
46
 
47
47
  return map.get(key) ?? null;
48
48
  }