Graphql: Support @Resolver without type name or class (code first)

Created on 18 Mar 2019  ·  12Comments  ·  Source: nestjs/graphql

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior


Application throw the following error when using @Resolver() without specifying any type name or type class:

  TypeError: Cannot read property 'prototype' of undefined

Caused by: https://github.com/nestjs/graphql/blob/master/lib/decorators/resolvers.decorators.ts#L250

Expected behavior


No error should be thrown.

Minimal reproduction of the problem with instructions


Sorry for my poor english, here is a test case to reproduce the issue: https://github.com/zhenwenc/nest-graphql-issues/blob/master/test/resolver-without-type-name.spec.ts

Environment


Nest version: 6.0.0

Most helpful comment

@zhenwenc I have happen to stumble upon the very same issue when using @ResolveProperty() decorator and what solved for me was to properly annotate the Resolver as per docs using Type not string name so for example in your case:

@Resolver('SampleObject')
@Resolver(of => SampleObject)

So in your test simply change the annotation as specified above and all should work as expected.

Hope it helps!
Tom

All 12 comments

Same issue with TypeError: Cannot read property 'prototype' of undefined

Using @Resolver() decorator without any params can throw this error.

So actually this is a typing issue. @Resolver() without parameters should never be used.

Hmm.. I think it's a valid case though. If I understand it correctly, the type for a resolver is only needed for FieldResolvers (or PropertyResolver).

Well, you're right. Thanks for reminding me :)

It should be fixed in 6.0.1 :) Let me know if you face any issue

@kamilmysliwiec Thanks a lot for the quick fix!

But my test case is still failing... :(

  ● should support @Resolver without specifying type name

    Generating schema error

      at Function.<anonymous> (node_modules/type-graphql/dist/schema/schema-generator.js:19:23)
      at fulfilled (node_modules/tslib/tslib.js:104:62)

I suspect that it's caused by this check, so that the queries defined in @Resolver without any params won't be included in the generated GraphQL schema, this check does make sense in "schema first" approach, but maybe not for "code first".

_(unrelated to this issue)_ And I also noticed that running the test with @nestjs/graphql is significantly slower than using type-graphql:

@nestjs/graphql:  ~5s
type-graphql:     ~1s

I found some other issues in @ResolveProperty:

There are multiple parameters accepts the custom defined property name, which is kinda confusing:

  @Resolver('SampleObject')
  class SampleResolver {
    @ResolveProperty('duplicatedNameField', () => String, {
      name: 'modifiedName',
    })
    foo(): string {
      return `What's my name?`;
    }
  }

Again, here is the test case to reproduce the issue: https://github.com/zhenwenc/nest-graphql-issues/blob/master/test/duplicate-resolve-property-name.spec.ts

Additionally, my test case is failing with another error:

    TypeError: Cannot read property 'getObjectType' of undefined

      at definitions.forEach.def (node_modules/type-graphql/dist/metadata/metadata-storage.js:126:92)
          at Array.forEach (<anonymous>)
      at MetadataStorage.buildFieldResolverMetadata (node_modules/type-graphql/dist/metadata/metadata-storage.js:122:21)
      at MetadataStorage.build (node_modules/type-graphql/dist/metadata/metadata-storage.js:77:14)
      at Function.generateFromMetadataSync (node_modules/type-graphql/dist/schema/schema-generator.js:27:51)
      at Function.<anonymous> (node_modules/type-graphql/dist/schema/schema-generator.js:16:33)
      at node_modules/tslib/tslib.js:107:75
      at Object.__awaiter (node_modules/tslib/tslib.js:103:16)

I think the test case demonstrates a valid use case, you could switch to type-graphql (by uncommenting the type-graphql lines), the test will pass.

Experiencing the same issue in my project. Wish to return a description for my GraphQL API. So, have a resolver:

import { Resolver, Query } from '@nestjs/graphql';

@Resolver()
export class InfoResolver {
  @Query()
  info(): string {
    return ``;
  }
}

When building my project, I'm getting Generating schema error, UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch()..

@zhenwenc I have happen to stumble upon the very same issue when using @ResolveProperty() decorator and what solved for me was to properly annotate the Resolver as per docs using Type not string name so for example in your case:

@Resolver('SampleObject')
@Resolver(of => SampleObject)

So in your test simply change the annotation as specified above and all should work as expected.

Hope it helps!
Tom

@id-kemo I also found that Resolver(SampleObject) works (without the function) and it won't trigger the TS7006: Parameter 'of' implicitly has an 'any' type. if you have strict mode enabled in TypeScript.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings