@pothos/plugin-prisma 0.15.0 → 0.16.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 (147) hide show
  1. package/.turbo/turbo-build.log +5 -5
  2. package/.turbo/turbo-test.log +8 -7
  3. package/.turbo/turbo-type.log +5 -0
  4. package/CHANGELOG.md +24 -0
  5. package/README.md +3 -3
  6. package/esm/cursors.d.ts +0 -0
  7. package/esm/cursors.d.ts.map +0 -0
  8. package/esm/cursors.js +0 -0
  9. package/esm/cursors.js.map +0 -0
  10. package/esm/field-builder.d.ts +0 -0
  11. package/esm/field-builder.d.ts.map +1 -1
  12. package/esm/field-builder.js +5 -3
  13. package/esm/field-builder.js.map +1 -1
  14. package/esm/generator.d.ts +0 -0
  15. package/esm/generator.d.ts.map +0 -0
  16. package/esm/generator.js +1 -0
  17. package/esm/generator.js.map +1 -1
  18. package/esm/global-types.d.ts +5 -4
  19. package/esm/global-types.d.ts.map +1 -1
  20. package/esm/global-types.js +0 -0
  21. package/esm/global-types.js.map +0 -0
  22. package/esm/index.d.ts +0 -0
  23. package/esm/index.d.ts.map +0 -0
  24. package/esm/index.js +0 -0
  25. package/esm/index.js.map +0 -0
  26. package/esm/loader-map.d.ts +0 -0
  27. package/esm/loader-map.d.ts.map +0 -0
  28. package/esm/loader-map.js +0 -0
  29. package/esm/loader-map.js.map +0 -0
  30. package/esm/model-loader.d.ts +2 -1
  31. package/esm/model-loader.d.ts.map +1 -1
  32. package/esm/model-loader.js +23 -11
  33. package/esm/model-loader.js.map +1 -1
  34. package/esm/node-ref.d.ts +4 -2
  35. package/esm/node-ref.d.ts.map +1 -1
  36. package/esm/node-ref.js +3 -2
  37. package/esm/node-ref.js.map +1 -1
  38. package/esm/object-ref.d.ts +7 -0
  39. package/esm/object-ref.d.ts.map +1 -0
  40. package/esm/object-ref.js +5 -0
  41. package/esm/object-ref.js.map +1 -0
  42. package/esm/prisma-field-builder.d.ts +6 -0
  43. package/esm/prisma-field-builder.d.ts.map +1 -1
  44. package/esm/prisma-field-builder.js +33 -5
  45. package/esm/prisma-field-builder.js.map +1 -1
  46. package/esm/refs.d.ts +4 -2
  47. package/esm/refs.d.ts.map +1 -1
  48. package/esm/refs.js +2 -1
  49. package/esm/refs.js.map +1 -1
  50. package/esm/schema-builder.d.ts +0 -0
  51. package/esm/schema-builder.d.ts.map +0 -0
  52. package/esm/schema-builder.js +8 -11
  53. package/esm/schema-builder.js.map +1 -1
  54. package/esm/types.d.ts +15 -5
  55. package/esm/types.d.ts.map +1 -1
  56. package/esm/types.js +0 -0
  57. package/esm/types.js.map +0 -0
  58. package/esm/util/index.d.ts +0 -0
  59. package/esm/util/index.d.ts.map +0 -0
  60. package/esm/util/index.js +0 -0
  61. package/esm/util/index.js.map +0 -0
  62. package/esm/util/map-includes.d.ts +1 -0
  63. package/esm/util/map-includes.d.ts.map +1 -1
  64. package/esm/util/map-includes.js +23 -6
  65. package/esm/util/map-includes.js.map +1 -1
  66. package/esm/util/merge-includes.d.ts +0 -0
  67. package/esm/util/merge-includes.d.ts.map +0 -0
  68. package/esm/util/merge-includes.js +0 -0
  69. package/esm/util/merge-includes.js.map +0 -0
  70. package/lib/cursors.d.ts +0 -0
  71. package/lib/cursors.d.ts.map +0 -0
  72. package/lib/cursors.js +0 -0
  73. package/lib/cursors.js.map +0 -0
  74. package/lib/field-builder.d.ts +0 -0
  75. package/lib/field-builder.d.ts.map +1 -1
  76. package/lib/field-builder.js +5 -3
  77. package/lib/field-builder.js.map +1 -1
  78. package/lib/generator.d.ts +0 -0
  79. package/lib/generator.d.ts.map +0 -0
  80. package/lib/generator.js +1 -0
  81. package/lib/generator.js.map +1 -1
  82. package/lib/global-types.d.ts +5 -4
  83. package/lib/global-types.d.ts.map +1 -1
  84. package/lib/global-types.js +0 -0
  85. package/lib/global-types.js.map +0 -0
  86. package/lib/index.d.ts +0 -0
  87. package/lib/index.d.ts.map +0 -0
  88. package/lib/index.js +0 -0
  89. package/lib/index.js.map +0 -0
  90. package/lib/loader-map.d.ts +0 -0
  91. package/lib/loader-map.d.ts.map +0 -0
  92. package/lib/loader-map.js +0 -0
  93. package/lib/loader-map.js.map +0 -0
  94. package/lib/model-loader.d.ts +2 -1
  95. package/lib/model-loader.d.ts.map +1 -1
  96. package/lib/model-loader.js +23 -11
  97. package/lib/model-loader.js.map +1 -1
  98. package/lib/node-ref.d.ts +4 -2
  99. package/lib/node-ref.d.ts.map +1 -1
  100. package/lib/node-ref.js +2 -1
  101. package/lib/node-ref.js.map +1 -1
  102. package/lib/object-ref.d.ts +7 -0
  103. package/lib/object-ref.d.ts.map +1 -0
  104. package/lib/object-ref.js +9 -0
  105. package/lib/object-ref.js.map +1 -0
  106. package/lib/prisma-field-builder.d.ts +6 -0
  107. package/lib/prisma-field-builder.d.ts.map +1 -1
  108. package/lib/prisma-field-builder.js +32 -4
  109. package/lib/prisma-field-builder.js.map +1 -1
  110. package/lib/refs.d.ts +4 -2
  111. package/lib/refs.d.ts.map +1 -1
  112. package/lib/refs.js +2 -1
  113. package/lib/refs.js.map +1 -1
  114. package/lib/schema-builder.d.ts +0 -0
  115. package/lib/schema-builder.d.ts.map +0 -0
  116. package/lib/schema-builder.js +8 -11
  117. package/lib/schema-builder.js.map +1 -1
  118. package/lib/types.d.ts +15 -5
  119. package/lib/types.d.ts.map +1 -1
  120. package/lib/types.js +0 -0
  121. package/lib/types.js.map +0 -0
  122. package/lib/util/index.d.ts +0 -0
  123. package/lib/util/index.d.ts.map +0 -0
  124. package/lib/util/index.js +0 -0
  125. package/lib/util/index.js.map +0 -0
  126. package/lib/util/map-includes.d.ts +1 -0
  127. package/lib/util/map-includes.d.ts.map +1 -1
  128. package/lib/util/map-includes.js +24 -7
  129. package/lib/util/map-includes.js.map +1 -1
  130. package/lib/util/merge-includes.d.ts +0 -0
  131. package/lib/util/merge-includes.d.ts.map +0 -0
  132. package/lib/util/merge-includes.js +0 -0
  133. package/lib/util/merge-includes.js.map +0 -0
  134. package/package.json +4 -3
  135. package/src/field-builder.ts +7 -5
  136. package/src/generator.ts +7 -0
  137. package/src/global-types.ts +17 -7
  138. package/src/model-loader.ts +30 -13
  139. package/src/node-ref.ts +7 -2
  140. package/src/object-ref.ts +8 -0
  141. package/src/prisma-field-builder.ts +52 -3
  142. package/src/refs.ts +5 -3
  143. package/src/schema-builder.ts +5 -7
  144. package/src/types.ts +63 -35
  145. package/src/util/map-includes.ts +35 -3
  146. package/tsconfig.tsbuildinfo +1 -1
  147. package/generated.ts +0 -54
@@ -18,8 +18,8 @@ import { queryFromInfo } from './util';
18
18
  const schemaBuilderProto = SchemaBuilder.prototype as PothosSchemaTypes.SchemaBuilder<SchemaTypes>;
19
19
 
20
20
  schemaBuilderProto.prismaObject = function prismaObject(type, { fields, findUnique, ...options }) {
21
- const ref = getRefFromModel(type, this);
22
- const name = options.name ?? type;
21
+ const ref = options.variant ? this.objectRef(options.variant) : getRefFromModel(type, this);
22
+ const name = options.variant ?? options.name ?? type;
23
23
 
24
24
  ref.name = name;
25
25
 
@@ -53,6 +53,7 @@ schemaBuilderProto.prismaNode = function prismaNode(
53
53
  {
54
54
  findUnique,
55
55
  name,
56
+ variant,
56
57
  ...options
57
58
  }: PrismaNodeOptions<SchemaTypes, ModelTypes<PrismaDelegate>, [], never, {}>,
58
59
  ) {
@@ -62,16 +63,13 @@ schemaBuilderProto.prismaNode = function prismaNode(
62
63
  throw new TypeError('builder.prismaNode requires @pothos/plugin-relay to be installed');
63
64
  }
64
65
 
65
- const typeName = name ?? type;
66
+ const typeName = variant ?? name ?? type;
66
67
  const delegate = getDelegateFromModel(this.options.prisma.client, type);
67
68
  const nodeRef = new PrismaNodeRef(typeName);
68
69
  const extendedOptions = {
69
70
  ...options,
71
+ variant,
70
72
  interfaces: [interfaceRef, ...(options.interfaces ?? [])],
71
- extensions: {
72
- ...options.extensions,
73
- pothosPrismaInclude: options.include,
74
- },
75
73
  isTypeOf: (val: unknown) => nodeRef.hasBrand(val),
76
74
  findUnique: (parent: unknown, context: {}) =>
77
75
  findUnique(options.id.resolve(parent as never, context) as string, context),
package/src/types.ts CHANGED
@@ -20,6 +20,7 @@ import {
20
20
  TypeParam,
21
21
  } from '@pothos/core';
22
22
  import { PrismaObjectFieldBuilder } from './field-builder';
23
+ import { PrismaObjectRef } from './object-ref';
23
24
 
24
25
  export interface PrismaDelegate {
25
26
  findUnique: (...args: any[]) => Promise<unknown>;
@@ -65,6 +66,7 @@ export type ModelTypes<Model extends {}> = Model extends {
65
66
  : never;
66
67
 
67
68
  export interface PrismaModelTypes {
69
+ Name: string;
68
70
  Shape: {};
69
71
  Include: unknown;
70
72
  Where: {};
@@ -126,16 +128,27 @@ export type PrismaObjectTypeOptions<
126
128
  FindUnique,
127
129
  Include extends Model['Include'],
128
130
  Shape extends object,
129
- > = Omit<
130
- | PothosSchemaTypes.ObjectTypeOptions<Types, ObjectRef<Shape>>
131
- | PothosSchemaTypes.ObjectTypeWithInterfaceOptions<Types, ObjectRef<Shape>, Interfaces>,
132
- 'fields'
133
- > & {
134
- name?: string;
135
- include?: Include;
136
- fields?: PrismaObjectFieldsShape<Types, Model, FindUnique extends null ? true : false, Shape>;
137
- findUnique: FindUnique & (((parent: Shape, context: Types['Context']) => Model['Where']) | null);
138
- };
131
+ > = NameOrVariant &
132
+ Omit<
133
+ | PothosSchemaTypes.ObjectTypeOptions<Types, Shape>
134
+ | PothosSchemaTypes.ObjectTypeWithInterfaceOptions<Types, Shape, Interfaces>,
135
+ 'fields'
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
+ };
142
+
143
+ export type NameOrVariant =
144
+ | {
145
+ name?: never;
146
+ variant?: string;
147
+ }
148
+ | {
149
+ name?: string;
150
+ variant?: never;
151
+ };
139
152
 
140
153
  export type PrismaNodeOptions<
141
154
  Types extends SchemaTypes,
@@ -143,31 +156,31 @@ export type PrismaNodeOptions<
143
156
  Interfaces extends InterfaceParam<Types>[],
144
157
  Include extends Model['Include'],
145
158
  Shape extends object,
146
- > = Omit<
147
- | PothosSchemaTypes.ObjectTypeOptions<Types, Shape>
148
- | PothosSchemaTypes.ObjectTypeWithInterfaceOptions<Types, ObjectRef<Shape>, Interfaces>,
149
- 'fields' | 'isTypeOf'
150
- > & {
151
- name?: string;
152
- include?: Include;
153
- id: Omit<
154
- FieldOptionsFromKind<
155
- Types,
156
- Shape,
157
- 'ID',
158
- false,
159
- {},
160
- 'Object',
161
- OutputShape<Types, 'ID'>,
162
- MaybePromise<OutputShape<Types, 'ID'>>
163
- >,
164
- 'args' | 'nullable' | 'resolve' | 'type'
159
+ > = NameOrVariant &
160
+ Omit<
161
+ | PothosSchemaTypes.ObjectTypeOptions<Types, Shape>
162
+ | PothosSchemaTypes.ObjectTypeWithInterfaceOptions<Types, Shape, Interfaces>,
163
+ 'fields' | 'isTypeOf'
165
164
  > & {
166
- resolve: (parent: Shape, context: Types['Context']) => MaybePromise<OutputShape<Types, 'ID'>>;
165
+ include?: Include;
166
+ id: Omit<
167
+ FieldOptionsFromKind<
168
+ Types,
169
+ Shape,
170
+ 'ID',
171
+ false,
172
+ {},
173
+ 'Object',
174
+ OutputShape<Types, 'ID'>,
175
+ MaybePromise<OutputShape<Types, 'ID'>>
176
+ >,
177
+ 'args' | 'nullable' | 'resolve' | 'type'
178
+ > & {
179
+ resolve: (parent: Shape, context: Types['Context']) => MaybePromise<OutputShape<Types, 'ID'>>;
180
+ };
181
+ fields?: PrismaObjectFieldsShape<Types, Model, false, Shape>;
182
+ findUnique: (id: string, context: Types['Context']) => Model['Where'];
167
183
  };
168
- fields?: PrismaObjectFieldsShape<Types, Model, false, Shape>;
169
- findUnique: (id: string, context: Types['Context']) => Model['Where'];
170
- };
171
184
 
172
185
  export type QueryForField<
173
186
  Types extends SchemaTypes,
@@ -254,9 +267,19 @@ export type RelatedFieldOptions<
254
267
  ShapeWithNullability<Types, Model['Relations'][Field]['Shape'], Nullable>
255
268
  >;
256
269
  }) & {
270
+ type?: PrismaObjectRef<Model['Relations'][Field]['Types']>;
257
271
  query?: QueryForField<Types, Args, Model['Include'][Field & keyof Model['Include']]>;
258
272
  };
259
273
 
274
+ export type VariantFieldOptions<
275
+ Types extends SchemaTypes,
276
+ Model extends PrismaModelTypes,
277
+ Variant extends PrismaObjectRef<Model>,
278
+ > = Omit<
279
+ PothosSchemaTypes.ObjectFieldOptions<Types, Model['Shape'], Variant, false, {}, Model['Shape']>,
280
+ 'resolve' | 'type'
281
+ >;
282
+
260
283
  export type RelationCountOptions<
261
284
  Types extends SchemaTypes,
262
285
  Shape,
@@ -286,7 +309,11 @@ export type RelationCountOptions<
286
309
  export type PrismaFieldOptions<
287
310
  Types extends SchemaTypes,
288
311
  ParentShape,
289
- Type extends keyof Types['PrismaTypes'] | [keyof Types['PrismaTypes']],
312
+ Type extends
313
+ | PrismaObjectRef<PrismaModelTypes>
314
+ | keyof Types['PrismaTypes']
315
+ | [keyof Types['PrismaTypes']]
316
+ | [PrismaObjectRef<PrismaModelTypes>],
290
317
  Model extends PrismaModelTypes,
291
318
  Param extends TypeParam<Types>,
292
319
  Args extends InputFieldMap,
@@ -325,7 +352,7 @@ export type PrismaFieldOptions<
325
352
  export type PrismaConnectionFieldOptions<
326
353
  Types extends SchemaTypes,
327
354
  ParentShape,
328
- Type extends keyof Types['PrismaTypes'],
355
+ Type extends PrismaObjectRef<PrismaModelTypes> | keyof Types['PrismaTypes'],
329
356
  Model extends PrismaModelTypes,
330
357
  Param extends OutputType<Types>,
331
358
  Nullable extends boolean,
@@ -406,6 +433,7 @@ export type RelatedConnectionOptions<
406
433
  'resolve' | 'type'
407
434
  > & {
408
435
  query?: QueryForField<Types, Args, Model['Include'][Field & keyof Model['Include']]>;
436
+ type?: PrismaObjectRef<Model['Relations'][Field]['Types']>;
409
437
  cursor: CursorFromRelation<Model, Field>;
410
438
  defaultSize?: number;
411
439
  maxSize?: number;
@@ -22,6 +22,8 @@ import {
22
22
  } from '../types';
23
23
  import { mergeIncludes, resolveIndirectType } from '.';
24
24
 
25
+ export const SELF_RELATION = '@self';
26
+
25
27
  function handleField(
26
28
  ctx: object,
27
29
  info: GraphQLResolveInfo,
@@ -108,7 +110,8 @@ function handleField(
108
110
  query = query(args, ctx);
109
111
  }
110
112
 
111
- const existingInclude = includes[relationName];
113
+ const existingInclude =
114
+ relationName === SELF_RELATION ? { include: includes } : includes[relationName];
112
115
 
113
116
  query = { ...(query as {}), include: newIncludes };
114
117
 
@@ -120,6 +123,10 @@ function handleField(
120
123
  }
121
124
  }
122
125
 
126
+ if (!mappings[relationName]) {
127
+ mappings[relationName] = [];
128
+ }
129
+
123
130
  const nestedIncludes = (query as { include: IncludeMap }).include;
124
131
  const nestedMappings: LoaderMappings = {};
125
132
  const nestedCounts: IncludeCounts = {
@@ -127,8 +134,33 @@ function handleField(
127
134
  parent: counts.current,
128
135
  };
129
136
 
130
- if (!mappings[relationName]) {
131
- mappings[relationName] = [];
137
+ if (relationName === SELF_RELATION) {
138
+ mappings[relationName].push({
139
+ field: selection.name.value,
140
+ alias: selection.alias?.value,
141
+ mappings: nestedMappings,
142
+ indirectPath: indirectMap?.path ?? [],
143
+ });
144
+
145
+ const { include } = query as { include: Record<string, IncludeMap> };
146
+
147
+ if (selection.selectionSet) {
148
+ includesFromSelectionSet(
149
+ ctx,
150
+ type,
151
+ info,
152
+ includes,
153
+ counts,
154
+ nestedMappings,
155
+ selection.selectionSet,
156
+ );
157
+ }
158
+
159
+ Object.keys(include).forEach((key) => {
160
+ includes[key] = include[key];
161
+ });
162
+
163
+ return;
132
164
  }
133
165
 
134
166
  mappings[relationName].push({