MichalLytek / typegraphql-prisma

Prisma generator to emit TypeGraphQL types and CRUD resolvers from your Prisma schema
https://prisma.typegraphql.com
MIT License
891 stars 113 forks source link

mismatches between generated typescript and typescript prisma (_count and cursor) #468

Open simonjoom opened 2 months ago

simonjoom commented 2 months ago

I found the issue when i started to create a custom resolver

my custom resolver use prisma from context attached to the generated typescript prismaClient To see the typescript mismatches from any relationresolver.ts file output: you can change getPrismaFromContext(ctx). to (getPrismaFromContext(ctx) as PrismaClient).

The original code use ctx.prisma in any so typescript is not working here

_count is not supported by the current generated typescript (completly different from the ts prisma code ) also cursor is not same (and need to be overriden to any )

my file ListingRelationsResolver.ts with error typescript on return

@TypeGraphQL.Resolver(_of => Listing)
export class ListingRelationsResolver {
  @TypeGraphQL.FieldResolver(_type => Category, {
    nullable: false
  })
  async category(@TypeGraphQL.Root() listing: Listing, @TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Info() info: GraphQLResolveInfo): Promise<Category> {
    const { _count } = transformInfoIntoPrismaArgs(info);
    return (getPrismaFromContext(ctx) as PrismaClient).listing.findUniqueOrThrow({
      where: {
        id: listing.id,
      },
    }).category({
      ...(_count && transformCountFieldIntoSelectRelationsCount(_count)),
    });
  }

}

204

So i use Promise<Omit<Category,"_count"> as a turnaround for the type output of this resolver.

for me this change turnaround typescript problems, also i tested _count and it s still working From there it seems there is still some improvement todo to set the code fully compatible with prisma.

simonjoom commented 2 months ago

Ok i found better solution than the turnaround for _count the generator created in ListingCount a function getFreelancers that do not exist in prisma

@TypeGraphQL.ObjectType("ListingCount", {})
export class ListingCount {
  freelancers!: number;

  @TypeGraphQL.Field(_type => TypeGraphQL.Int, {
    name: "freelancers",
    nullable: false
  })
  getFreelancers?(@TypeGraphQL.Root() root: ListingCount, @TypeGraphQL.Args() args: ListingCountFreelancersArgs): number {
    return root.freelancers;
  }
}

just adding ? after the function will allow typescript to omit this check

Also today i found an other bug that i also resolved in code :blush: some change that can be done in the generator for upsert definitions

The current generator give:


@TypeGraphQL.InputType("CategoryUpsertWithoutListingsInput", {})
export class CategoryUpsertWithoutListingsInput {
  @TypeGraphQL.Field(_type => CategoryUpdateWithoutListingsInput, {
    nullable: false
  })
  update!: CategoryUpdateWithoutListingsInput;

  @TypeGraphQL.Field(_type => CategoryCreateWithoutListingsInput, {
    nullable: false
  })
  create!: CategoryCreateWithoutListingsInput;

  @TypeGraphQL.Field(_type => CategoryWhereInput , {
    nullable: true
  })
  where?: CategoryWhereInput | undefined;
}

instead it should to give


@TypeGraphQL.InputType("CategoryUpsertWithoutListingsInput", {})
export class CategoryUpsertWithoutListingsInput {
  @TypeGraphQL.Field(_type => CategoryUpdateWithoutListingsInput, {
    nullable: false
  })
  update!: CategoryUpdateWithoutListingsInput;

  @TypeGraphQL.Field(_type => CategoryCreateWithoutListingsInput, {
    nullable: false
  })
  create!: CategoryCreateWithoutListingsInput;

  @TypeGraphQL.Field(_type => CategoryWhereUniqueInput , {
    nullable: true
  })
  where: CategoryWhereUniqueInput | undefined;
}

nota:

where is required a WhereInput have to be change to a WhereUniqueInput

and then no complain with typescript about upsert

Also in where we can even add Prisma.AtLeast to be even more near Prisma signature:

where: Prisma.AtLeast<CategoryWhereUniqueInput,"id"|"name"> | undefined; and let inside file CategoryWhereUniqueInput id and name as optional with '?' to avoid an other complain

simonjoom commented 2 months ago

sorry i misclosed this issue... just reopen it for the owner of the lib