notiz-dev / nestjs-prisma-starter

Starter template for NestJS 😻 includes GraphQL with Prisma Client, Passport-JWT authentication, Swagger Api and Docker
MIT License
2.36k stars 337 forks source link

e2e tests: Undefined type error. Provide explicit type for the "user" of the "AuthResolver" class. #422

Closed lparolari closed 2 years ago

lparolari commented 4 years ago

Bug

Steps to reproduce

Expected behaviour

e2e tests passed

Actual behaviour

$ jest --config ./test/jest-e2e.json
 FAIL  test/app.resolver.e2e-spec.ts
  ● AppResolver (e2e) › helloWorld (Query)

    Undefined type error. Make sure you are providing an explicit type for the "user" of the "AuthResolver" class.

      at TypeMetadataStorageHost.compileExternalFieldResolverMetadata (../node_modules/@nestjs/graphql/dist/schema-builder/storages/type-metadata.storage.js:256:23)
      at ../node_modules/@nestjs/graphql/dist/schema-builder/storages/type-metadata.storage.js:240:22
          at Array.forEach (<anonymous>)
      at TypeMetadataStorageHost.compileFieldResolverMetadata (../node_modules/@nestjs/graphql/dist/schema-builder/storages/type-metadata.storage.js:229:18)
      at TypeMetadataStorageHost.compile (../node_modules/@nestjs/graphql/dist/schema-builder/storages/type-metadata.storage.js:144:14)
      at GraphQLSchemaFactory.<anonymous> (../node_modules/@nestjs/graphql/dist/schema-builder/graphql-schema.factory.js:38:57)
      at ../node_modules/@nestjs/graphql/node_modules/tslib/tslib.js:114:75
      at Object.__awaiter (../node_modules/@nestjs/graphql/node_modules/tslib/tslib.js:110:16)

  ● AppController (e2e) › /hello/:name (GET)

    Undefined type error. Make sure you are providing an explicit type for the "user" of the "AuthResolver" class.

[...]

How can I fix this? Thank you!

lparolari commented 4 years ago

After diving a bit into the problem and reading this illuminating issue https://github.com/nestjs/graphql/issues/226, I found that my problem is due to this portion of code

https://github.com/fivethree-team/nestjs-prisma-starter/blob/d628befcf507f4c7ab7b0fb55697ef997063e40e/src/resolvers/auth/auth.resolver.ts#L46-L49

and in particular to

https://github.com/fivethree-team/nestjs-prisma-starter/blob/d628befcf507f4c7ab7b0fb55697ef997063e40e/src/resolvers/auth/auth.resolver.ts#L46

where, as the error message says, I'm not providing an explicit type for "user".

Fix

Import the user model and add the type explicitly to the decorator for the user method.

import { User } from '../../models/user.model';
@ResolveField('user', (of) => User)   // <-- modify this
async user(@Parent() auth: Auth) {
  return await this.auth.getUserFromToken(auth.accessToken);
}
marcjulian commented 4 years ago

@lparolari thanks for raising this issue. This project uses NestJS CLI Plugin to generate the GraphQL types, hence the type in @ResolveField('user') is not needed during the build step. However, jest is testing against the typescript and not invoking the CLI Plugin.

I found the following issue and a blog post on those topics.

Let me know if that helps you.

lparolari commented 4 years ago

@marcjulian Thank you a lot! I followed the issue you reported and worked like a charm. Do you think that is useful to add this trick to the project or am I using something in a wrong way?

marcjulian commented 4 years ago

@lparolari yes it very useful to add this trick to this starter project. Do you mind creating a PR?

lparolari commented 4 years ago

@marcjulian Sure, just did it. I hope it might help.