Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
simonjoom opened this issue Sep 16, 2024 · 2 comments
Labels
bug Something isn't working community Something initiated by the community

Comments

@simonjoom
Copy link

simonjoom commented Sep 16, 2024

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.

@MichalLytek MichalLytek added bug Something isn't working community Something initiated by the community labels Sep 16, 2024
@simonjoom
Copy link
Author

simonjoom commented Sep 18, 2024

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 😊
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
Copy link
Author

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

@simonjoom simonjoom reopened this Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working community Something initiated by the community
Projects
None yet
Development

No branches or pull requests

2 participants