diff --git a/packages/plugin-prisma-crud/src/schema-builder.ts b/packages/plugin-prisma-crud/src/schema-builder.ts index 2245d63da..3e22fbe94 100644 --- a/packages/plugin-prisma-crud/src/schema-builder.ts +++ b/packages/plugin-prisma-crud/src/schema-builder.ts @@ -12,6 +12,7 @@ import { FilterListOps, FilterOps, FilterShape, + OpsOptions, PrismaFilterOptions, PrismaListFilterOptions, PrismaOrderByOptions, @@ -28,20 +29,31 @@ const OrderByRefMap = new WeakMap< schemaBuilder.prismaFilter = function prismaFilter< Type extends InputType, - Ops extends FilterOps, + Ops extends OpsOptions, >(type: Type, { ops, name, ...options }: PrismaFilterOptions) { const filterName = name ?? `${nameFromType(type, this)}Filter`; const ref = - this.inputRef>, Ops>>( - filterName, - ); + this.inputRef< + Pick< + FilterShape>, + Ops extends string[] ? Ops[number] : keyof Ops + > + >(filterName); + + const opsOptions: Record = Array.isArray(ops) + ? ops.reduce>((map, op) => { + // eslint-disable-next-line no-param-reassign + map[op] = {}; + return map; + }, {}) + : ops; ref.implement({ ...options, fields: (t) => { const fields: Record> = {}; - for (const op of ops) { + for (const op of Object.keys(opsOptions)) { const isList = op === 'in' || op === 'notIn'; let fieldType: InputType | [InputType] = type; @@ -54,6 +66,7 @@ schemaBuilder.prismaFilter = function prismaFilter< fields[op] = t.field({ required: isList ? { list: false, items: true } : false, type: fieldType, + ...(opsOptions[op] as {}), }); } @@ -61,12 +74,12 @@ schemaBuilder.prismaFilter = function prismaFilter< }, }); - return ref as InputRef>, Ops>>; + return ref as never; }; schemaBuilder.prismaListFilter = function prismaListFilter< Type extends InputType, - Ops extends FilterListOps, + Ops extends OpsOptions, >(type: Type, { name, ops, ...options }: PrismaListFilterOptions) { let filterName = name; @@ -78,19 +91,25 @@ schemaBuilder.prismaListFilter = function prismaListFilter< : `List${typeName}`; } - const ref = this.inputRef<{ - [K in Ops]: InputShapeFromTypeParam; - }>(filterName); + const ref = this.inputRef(filterName); + const opsOptions: Record = Array.isArray(ops) + ? ops.reduce>((map, op) => { + // eslint-disable-next-line no-param-reassign + map[op] = {}; + return map; + }, {}) + : ops; ref.implement({ ...options, fields: (t) => { const fields: Record> = {}; - for (const op of ops) { + for (const op of Object.keys(opsOptions)) { fields[op] = t.field({ required: false, type, + ...(opsOptions[op] as {}), }); } @@ -98,9 +117,7 @@ schemaBuilder.prismaListFilter = function prismaListFilter< }, }); - return ref as InputRef<{ - [K in Ops]: InputShapeFromTypeParam; - }>; + return ref as never; }; schemaBuilder.orderByEnum = function orderByEnum() { diff --git a/packages/plugin-prisma-crud/src/types.ts b/packages/plugin-prisma-crud/src/types.ts index f71706d85..0babe38c3 100644 --- a/packages/plugin-prisma-crud/src/types.ts +++ b/packages/plugin-prisma-crud/src/types.ts @@ -1,14 +1,7 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ // Type map: https://github.com/prisma/prisma/blob/main/packages/client/src/runtime/utils/common.ts#L63 -import { - BaseEnum, - InputRef, - InputShapeFromTypeParam, - InputType, - InputTypeParam, - SchemaTypes, -} from '@pothos/core'; +import { BaseEnum, InputRef, InputType, SchemaTypes } from '@pothos/core'; import { PrismaModelTypes } from '@pothos/plugin-prisma'; export type ScalarFilters = T extends { equals?: unknown } diff --git a/packages/plugin-prisma-crud/tests/__snapshots__/index.test.ts.snap b/packages/plugin-prisma-crud/tests/__snapshots__/index.test.ts.snap index a4d4b9dfe..d4454b506 100644 --- a/packages/plugin-prisma-crud/tests/__snapshots__/index.test.ts.snap +++ b/packages/plugin-prisma-crud/tests/__snapshots__/index.test.ts.snap @@ -1,11 +1,21 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`prisma crud generates schema 1`] = ` -"input IntFilter { +"input CommentWhere { + createdAt: DateTime +} + +scalar DateTime + +input IntFilter { equals: Int not: IntFilter } +input ListCommentWhere { + some: CommentWhere +} + enum OrderBy { asc desc @@ -22,6 +32,17 @@ input PostOrderBy { title: OrderBy } +input PostWhere { + author: UserWhere + + \\"\\"\\"filter by author id\\"\\"\\" + authorId: IntFilter + comments: ListCommentWhere + createdAt: DateTime + id: IntFilter + title: String +} + input ProfileOrderBy { bio: OrderBy user: UserOrderBy @@ -47,5 +68,9 @@ input StringListFilter { input UserOrderBy { name: OrderBy profile: ProfileOrderBy +} + +input UserWhere { + id: IntFilter }" `; diff --git a/packages/plugin-prisma-crud/tests/example/schema/index.ts b/packages/plugin-prisma-crud/tests/example/schema/index.ts index 6b01ca989..b9a829c23 100644 --- a/packages/plugin-prisma-crud/tests/example/schema/index.ts +++ b/packages/plugin-prisma-crud/tests/example/schema/index.ts @@ -14,6 +14,11 @@ const StringListFilter = builder.prismaListFilter(StringFilter, { ops: ['every', 'some', 'none'], }); +builder.scalarType('DateTime', { + serialize: (value) => value.toISOString(), + parseValue: (value) => (typeof value === 'number' ? new Date(value) : new Date(String(value))), +}); + builder.queryType({}); builder.queryField('post', (t) => diff --git a/packages/plugin-prisma/src/types.ts b/packages/plugin-prisma/src/types.ts index d326ba2f0..00601b780 100644 --- a/packages/plugin-prisma/src/types.ts +++ b/packages/plugin-prisma/src/types.ts @@ -498,13 +498,12 @@ export type PrismaConnectionFieldOptions< ResolveReturnShape >, 'args' | 'resolve' | 'type' -<<<<<<< HEAD > & (InputShapeFromFields & PothosSchemaTypes.DefaultConnectionArguments extends infer ConnectionArgs ? { type: Type; - cursor: string & keyof Model['Where']; + cursor: string & keyof Model['WhereUnique']; defaultSize?: number | ((args: ConnectionArgs, ctx: Types['Context']) => number); maxSize?: number | ((args: ConnectionArgs, ctx: Types['Context']) => number); resolve: ( @@ -527,32 +526,6 @@ export type PrismaConnectionFieldOptions< ) => MaybePromise; } : never); -======= - > & { - type: Type; - cursor: string & keyof Model['WhereUnique']; - defaultSize?: number; - maxSize?: number; - resolve: ( - query: { - include?: Model['Include']; - cursor?: {}; - take: number; - skip: number; - }, - parent: ParentShape, - args: InputShapeFromFields & PothosSchemaTypes.DefaultConnectionArguments, - context: Types['Context'], - info: GraphQLResolveInfo, - ) => MaybePromise; - totalCount?: ( - parent: ParentShape, - args: InputShapeFromFields & PothosSchemaTypes.DefaultConnectionArguments, - context: Types['Context'], - info: GraphQLResolveInfo, - ) => MaybePromise; - }; ->>>>>>> 67bda837 (wip) export type RelatedConnectionOptions< Types extends SchemaTypes,