type-graphql
type-graphql copied to clipboard
InputType returns nullable fields as undefined
Describe the Bug I'm using type-graphql v1.1.1 and if no value is passed to a nullable field on an InputType, it gets undefined. It's the same bug described in #475.
To Reproduce
@InputType()
export class GetMessageFilters {
@Field({ nullable: true })
_id?: number;
@Field({ nullable: true })
authorId?: number;
@Field({ nullable: true })
member?: number;
}
@Query(() => Message)
async GetMessage(
@Arg('filters') filters: GetMessageFilters,
@Arg('options', { nullable: true }) options?: MessageQueryOptions
): Promise<Message> {
console.log(filters); // { _id: undefined, authorId: undefined, member: undefined }
}
Expected Behavior
Nullable fields should not be returned: filters === {}
Environment (please complete the following information):
- OS: Manjaro Linux
- Node v16.11.0
- Package version 1.1.1
- TypeScript version 4.4.4
So if it's related to #475 and the test case expect(mutationInputValue).not.toHaveProperty("optionalNestedField");
passes, it means it works.
So please create a PR with a failing test case that uses your conditions.
I ran tests with my code and they don't fail but somehow when I use my api I get those properties as undefined, I don't know what's going on :/
@MichalLytek I found that the problem is related to Babel, I created a fresh project with Next.js and it worked fine until I had to enable decorators for Next in my .babelrc
file:
{
"presets": ["next/babel"],
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }]
]
}
After adding that type-graphql
started to return undefined
values in nullable fields
Any recommendation? Thanks in advance
@MichalLytek The bug causes by these codes:
https://github.com/MichalLytek/type-graphql/blob/8f6c0db3851134f265e52a444bbcd1b680a03770/src/resolvers/convert-args.ts#L106
https://github.com/MichalLytek/type-graphql/blob/8f6c0db3851134f265e52a444bbcd1b680a03770/src/helpers/types.ts#L98
new Target()
will create some undefined properties
@MichalLytek I found that the problem is related to Babel, I created a fresh project with Next.js and it worked fine until I had to enable decorators for Next in my
.babelrc
file:{ "presets": ["next/babel"], "plugins": [ ["@babel/plugin-proposal-decorators", { "legacy": true }] ] }
After adding that
type-graphql
started to returnundefined
values in nullable fields
It does not related to Babel
Hi @forsigner, did you ever fix this?
@MichalLytek
Just for context- I was running 1.2.0.rc1
with no issues. Now I am seeing this exact same issue reported with 2.0.0-beta.2
regarding undefined nullable fields when using the @InputType()
decorator.
@nigel-loops Please create PR with a failing test case or create a minimal reproducible code example repository 😉
@MichalLytek ;) I did. I found the issue and I am going to share this just incase it is helpful in the future. Below is the codesandbox:
https://codesandbox.io/p/sandbox/zealous-black-4fr74r?file=%2Fpackage.json%3A12%2C28-12%2C29
In my current project my tsconfig.json
target was set to ES2022
. However, setting the target back to es2018
resolved these undefined nullable fields being included in my object with in my @InputType()
if you run the above codesandbox and execute the following graphql request in the apollo playground:
Operation
mutation AddBook($newBookData: NewBookInput!) {
addBook(newBookData: $newBookData) {
author
title
}
}
Variables
{
"newBookData": {
"title": "this is a title"
}
}
When you run the above command, even though author
wasnt supplied in the newBookData
payload, the inputData shows author
as undefined. When changing the target in tsconfig.json
to es2018
it fixes it. Any idea why?
@nigel-loops I use "type-graphql": "^2.0.0-beta.1"
now
When changing the target in tsconfig.json to es2018 it fixes it. Any idea why?
@nigel-loops That's because with targeting newest es version, it now emits proper class properties and other not transpiled syntax.
I can confirm that with "target": "esnext"
the tests are failing:
So please setup your project according to the docs and use es2019
target:
https://typegraphql.com/docs/next/installation.html
es2021
will be possible after merging #1400 😉
Ok great. That makes perfect sense. Thanks for the update.