profile
viewpoint

Ask questionsDefault values in default value of input types

Describe the Bug When defining a default value for an input type, its default values are not applied. I am not sure if its a problem of type-graphql or graphql-js.

To Reproduce

@InputType()
class MyInput {
  @Field(() => Int, { defaultValue: 30 })
  public take!: number;
}
@Resolver()
class MyResolver {
  @Query(() => Boolean)
  public myQuery(@Arg('arg', { defaultValue: {} }) arg: MyInput): boolean {
    console.log(arg);
    return true;
  }
}
{
  myQuery
}

with this query the log output is {}

Expected Behavior the log output should be {take:30}

Environment (please complete the following information):

  • Docker: node:12-alpine
  • Package version: 0.18.0-beta.17
  • TypeScript version: 3.8.3
MichalLytek/type-graphql

Answer questions backbone87

@croossin i solved it by reusing the default value of the "parent" input object:

const DEFAULT_FOO = { bar: 0 };

@InputType()
class FooInput {
  @Field({ defaultValue: DEFAULT_FOO.bar })
  public bar!: number;
}

@InputType()
class MyInput {
  @Field({ defaultValue: DEFAULT_FOO })
  public foo!: FooInput;
}
useful!
source:https://uonfu.com/
Github User Rank List