Skip to content

Rebuilding schema results in failure due to default value analysis #787

@oliversalzburg

Description

@oliversalzburg

Describe the Bug
When buildSchema has been called once, future calls result in an exception being thrown due to default value conflicts. Examples of the error are:

The 'created' field of 'ArchiveInput' has conflicting default values. Default value from decorator ('Fri Feb 19 2021 10:58:17 GMT+0100 (Central European Standard Time)') is not equal to the property initializer value ('Fri Feb 19 2021 10:58:27 GMT+0100 (Central European Standard Time)').

The 'cordova' field of 'SupportArchiveInput' has conflicting default values. Default value from decorator ('[object Object]') is not equal to the property initializer value ('[object Object]').

To Reproduce
The root cause seems to be a = new Date() initializer in the target class. As a new instance of the class is constructed during schema generation, to retrieve default values from the class instance, the second call has a more recent timestamp.

@InputType("ArchiveInput")
@ObjectType()
export class Archive {
	@Field(is => Date)
	created = new Date();
}

This also affects other cases where a complex default object is assigned with x = new Complex() in the class.

I created a full repro at https://github.com/oliversalzburg/double-build-repro

Expected Behavior
buildSchema calls should be idempotent.

Additionally, assigning a default value in the graph through this mechanism wasn't intentional. It was purely meant as a measure to initialize locally constructed instances of that class.

Environment (please complete the following information):

  • OS: Windows 10
  • Node v14.15.4
  • Package version 1.1.1
  • TypeScript version 4.1.5

Additional Context

const inputInstance = new (inputType.target as any)();

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions