MichalLytek / type-graphql

Create GraphQL schema and resolvers with TypeScript, using classes and decorators!

Home Page:https://typegraphql.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inherited ObjectTypes don't like Object.assign

z0d14c opened this issue · comments

Describe the Bug
I have some @ObjectType-s that inherit from an @InterfaceType. They aren't particularly complicated but recently something broke and I'm not sure why (I inherited this code but the implementation seems simple enough). I am not sure if I am missing some basic javascript behavior here, but it seemed to be working the other day and then suddenly stopped.

To Reproduce
A quick guide how to reproduce the bug.
You can paste here code snippets or even better, provide a link to the repository with minimal reproducible code example.

abstract class A {
  constructor(params) {
   Object.assign(this, params)
  }
}

class B extends A {
  constructor(params) {
    super(params)
    // console.log(this) here will show params don't get assigned, e.g. params.value did not get assigned to this.value
  }
}

If I put that logic in a separate function on the base class and call it, e.g. applyParams(params) and then call that in the descendant class after super(), everything works fine and dandy.

Expected Behavior
I expect params to be applied to the descendant object

Logs

Environment (please complete the following information):

  • OS: MacOS
  • Node 16.2.0
  • Package version 1.1.1
  • TypeScript version 4.9.5

Additional Context
This happened after a recent yarn install so I suppose that is likely the culprit but it still doesn't make too much sense to me why it would happen, unless something in type-graphql

can you find some point in your git history which your code works again? you could compare the difference between them.

you said that it happened after a yarn install. note that it is important to also checkout yarn.lock to track all dependency changes.

TypeGraphQL does not support constructor parameters.
The classes are more like DTOs, not a domain models.

In order to create instances of that classes (to make getters and field resolvers work), TypeGraphQL itself creates empty instances of the class (without providing args to constructor) and assign the data from plain js object (returned by other resolvers or from execution args).

To avoid issues, it's recommended to avoid constructor parameters.
I don't see any issue regarding TypeGraphQL here, so closing this one 🔒