From fe6b3062d99e3544f6a95d44484b990f64b35abe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Lytek?= Date: Mon, 6 Jan 2020 16:09:59 +0100 Subject: [PATCH] fix(args): stop creating properties for nullable args fields --- src/resolvers/convert-args.ts | 7 +++++-- tests/functional/resolvers.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/resolvers/convert-args.ts b/src/resolvers/convert-args.ts index 00ac52952..f848e0eee 100644 --- a/src/resolvers/convert-args.ts +++ b/src/resolvers/convert-args.ts @@ -126,8 +126,11 @@ export function convertArgsToInstance(argsMetadata: ArgsParamMetadata, args: Arg const transformedFields = argsFields.reduce>((fields, field) => { const fieldValue = args[field.name]; - const fieldTarget = field.getType(); - fields[field.name] = convertValuesToInstances(fieldTarget, fieldValue); + // don't create property for nullable field + if (fieldValue !== undefined) { + const fieldTarget = field.getType(); + fields[field.name] = convertValuesToInstances(fieldTarget, fieldValue); + } return fields; }, {}); diff --git a/tests/functional/resolvers.ts b/tests/functional/resolvers.ts index 7f5865283..f4a568e4e 100644 --- a/tests/functional/resolvers.ts +++ b/tests/functional/resolvers.ts @@ -1152,6 +1152,16 @@ describe("Resolvers", () => { } classes.SampleArgs = SampleArgs; + @ArgsType() + class SampleOptionalArgs { + @Field() + stringField: string; + + @Field({ nullable: true }) + optionalField?: string; + } + classes.SampleOptionalArgs = SampleOptionalArgs; + @InputType() class SampleInput { private readonly TRUE = true; @@ -1300,6 +1310,12 @@ describe("Resolvers", () => { } } + @Mutation() + mutationWithOptionalArgs(@Args() args: SampleOptionalArgs): boolean { + mutationInputValue = args; + return true; + } + @Mutation() mutationWithInput(@Arg("input") input: SampleInput): number { if (input.isTrue()) { @@ -1553,6 +1569,18 @@ describe("Resolvers", () => { expect(result).toBeLessThanOrEqual(10); }); + it("shouldn't create properties of nullable args", async () => { + const mutation = `mutation { + mutationWithOptionalArgs(stringField: "stringField") + }`; + + const { errors } = await graphql(schema, mutation); + + expect(errors).toBeUndefined(); + expect(mutationInputValue).toBeInstanceOf(classes.SampleOptionalArgs); + expect(mutationInputValue).not.toHaveProperty("optionalField"); + }); + it("should create instance of input object", async () => { const mutation = `mutation { mutationWithInput(input: { factor: 10 })