Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support @Resolver without type name or class (code first) #158

Closed
zhenwenc opened this issue Mar 18, 2019 · 12 comments
Closed

Support @Resolver without type name or class (code first) #158

zhenwenc opened this issue Mar 18, 2019 · 12 comments

Comments

@zhenwenc
Copy link

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

@dzzzzzy
Copy link

dzzzzzy commented Mar 18, 2019

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

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

@kamilmysliwiec
Copy link
Member

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

@zhenwenc
Copy link
Author

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).

@kamilmysliwiec
Copy link
Member

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

@kamilmysliwiec
Copy link
Member

kamilmysliwiec commented Mar 18, 2019

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

@zhenwenc
Copy link
Author

@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".

@zhenwenc
Copy link
Author

(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

@zhenwenc
Copy link
Author

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.

@GoshaFighten
Copy link

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()..

@itskemo
Copy link

itskemo commented Aug 14, 2019

@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

@msheakoski
Copy link

@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.

@lock
Copy link

lock bot commented May 5, 2020

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.

@lock lock bot locked as resolved and limited conversation to collaborators May 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants