Skip to content

Commit

Permalink
fix a couple issue with generated types
Browse files Browse the repository at this point in the history
  • Loading branch information
hayes committed Jan 7, 2023
1 parent 95b38a0 commit 63547bf
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 41 deletions.
22 changes: 22 additions & 0 deletions packages/plugin-prisma-utils/src/schema-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,28 @@ schemaBuilder.prismaUpdateRelation = function prismaUpdateRelation<

if (fieldOption instanceof InputFieldRef) {
fieldDefs[field] = fieldOption as InputFieldRef<SchemaTypes, 'InputObject'>;
} else if (fieldModel.isList && (field === 'update' || field === 'updateMany')) {
const {
name: nestedName = `${ref.name}U${field.slice(1)}`,
where: whereType,
data: dataType,
} = fieldOption as {
name?: string;
where: InputRef<unknown> | InputFieldRef<SchemaTypes>;
data: InputRef<unknown> | InputFieldRef<SchemaTypes>;
};

const nestedRef = this.inputType(nestedName, {
fields: (t2) => ({
where: whereType instanceof InputFieldRef ? whereType : t2.field({ type: whereType }),
data: dataType instanceof InputFieldRef ? dataType : t2.field({ type: dataType }),
}),
});

fieldDefs[field] = t.field({
required: false,
type: [nestedRef],
});
} else {
fieldDefs[field] = t.field({
required: false,
Expand Down
47 changes: 31 additions & 16 deletions packages/plugin-prisma-utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export interface FilterShape<T> {

export type FilterOps = keyof FilterShape<unknown>;

export type Enumerable<T> = T | T[];

export type PrismaOrderByFields<Types extends SchemaTypes, Model extends PrismaModelTypes> = {
[K in keyof Model['OrderBy'] as K extends Model['ListRelations']
? never
Expand Down Expand Up @@ -365,22 +367,35 @@ export interface PrismaUpdateManyRelationFields<
? T
: never
>;
update?: InputWithShape<
Types,
Model['Update'][Relation & keyof Model['Update']] & { update?: unknown } extends {
update?: infer T;
}
? T
: never
>;
updateMany?: InputWithShape<
Types,
Model['Update'][Relation & keyof Model['Update']] & { updateMany?: unknown } extends {
updateMany?: infer T;
}
? T
: never
>;
update?: Model['Update'][Relation & keyof Model['Update']] & {
update?: { data: unknown; where: unknown };
} extends {
update?: {
data: infer D;
where: infer W;
};
}
? {
name?: string;
data: InputWithShape<Types, D>;
where: InputWithShape<Types, W>;
}
: never;
updateMany?: Model['Update'][Relation & keyof Model['Update']] & {
updateMany?: { data: unknown; where: unknown };
} extends {
update?: {
data: infer D;
where: infer W;
};
}
? {
name?: string;
data: InputWithShape<Types, D>;
where: InputWithShape<Types, W>;
}
: never;

deleteMany?: InputWithShape<
Types,
Model['Update'][Relation & keyof Model['Update']] & { deleteMany?: unknown } extends {
Expand Down
112 changes: 96 additions & 16 deletions packages/plugin-prisma-utils/tests/__snapshots__/crud.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,18 @@ input MediaUpdatePostsRelation {
deleteMany: [PostMediaWithoutMediaFilter!]
disconnect: [PostMediaUniqueFilter!]
set: [PostMediaUniqueFilter!]
update: [PostMediaUpdateWithoutMediaInput!]
updateMany: [PostMediaWithoutMediaFilter!]
update: [MediaUpdatePostsRelationUpdate!]
updateMany: [MediaUpdatePostsRelationUpdateMany!]
}
input MediaUpdatePostsRelationUpdate {
data: PostMediaUpdateWithoutMediaInput
where: PostMediaUniqueFilter
}
input MediaUpdatePostsRelationUpdateMany {
data: PostMediaUpdateWithoutMediaInput
where: PostMediaWithoutMediaFilter
}
input MediaUpdateUploadedByRelation {
Expand Down Expand Up @@ -503,8 +513,18 @@ input PostUpdateCommentsRelation {
deleteMany: [CommentWithoutPostFilter!]
disconnect: [CommentUniqueFilter!]
set: [CommentUniqueFilter!]
update: [CommentUpdateWithoutPostInput!]
updateMany: [CommentWithoutPostFilter!]
update: [PostUpdateCommentsRelationUpdate!]
updateMany: [PostUpdateCommentsRelationUpdateMany!]
}
input PostUpdateCommentsRelationUpdate {
data: CommentUpdateWithoutPostInput
where: CommentUniqueFilter
}
input PostUpdateCommentsRelationUpdateMany {
data: CommentUpdateWithoutPostInput
where: CommentWithoutPostFilter
}
input PostUpdateMediaRelation {
Expand All @@ -514,8 +534,18 @@ input PostUpdateMediaRelation {
deleteMany: [PostMediaWithoutPostFilter!]
disconnect: [PostMediaUniqueFilter!]
set: [PostMediaUniqueFilter!]
update: [PostMediaUpdateWithoutPostInput!]
updateMany: [PostMediaWithoutPostFilter!]
update: [PostUpdateMediaRelationUpdate!]
updateMany: [PostUpdateMediaRelationUpdateMany!]
}
input PostUpdateMediaRelationUpdate {
data: PostMediaUpdateWithoutPostInput
where: PostMediaUniqueFilter
}
input PostUpdateMediaRelationUpdateMany {
data: PostMediaUpdateWithoutPostInput
where: PostMediaWithoutPostFilter
}
input PostUpdateWithoutAuthorInput {
Expand Down Expand Up @@ -748,8 +778,18 @@ input UserUpdateCommentsRelation {
deleteMany: [CommentWithoutAuthorFilter!]
disconnect: [CommentUniqueFilter!]
set: [CommentUniqueFilter!]
update: [CommentUpdateWithoutAuthorInput!]
updateMany: [CommentWithoutAuthorFilter!]
update: [UserUpdateCommentsRelationUpdate!]
updateMany: [UserUpdateCommentsRelationUpdateMany!]
}
input UserUpdateCommentsRelationUpdate {
data: CommentUpdateWithoutAuthorInput
where: CommentUniqueFilter
}
input UserUpdateCommentsRelationUpdateMany {
data: CommentUpdateWithoutAuthorInput
where: CommentWithoutAuthorFilter
}
input UserUpdateFollowersRelation {
Expand All @@ -759,8 +799,18 @@ input UserUpdateFollowersRelation {
deleteMany: [FollowWithoutToFilter!]
disconnect: [FollowUniqueFilter!]
set: [FollowUniqueFilter!]
update: [FollowUpdateWithoutToInput!]
updateMany: [FollowWithoutToFilter!]
update: [UserUpdateFollowersRelationUpdate!]
updateMany: [UserUpdateFollowersRelationUpdateMany!]
}
input UserUpdateFollowersRelationUpdate {
data: FollowUpdateWithoutToInput
where: FollowUniqueFilter
}
input UserUpdateFollowersRelationUpdateMany {
data: FollowUpdateWithoutToInput
where: FollowWithoutToFilter
}
input UserUpdateFollowingRelation {
Expand All @@ -770,8 +820,18 @@ input UserUpdateFollowingRelation {
deleteMany: [FollowWithoutFromFilter!]
disconnect: [FollowUniqueFilter!]
set: [FollowUniqueFilter!]
update: [FollowUpdateWithoutFromInput!]
updateMany: [FollowWithoutFromFilter!]
update: [UserUpdateFollowingRelationUpdate!]
updateMany: [UserUpdateFollowingRelationUpdateMany!]
}
input UserUpdateFollowingRelationUpdate {
data: FollowUpdateWithoutFromInput
where: FollowUniqueFilter
}
input UserUpdateFollowingRelationUpdateMany {
data: FollowUpdateWithoutFromInput
where: FollowWithoutFromFilter
}
input UserUpdateInput {
Expand All @@ -793,8 +853,18 @@ input UserUpdateMediaRelation {
deleteMany: [MediaWithoutUploadedByFilter!]
disconnect: [MediaUniqueFilter!]
set: [MediaUniqueFilter!]
update: [MediaUpdateWithoutUploadedByInput!]
updateMany: [MediaWithoutUploadedByFilter!]
update: [UserUpdateMediaRelationUpdate!]
updateMany: [UserUpdateMediaRelationUpdateMany!]
}
input UserUpdateMediaRelationUpdate {
data: MediaUpdateWithoutUploadedByInput
where: MediaUniqueFilter
}
input UserUpdateMediaRelationUpdateMany {
data: MediaUpdateWithoutUploadedByInput
where: MediaWithoutUploadedByFilter
}
input UserUpdatePostsRelation {
Expand All @@ -804,8 +874,18 @@ input UserUpdatePostsRelation {
deleteMany: [PostWithoutAuthorFilter!]
disconnect: [PostUniqueFilter!]
set: [PostUniqueFilter!]
update: [PostUpdateWithoutAuthorInput!]
updateMany: [PostWithoutAuthorFilter!]
update: [UserUpdatePostsRelationUpdate!]
updateMany: [UserUpdatePostsRelationUpdateMany!]
}
input UserUpdatePostsRelationUpdate {
data: PostUpdateWithoutAuthorInput
where: PostUniqueFilter
}
input UserUpdatePostsRelationUpdateMany {
data: PostUpdateWithoutAuthorInput
where: PostWithoutAuthorFilter
}
input UserUpdateProfileRelation {
Expand Down
31 changes: 22 additions & 9 deletions packages/plugin-prisma-utils/tests/examples/crud/generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { BaseEnum, EnumRef, InputObjectRef, InputType, SchemaTypes } from '@pothos/core';
import {
BaseEnum,
EnumRef,
InputObjectRef,
InputType,
InputTypeParam,
SchemaTypes,
} from '@pothos/core';
import { getModel, PrismaModelTypes } from '@pothos/plugin-prisma';
import { FilterOps } from '../../../src';
import { Prisma } from '../../client';
Expand Down Expand Up @@ -188,7 +195,7 @@ export class PrismaCrudGenerator<Types extends SchemaTypes> {
return this.builder.prismaCreate(modelName, {
name: fullName,
fields: (t) => {
const fields: Record<string, InputType<Types>> = {};
const fields: Record<string, InputTypeParam<Types>> = {};
const withoutFields = model.fields.filter((field) => without?.includes(field.name));
const relationIds = model.fields.flatMap((field) => field.relationFromFields ?? []);

Expand Down Expand Up @@ -218,7 +225,7 @@ export class PrismaCrudGenerator<Types extends SchemaTypes> {
}

if (type) {
fields[field.name] = type;
fields[field.name] = field.isList && field.kind !== 'object' ? [type] : type;
}
});

Expand All @@ -243,10 +250,10 @@ export class PrismaCrudGenerator<Types extends SchemaTypes> {
const relatedModel = getModel(relationField.type, this.builder);
const relatedFieldName = relatedModel.fields.find(
(field) => field.relationName === relationField.relationName,
);
)!;

return {
create: this.getCreateInput(relatedModel.name as Name, [relatedFieldName!.name]),
create: this.getCreateInput(relatedModel.name as Name, [relatedFieldName.name]),
connect: this.getWhereUnique(relatedModel.name as Name),
};
},
Expand All @@ -266,7 +273,7 @@ export class PrismaCrudGenerator<Types extends SchemaTypes> {
return this.builder.prismaUpdate(modelName, {
name: fullName,
fields: () => {
const fields: Record<string, InputType<Types>> = {};
const fields: Record<string, InputTypeParam<Types>> = {};
const withoutFields = model.fields.filter((field) => without?.includes(field.name));
const relationIds = model.fields.flatMap((field) => field.relationFromFields ?? []);

Expand Down Expand Up @@ -296,7 +303,7 @@ export class PrismaCrudGenerator<Types extends SchemaTypes> {
}

if (type) {
fields[field.name] = type;
fields[field.name] = field.isList && field.kind !== 'object' ? [type] : type;
}
});

Expand Down Expand Up @@ -330,8 +337,14 @@ export class PrismaCrudGenerator<Types extends SchemaTypes> {
disconnect: this.getWhereUnique(relationField.type as Name),
delete: this.getWhereUnique(relationField.type as Name),
connect: this.getWhereUnique(relationField.type as Name),
update: this.getUpdateInput(relationField.type as Name, [relatedFieldName]),
updateMany: this.getWhere(relationField.type as Name, [relatedFieldName]),
update: {
where: this.getWhereUnique(relationField.type as Name),
data: this.getUpdateInput(relationField.type as Name, [relatedFieldName]),
},
updateMany: {
where: this.getWhere(relationField.type as Name, [relatedFieldName]),
data: this.getUpdateInput(relationField.type as Name, [relatedFieldName]),
},
deleteMany: this.getWhere(relationField.type as Name, [relatedFieldName]),
};
}
Expand Down

0 comments on commit 63547bf

Please sign in to comment.