Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
fix(args): stop creating properties for nullable args fields
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalLytek committed Jan 6, 2020
1 parent 9be7837 commit fe6b306
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/resolvers/convert-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,11 @@ export function convertArgsToInstance(argsMetadata: ArgsParamMetadata, args: Arg

const transformedFields = argsFields.reduce<Record<string, any>>((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;
}, {});

Expand Down
28 changes: 28 additions & 0 deletions tests/functional/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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 })
Expand Down

0 comments on commit fe6b306

Please sign in to comment.