prisma-nestjs-graphql icon indicating copy to clipboard operation
prisma-nestjs-graphql copied to clipboard

Input is empty when using generated InputType

Open matheins opened this issue 4 years ago • 7 comments
trafficstars

Its a bit confusing to me but when using the generated input types im not able to get args from my graphql mutations. When using my own dto as described in the nestjs docs everything works as expected.

My resolver:

@Mutation(() => Business, { nullable: true })
  async createBusiness(
    @UserEntity() user: User,
    @Args('business') business: BusinessCreateInput,
  ) {
    console.log(business); // output: BusinessCreateInput {}
    return this.businessesService.create(business, user);
  }

My graphql mutation

mutation CreateBusiness{
  createBusiness(business: { name: "Test business" })
}

Any ideas why this happens when using the generated types?

matheins avatar Nov 16 '21 15:11 matheins

What is your prisma schema, what version of nestjs and library are you using?

unlight avatar Nov 16 '21 19:11 unlight

Prisma schema:

model Business {
  id               String           @id @default(uuid())
  createdAt        DateTime         @default(now())
  updatedAt        DateTime         @updatedAt
  address          Address?         @relation("Address")
  billingAddress   Address?         @relation("BillingAddress")
  users            User[]
  name             String
  legalName        String
  moderationStatus ModerationStatus @default(PENDING)
  vatId            String?
  legalForm        String?
  stripeId         String?
  websiteUrl       String?
  addressId        String?
  billingAddressId String?
}

Versions

nestjs/core: 8.0.6
prisma-nestjs-graphql: ^14.6.0

Generated InputType

@InputType()
export class BusinessCreateInput {

    @Field(() => String, {nullable:true})
    id?: string;

    @Field(() => Date, {nullable:true})
    createdAt?: Date | string;

    @Field(() => Date, {nullable:true})
    updatedAt?: Date | string;

    @Field(() => String, {nullable:false})
    name!: string;

    @Field(() => String, {nullable:false})
    legalName!: string;

    @Field(() => ModerationStatus, {nullable:true})
    moderationStatus?: keyof typeof ModerationStatus;

    @Field(() => String, {nullable:true})
    vatId?: string;

    @Field(() => String, {nullable:true})
    legalForm?: string;

    @Field(() => String, {nullable:true})
    stripeId?: string;

    @Field(() => String, {nullable:true})
    websiteUrl?: string;

    @Field(() => String, {nullable:true})
    addressId?: string;

    @Field(() => String, {nullable:true})
    billingAddressId?: string;

    @Field(() => AddressCreateNestedOneWithoutBusinessInput, {nullable:true})
    address?: AddressCreateNestedOneWithoutBusinessInput;

    @Field(() => AddressCreateNestedOneWithoutBillingAddressFromInput, {nullable:true})
    billingAddress?: AddressCreateNestedOneWithoutBillingAddressFromInput;

    @Field(() => UserCreateNestedManyWithoutBusinessInput, {nullable:true})
    users?: UserCreateNestedManyWithoutBusinessInput;
}

matheins avatar Nov 16 '21 20:11 matheins

Looks normal and it works for me. Are you using standard set of tools like tsc compiler, reflect-metadata, etc? Or non-standard, like swc/esbuild, lite reflection lib?

unlight avatar Nov 16 '21 20:11 unlight

Yes im using the standard set wich comes with nest out of the box

matheins avatar Nov 16 '21 21:11 matheins

Sorry, but I dont have any ideas why it's not working. Can you provide minimal repository to reproduce error?

unlight avatar Nov 16 '21 21:11 unlight

Ok I managed to debug the cause of this problem: I was using the ValidationPipe globally like this:

// main.ts
app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true,
      transform: true,
    }),
  );

The request was working with my usual dto's because im using class-validator on every field. None of the generated types has class-validators so none of the fields was able to go through. Deactivating the validation pipe fixes this problem for now.

Is there a way to use the generated types together with a global validation pipe? Im still using some manually created dtos with class-validation and it would be cool if I could still use them :-)

matheins avatar Nov 16 '21 21:11 matheins

I think problem in whitelist = true. But it does not make sense to use whitelist = true with graphql, graphql is typed. I think you have some options here:

  1. Disable whitelist, and use ValidationPipe in specific methods (decorator @UsePipes) or parameters where its needed (@Param, @Body)
  2. You can try to attach @Allow decorator to all fields (from class-validator, see https://github.com/typestack/class-validator#whitelisting) to all generated graphql classes, see decorate config option - https://github.com/unlight/prisma-nestjs-graphql#decorate

unlight avatar Nov 17 '21 08:11 unlight