-
-
Notifications
You must be signed in to change notification settings - Fork 677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@Field({ defaultValue: 0 }) marks field as nullable #498
Comments
There's no technical possibility to detect if |
Cool. A warning about to this in the documentation would help. |
This creates a problem when sharing the same class for both How would that work without duplicating classes right now? The documentation specifies If |
It seems that setting a default value simply through @ObjectType()
@InputType('TestInput')
export class Test {
@Field()
public testField: number = 0;
} type Test {
testField: Float!
}
input TestInput {
testField: Float = 0
} In this case, what's the purpose of I still believe it is a bug that assining a value to it on an object field makes the field nullable. |
For many cases, I can't detect an initializer value, so you have to use a decorator option, like
Sharing body between input and output type is always a problem as you the behave differently.
I think that this line is resposible for this issue: if (!typeOptions.nullable && typeOptions.defaultValue === undefined) {
gqlType = new GraphQLNonNull(gqlType);
} The current pipeline doesn't allow to easily distinguish between input and output types on generating nullable type, so for now use the property initializer approach as a workaround and I will fix it in vNext 😉 |
I assumed extracting the initializer value wouldn't work for But since this is a different decorator, maybe |
Removing this won't allow you to "reuse" the same class as output type and in other parts of the app, as you may don't want to have |
I just noticed a different issue now with this. @Field()
public testField: number = 4; When returning an object from a resolver using the above signature, if the field is undefined, it won't be initialized to the value of 4 and it will pass through undefined, resulting in Using So there's definitely some inconsistency here, if field initializers are supposed to be supported. Is this undocumented behavior that I shouldn't rely on? |
Default values for object types are not supported and not tested, so I can't help you with that right now as it's a kinda undocumented feature. I have no idea how If you want, you can create a failing test cases for that, then apply a fix and then create a PR for that. |
@andreialecu FieldResolver is what you want to go for |
I ended up with creating a middleware to inject the default value. import { UseMiddleware, MiddlewareFn } from 'type-graphql'
function DefaultValue<T>(defaultValue: T): MiddlewareFn {
return async (_, next) => {
const original = await next()
if (original === undefined || original === null) {
return defaultValue
}
return original
}
}
@ObjectType()
export class Test1 {
@UseMiddleware(DefaultValue(0))
public testField: number;
} I also tried Custom decorators ( |
On the behavior regarding argument lists and input types: graphql/graphql-spec#570 Lets a asume a string argument or input field:
since this issue also talks about output types: should i create a new issue to track the nullable behavior for input types/arguments? |
Closing as should be fixed by #806 🔒 |
Describe the bug
So I'm not sure if this is a bug or I'm simply doing something wrong, but it seems confusing.
I found
defaultValue
through intellisense on@Field
and I'm using it on@ObjectType()
.Setting a
defaultValue
on a field makes the field nullable.I couldn't find any documentation regarding
defaultValue
other than for@Args()
.To Reproduce
Generates the following schema:
Additionally, trying
@Field({ defaultValue: 0, nullable: false })
, results in the following error:Expected behavior
The schema should have the field marked as required:
Enviorment (please complete the following information):
The text was updated successfully, but these errors were encountered: