Skip to content
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

Default values in default value of input types #615

Closed
backbone87 opened this issue Apr 25, 2020 · 4 comments
Closed

Default values in default value of input types #615

backbone87 opened this issue Apr 25, 2020 · 4 comments
Labels
Community 👨‍👧 Something initiated by a community Invalid 👎 This doesn't seem right

Comments

@backbone87
Copy link

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
Copy link
Owner

MichalLytek commented Apr 25, 2020

Yes, this is a graphql-js behavior - defaults are not working in cascade.

To reproduce:

import {
  GraphQLObjectType,
  GraphQLString,
  GraphQLInputObjectType,
  GraphQLSchema,
  printSchema,
  graphql,
} from "graphql";

const Query = new GraphQLObjectType({
  name: "Query",
  fields: () => ({
    sampleQuery: {
      type: GraphQLString,
      args: {
        sampleArgs: {
          type: SampleInput,
          defaultValue: {},
        },
      },
      resolve: (_, args) => {
        console.log(args);
        return "sampleQuery";
      },
    },
  }),
});

const SampleInput = new GraphQLInputObjectType({
  name: "SampleInput",
  fields: {
    sampleField: {
      type: GraphQLString,
      defaultValue: "default",
    },
  },
});

const schema = new GraphQLSchema({
  types: [SampleInput],
  query: Query,
});

console.log(printSchema(schema));

const query = /* graphql */ `
  query {
    sampleQuery
  }
`;

graphql(schema, query).then(console.log);
type Query {
  sampleQuery(sampleArgs: SampleInput = {}): String 
}

input SampleInput {
  sampleField: String = "default"
}

{ sampleArgs: {} }
{ data: [Object: null prototype] { sampleQuery: 'sampleQuery' } }

Please open an issue in theirs repository or against GraphQL Spec 🔒

@MichalLytek MichalLytek added Community 👨‍👧 Something initiated by a community Invalid 👎 This doesn't seem right labels Apr 25, 2020
@croossin
Copy link

@backbone87 Did you ever figure out a solution for this? Or open an issue in their repo?

@backbone87
Copy link
Author

backbone87 commented Nov 18, 2020

@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;
}

@MechJosh0
Copy link

MechJosh0 commented Jan 14, 2021

There is an open issue for it on Graphql-js, and a Graphql RFC.

cc @croossin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community 👨‍👧 Something initiated by a community Invalid 👎 This doesn't seem right
Projects
None yet
Development

No branches or pull requests

4 participants