Type-graphql: Unable to infer GraphQL type, only in test

Created on 27 Aug 2020  路  3Comments  路  Source: MichalLytek/type-graphql

Describe the Bug
Trying to unit test a simple resolver using Jest.

To Reproduce
Minimal repro

import 'reflect-metadata'
import { Resolver, Query, ObjectType, Field } from 'type-graphql'

describe('', () => {
    test('', () => {
        const sut = new SomeClassResolver()
        // ...
    })
})

@Resolver()
export class SomeClassResolver {
    @Query((returns) => SomeClass)
    async retrieveSomeClass(): Promise<SomeClass> {
        // ...
    }
}

@ObjectType()
export class SomeClass {
    @Field()
    someString!: string
}

Expected Behavior
The test to pass.

Logs

    Unable to infer GraphQL type from TypeScript reflection system. You need to provide explicit type for 'someString' of 'SomeClass' class.

       5 |  test('', () => {
       6 |      const sut = new SomeClassResolver()
    >  7 |  })
         |    ^
       8 | })
       9 |
      10 | @Resolver()

      at Object.findType (node_modules/type-graphql/dist/helpers/findType.js:17:15)
      at node_modules/type-graphql/dist/decorators/Field.js:16:53
      at DecorateProperty (node_modules/reflect-metadata/Reflect.js:553:33)
      at Object.decorate (node_modules/reflect-metadata/Reflect.js:123:24)
      at __decorate (src/server-graphql/some.test.ts:7:90)
      at Object.<anonymous> (src/server-graphql/some.test.ts:21:2)

Environment (please complete the following information):

  • MacOS 1.15.6
  • Node 12.18.3
  • Package version 1.0.0
  • typescript: 4.0.2
  • jest: 26.4.2
  • ts-jest: 26.3.0

Additional Context
If I provide the type again using () => String, the error goes away.

BUT the same resolver works perfectly when running the normal way in _apollo-server-express_. Do I need to add additional type information just for testing?

Question Solved

Most helpful comment

@MichalLytek Thanks for the pointer. I was able to solve it by comparing my setup with yours.

Turns out

"emitDecoratorMetadata": true,
"experimentalDecorators": true,

was missing from _compilerOptions_ in _jest.tsconfig.json_ (but present in _tsconfig.json_).

All 3 comments

I am experiencing the same issue, very annoying

@memark @leinonen

It must be something with your config as the tests in this repository works fine with the same Jest stack.
It's hard to say what's wrong with your config without seeing it.

So please share a repository with a reproducible code example - a minimal one, single test file, single case, one class, not a dump of half of your project.

@MichalLytek Thanks for the pointer. I was able to solve it by comparing my setup with yours.

Turns out

"emitDecoratorMetadata": true,
"experimentalDecorators": true,

was missing from _compilerOptions_ in _jest.tsconfig.json_ (but present in _tsconfig.json_).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tafelito picture tafelito  路  3Comments

Asim13se picture Asim13se  路  3Comments

MichalLytek picture MichalLytek  路  3Comments

glentakahashi picture glentakahashi  路  3Comments

MichalLytek picture MichalLytek  路  4Comments