Type-graphql: Cannot read property 'fields' of undefined

Created on 27 Dec 2018  路  3Comments  路  Source: MichalLytek/type-graphql

Describe the bug
buildSchema() throw a "Uncaught (in promise) TypeError: Cannot read property 'fields' of undefined" that is raised from src/metadata/metadata-storage.ts (line 185).
In my case, fields is undefined, maybe because objectType is not what it should be in the objectType.fields!.find() call.

To Reproduce
Use a class decorator "@Resolver()" with a function as first parameter instead of just a Class (ie: "@Resolver(of => Info)" instead of "@Resolver(Info)"
AND
Have any calculated field with a "@FieldResolver()"

For example, this code cause the problem:

import { Service } from 'typedi'
import { Resolver, Query, Arg, FieldResolver, Root } from 'type-graphql'

import { State, StateService } from '../State'
import { Info, InfoService } from '../Info'


@Service()
@Resolver(of => State)
export default class StateResolver {
    constructor (
        private readonly service: StateService,
        private readonly infoService: InfoService
    ) {
    }

    @Query(returns => State, { nullable: true })
    public async stateByID (@Arg("id") id: string): Promise<null | State> {
        return this.service.get(id) || null
    }

    @Query(returns => [State])
    public async states (
        @Arg("offset", { defaultValue: 0 }) offset: number = 0,
        @Arg("limit", { nullable: true }) limit?: number
    ): Promise<Array<State>> {
        return this.service.list(offset, limit || Infinity)
    }

    @FieldResolver()
    public async lastInfo (@Root() state: State): Promise<null | Info> {
        return this.infoService.get(state.lastInfosID) || null
    }

}

Expected behavior
No exception raised ... by having a non undefined .fields! property on objectType

Logs
Here is the exception log plus some variable spies (on objectTypeCls and objectTypeCls(), from line 180 of the TS source code)

19:10:44.363 /home/tongtwist/Documents/devel/giteries/myProject/node_modules/type-graphql/metadata/metadata-storage.js:133 !!! objectTypeCls: 茠 (of) {
    return _Info.Info;
}
19:10:44.365 /home/tongtwist/Documents/devel/giteries/myProject/node_modules/type-graphql/metadata/metadata-storage.js:134 !!! objectTypeCls(): 茠 Info() {
    (0, _classCallCheck3.default)(this, Info);
}
19:10:44.398 /home/tongtwist/Documents/devel/giteries/myProject/node_modules/type-graphql/metadata/metadata-storage.js:141 Uncaught (in promise) TypeError: Cannot read property 'fields' of undefined
    at definitions.forEach.def (/home/tongtwist/Documents/devel/giteries/myProject/node_modules/type-graphql/metadata/metadata-storage.js:141)
    at Array.forEach (<anonymous>)
    at MetadataStorage.buildFieldResolverMetadata (/home/tongtwist/Documents/devel/giteries/myProject/node_modules/type-graphql/metadata/metadata-storage.js:124)
    at MetadataStorage.build (/home/tongtwist/Documents/devel/giteries/myProject/node_modules/type-graphql/metadata/metadata-storage.js:77)
    at Function.generateFromMetadataSync (/home/tongtwist/Documents/devel/giteries/myProject/node_modules/type-graphql/schema/schema-generator.js:26)
    at Function.<anonymous> (/home/tongtwist/Documents/devel/giteries/myProject/node_modules/type-graphql/schema/schema-generator.js:15)
    at Generator.next (<anonymous>)
    at /home/tongtwist/Documents/devel/giteries/myProject/node_modules/tslib/tslib.js:107
    at new Promise (<anonymous>)
    at Object.__awaiter (/home/tongtwist/Documents/devel/giteries/myProject/node_modules/tslib/tslib.js:103)
definitions.forEach.def @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/type-graphql/metadata/metadata-storage.js:141
buildFieldResolverMetadata @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/type-graphql/metadata/metadata-storage.js:124
build @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/type-graphql/metadata/metadata-storage.js:77
generateFromMetadataSync @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/type-graphql/schema/schema-generator.js:26
(anonymous) @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/type-graphql/schema/schema-generator.js:15
(anonymous) @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/tslib/tslib.js:107
__awaiter @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/tslib/tslib.js:103
generateFromMetadata @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/type-graphql/schema/schema-generator.js:14
(anonymous) @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/type-graphql/utils/buildSchema.js:10
(anonymous) @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/tslib/tslib.js:107
__awaiter @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/tslib/tslib.js:103
buildSchema @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/type-graphql/utils/buildSchema.js:8
_callee$ @ runner.js:8
tryCatch @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:62
invoke @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:296
prototype.(anonymous function) @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:114
step @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/babel-runtime/helpers/asyncToGenerator.js:17
(anonymous) @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/babel-runtime/helpers/asyncToGenerator.js:35
F @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/core-js/library/modules/_export.js:36
(anonymous) @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/babel-runtime/helpers/asyncToGenerator.js:14
bootstrap @ /home/tongtwist/Documents/devel/giteries/myProject/libraries/dinoo-app/dinoo/lib/runner.js:40
(anonymous) @ runner.js:14
(anonymous) @ runner.js:14
Module._compile @ module.js:642
Module._extensions..js @ module.js:653
Module.load @ module.js:561
tryModuleLoad @ module.js:504
Module._load @ module.js:496
Module.require @ module.js:586
require @ internal/module.js:11
(anonymous) @ index.html:13
Promise rejected (async)
step @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/babel-runtime/helpers/asyncToGenerator.js:27
(anonymous) @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/babel-runtime/helpers/asyncToGenerator.js:35
F @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/core-js/library/modules/_export.js:36
(anonymous) @ /home/tongtwist/Documents/devel/giteries/myProject/node_modules/babel-runtime/helpers/asyncToGenerator.js:14
bootstrap @ /home/tongtwist/Documents/devel/giteries/myProject/libraries/dinoo-app/dinoo/lib/runner.js:40
(anonymous) @ runner.js:14
(anonymous) @ runner.js:14
Module._compile @ module.js:642
Module._extensions..js @ module.js:653
Module.load @ module.js:561
tryModuleLoad @ module.js:504
Module._load @ module.js:496
Module.require @ module.js:586
require @ internal/module.js:11
(anonymous) @ index.html:13

Enviroment

  • OS: Linux
  • Node 8.13.0 (from Electron 2.0.13)
  • Package version 0.16.0
  • TypeScript version 3.2.2

Additional context
The given example code is executed in a "Electron main process" (the NodeJS part) and not in a "renderer process" (the browser part)

ed
In my environment, on line 183 of src/metadata/metadata-storage.ts, the test function comparaison compares this value:

茠 Info() {
    (0, _classCallCheck3.default)(this, Info);
}

with this value:

茠 (of) {
    return _Info.Info;
}

in a case where these values should be equals.
But, if add function call parenthesis on objectTypeCls in this comparaison, I retrieve the wanted equality and all is working without error.

Question Solved

Most helpful comment

I bet that your problem has the same root as #206.

If changing target: es2016 won't work, please create a repository with a minimal reproducible code example 馃槈

All 3 comments

I bet that your problem has the same root as #206.

If changing target: es2016 won't work, please create a repository with a minimal reproducible code example 馃槈

It seems, I have the very same symptom as #206 But, I have target: esnext in my tsconfig file.

May my problem could come from a module: esnext? Unfortunately, due to some dependencies in a multi-target code, this parameter is mandatory for my project.

I'll come back ASAP with a small (Electron) repo that reproduce my problem.

My bad. You was right. :grimacing:

I have tsconfig file with good parameters, but I forgot that I have a second layer in my build chain where BabelJS take the javascript files produced by tsc and transpile them to es5 :mask:

So the good news, is that I realize I can simplify my build chain by removing BabelJS :tada:

Sorry for this fake issue, and good job for this lib!
:stuck_out_tongue_winking_eye:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tafelito picture tafelito  路  3Comments

Tybot204 picture Tybot204  路  3Comments

robertchung97 picture robertchung97  路  3Comments

MichalLytek picture MichalLytek  路  4Comments

limenutt picture limenutt  路  3Comments