@pothos/plugin-prisma 0.17.2 → 0.18.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.
@@ -14,7 +14,14 @@ import { prismaCursorConnectionQuery, wrapConnectionResult } from './cursors';
14
14
  import { getLoaderMapping, setLoaderMappings } from './loader-map';
15
15
  import { ModelLoader } from './model-loader';
16
16
  import { PrismaObjectRef } from './object-ref';
17
- import { getDelegateFromModel, getFindUniqueForRef, getRefFromModel, getRelation } from './refs';
17
+ import {
18
+ getCursorFormatter,
19
+ getCursorParser,
20
+ getDelegateFromModel,
21
+ getFindUniqueForRef,
22
+ getRefFromModel,
23
+ getRelation,
24
+ } from './refs';
18
25
  import {
19
26
  PrismaDelegate,
20
27
  PrismaModelTypes,
@@ -96,10 +103,13 @@ export class PrismaObjectFieldBuilder<
96
103
  const loaderCache = ModelLoader.forModel(this.model, this.builder);
97
104
  let typeName: string | undefined;
98
105
 
106
+ const formatCursor = getCursorFormatter(relationField.type, this.builder, cursor);
107
+ const parseCursor = getCursorParser(relationField.type, this.builder, cursor);
108
+
99
109
  const getQuery = (args: PothosSchemaTypes.DefaultConnectionArguments, ctx: {}) => ({
100
110
  ...((typeof query === 'function' ? query(args, ctx) : query) as {}),
101
111
  ...prismaCursorConnectionQuery({
102
- column: cursor,
112
+ parseCursor,
103
113
  maxSize,
104
114
  defaultSize,
105
115
  args,
@@ -160,7 +170,7 @@ export class PrismaObjectFieldBuilder<
160
170
  await getResult(),
161
171
  args,
162
172
  connectionQuery.take,
163
- cursor,
173
+ formatCursor,
164
174
  (parent as { _count?: Record<string, number> })._count?.[name],
165
175
  );
166
176
  },
package/src/refs.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { ObjectRef, SchemaTypes } from '@pothos/core';
2
2
  import { Prisma } from '../tests/client';
3
+ import { formatCursor, parseCompositeCursor, parseRawCursor } from './cursors';
3
4
  import { PrismaObjectRef } from './object-ref';
4
5
  import { PrismaDelegate } from './types';
5
6
  import { PrismaModelTypes } from '.';
@@ -65,6 +66,25 @@ export function getRelation<Types extends SchemaTypes>(
65
66
  name: string,
66
67
  builder: PothosSchemaTypes.SchemaBuilder<Types>,
67
68
  relation: string,
69
+ ) {
70
+ const modelData = getModel(name, builder);
71
+
72
+ const fieldData = modelData.fields.find((field) => field.name === relation);
73
+
74
+ if (!fieldData) {
75
+ throw new Error(`Field '${relation}' not found in model '${name}'`);
76
+ }
77
+
78
+ if (fieldData.kind !== 'object') {
79
+ throw new Error(`Field ${relation} of model '${name}' is not a relation (${fieldData.kind})`);
80
+ }
81
+
82
+ return fieldData;
83
+ }
84
+
85
+ export function getModel<Types extends SchemaTypes>(
86
+ name: string,
87
+ builder: PothosSchemaTypes.SchemaBuilder<Types>,
68
88
  ) {
69
89
  const { client } = builder.options.prisma;
70
90
  // eslint-disable-next-line no-underscore-dangle
@@ -76,17 +96,34 @@ export function getRelation<Types extends SchemaTypes>(
76
96
  throw new Error(`Model '${name}' not found in DMMF`);
77
97
  }
78
98
 
79
- const fieldData = modelData.fields.find((field) => field.name === relation);
99
+ return modelData;
100
+ }
80
101
 
81
- if (!fieldData) {
82
- throw new Error(`Field '${relation}' not found in model '${name}'`);
83
- }
102
+ export function getCursorFormatter<Types extends SchemaTypes>(
103
+ name: string,
104
+ builder: PothosSchemaTypes.SchemaBuilder<Types>,
105
+ cursor: string,
106
+ ) {
107
+ const modelData = getModel(name, builder);
108
+ const primaryKey = modelData.primaryKey?.name ?? modelData.primaryKey?.fields.join('_');
84
109
 
85
- if (fieldData.kind !== 'object') {
86
- throw new Error(`Field ${relation} of model '${name}' is not a relation (${fieldData.kind})`);
87
- }
110
+ return formatCursor(cursor === primaryKey ? modelData.primaryKey!.fields : cursor);
111
+ }
88
112
 
89
- return fieldData;
113
+ export function getCursorParser<Types extends SchemaTypes>(
114
+ name: string,
115
+ builder: PothosSchemaTypes.SchemaBuilder<Types>,
116
+ cursor: string,
117
+ ) {
118
+ const modelData = getModel(name, builder);
119
+ const primaryKey = modelData.primaryKey?.name ?? modelData.primaryKey?.fields.join('_');
120
+
121
+ const parser =
122
+ cursor === primaryKey ? parseCompositeCursor(modelData.primaryKey!.fields) : parseRawCursor;
123
+
124
+ return (rawCursor: string) => ({
125
+ [cursor]: parser(rawCursor),
126
+ });
90
127
  }
91
128
 
92
129
  export function getRelatedDelegate<Types extends SchemaTypes>(
@@ -37,6 +37,7 @@ schemaBuilderProto.prismaObject = function prismaObject(type, { fields, findUniq
37
37
  extensions: {
38
38
  ...options.extensions,
39
39
  pothosPrismaInclude: options.include,
40
+ pothosPrismaModel: type,
40
41
  },
41
42
  name,
42
43
  fields: fields ? () => fields(new PrismaObjectFieldBuilder(name, this, type)) : undefined,