MichalLytek / MichalLytek/type-graphql

Base abstract class is referenced/instantiated when child class should be

Open
#1,492 2 comments 0 reactions 0 assignees View on GitHub
Community :family_man_girl: Need More Info :man_shrugging: Question :grey_question:
Dominant language
TypeScript
Stars
8.1k
Forks
672
PR merge metrics
No merged PRs in 30d

Description

**Describe the Bug**
Assume you have some type of union or polymorphic object as shown in the example. You may want to have an abstract value which can be set by any child/implementation. In many of the examples, this is shown as an abstract class with an `@ObjectType({ implements: BaseClass })` in children.

However, when it comes to actually running the methods, it seems like they are being executed on the BaseClass instead of the ChildClass. This means that concrete base class methods cannot reference or use abstract methods on the base class (since they are not instantiated).

While I haven't spelunked enough to understand exactly what's going on, my hunch is that typegraphql is calling BaseClass.value() instead of ChildClass.value(). This distinction would not matter normally, but comes into play when you truly start using abstraction and inheritance.

**To Reproduce**
https://codesandbox.io/p/sandbox/typegraphql-v2-demo-forked-wkyhwm?file=/src/index.ts:106,33

**Expected Behavior**
The function as shown should work and return the value from the child class

**Logs**
```
"TypeError: this._publicationDate is not a function",
" at BaseItem.publicationDate (/project/sandbox/src/index.ts:30:17)",
" at /project/sandbox/node_modules/type-graphql/dist/resolvers/create.js:64:45",
" at applyMiddlewares (/project/sandbox/node_modules/type-graphql/dist/resolvers/helpers.js:59:16)",
" at /project/sandbox/node_modules/type-graphql/dist/resolvers/create.js:52:47",
" at field.resolve (/project/sandbox/node_modules/@apollo/server/src/utils/schemaInstrumentation.ts:82:22)",
" at executeField (/project/sandbox/node_modules/graphql/execution/execute.js:492:20)",
" at executeFields (/project/sandbox/node_modules/graphql/execution/execute.js:414:22)",
" at completeObjectValue (/project/sandbox/node_modules/graphql/execution/execute.js:925:10)",
" at completeValue (/project/sandbox/node_modules/graphql/execution/execute.js:646:12)",
" at completeValue (/project/sandbox/node_modules/graphql/execution/execute.js:595:23)"
```

**Environment (please complete the following information):**

- OS: all
- Node 16
- Package version - v2
- TypeScript version - 4.7

Contributor guide

Open the contributing guide

Research direction

Start with the linked CodeSandbox reproduction in src/index.ts and trace the resolver path shown in type-graphql/dist/resolvers/create.js and resolvers/helpers.js. Confirm why the base class instance handles the field, then verify that the child implementation supplies the value and the example returns it without the TypeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, node.js, typescript
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.