#I have one issue to use @InputType of nestjs/graphql

15 messages · Page 1 of 1 (latest)

mystic jay
#

I defined the inputType like this.

@InputType()
export class CreateTaskInput {
@Field()
userId: string;

@Field()
name: string;
}

.... resolver file
@Mutation({name: "createTask"})
async createTask(
@Args("taskPayload") taskPayload: CreateTaskInput
) {
  console.log(JSON.stringify(taskPayload), taskPayload.userId); // {} undefined
}

---
mutation CreateTask($payload) {
 createTask(taskPayload: $payload)
}

... CreateTask({taskPayload: { userId: "Test", name: "test" }})

How can I resolve this undefined issue?

sleek lance
#

Are you using the ValidationPipe?

mystic jay
#

Yes

sleek lance
#

Do you have whitelist: true set in its options?

mystic jay
#

Yes

app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true,
    }),
  );
sleek lance
#

That's the issue then, as that will strip any unknon properties

#

And by unknown, I mean unknown to class-validator (not decorated with a c-v decorator)

#

If you don't need validation and just need the property, use @Allow()

mystic jay
#

Where should I add @Allow()? 🙂

sleek lance
#

On each property you want to allow witthout validation

mystic jay
#

Awesome!

#

Let me try to add it to each property

sleek lance
#

This is class-validator's functionality, and you should refer to their documentation for it

mystic jay
#

Wow. it is working

#

Thank you @sleek lance