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

Plugin doesn't not infer type properly on Watching for file changes (running nest start --watch) (typescript 5) #2800

Closed
2 of 4 tasks
msimon opened this issue Apr 28, 2023 · 3 comments

Comments

@msimon
Copy link
Contributor

msimon commented Apr 28, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

We noticed the issue when updating to typescript 5 and updating all our nestJs packages.

Having the plugin set in ./nest-cli.json

{
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "plugins": [
      {
        "name": "@nestjs/graphql",
        "options": {
          "introspectComments": true
        }
      }
    ]
  }
}

And given a class like:

@Table
@ObjectType()
export class User extends BaseModel {
  ...

  @Column
  firstName?: string;

  @Column
  lastName?: string;

  @Unique
  @Column
  email?: string;

  @HideField()
  @HasMany(() => Availability)
  availability: Availability[];

  // @Column
  // email2?: string;
}

And another class like:

@ObjectType()
export class TokenResponse {
  constructor(accessToken: string, refreshToken: string, user: User) {
    this.accessToken = accessToken;
    this.refreshToken = refreshToken;
    this.user = user;
  }

  accessToken: string;
  refreshToken: string;
  user: User;
}

Running nest start --watch first. No issues. schema.graphql

Modifying any field in the user object, e.g: uncomment the email2 => raise the following issue:

Error SchemaGenerationError: Schema generation error (code-first approach)
   ....
   details: [
    GraphQLError: Type TokenResponse must define one or more fields.
   .... {
      path: undefined,
      locations: undefined,
      extensions: [Object: null prototype] {}
    }
  ]

What's happening is that the plugin is not inferring @Field on any variable in the Token class.

I dug a bit into what's generating and after the update on the user, the dist/token/token.entity.js has the following:

static _GRAPHQL_METADATA_FACTORY() {
        return {};
    }

instead of:

    static _GRAPHQL_METADATA_FACTORY() {
        return { accessToken: { type: () => String }, refreshToken: { type: () => String }, user: { type: () => require("../user/user.entity").User } };
    }

I tried to figure out if there was something up around:

But I ran out of time before I get anything concrete

Minimum reproduction code

https://github.com/HeroJourneyClub/nestjs_graphql_gen_error

Steps to reproduce

  1. yarn install
  2. yarn start:dev
  3. Edit nest_graphql_gen/src/user/user.entity.ts > Uncomment email2 code & save
  4. The above error will be displayed.

Expected behavior

@field is inferred properly and no error is raised

Package version

11.0.4

Graphql version

graphql: 16.6.0
apollo-server-express: N/A
apollo-server-fastify: N/A

NestJS version

9.3.12

Node.js version

v16.14.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

Only noticed this issue after upgrading to Typescript 5 and upgrading all our Nestjs package.

rm -rf dist, then rebuild infer the @field properly, so it seems to be on update.
Any file that does not go through:

return typeClassVisitor.visit(sf, ctx, program, options);

will be having an incorrect _GRAPHQL_METADATA_FACTORY in the generated JS

@nicohaenggi
Copy link

Running into the same issue, the issue occurs due to the introspectComments being enabled.

The underlying issue seems to be the following line [1], where jsDoc[0] is undefined whenever there are no comments for a specific property, thus throwing an exception. Just replacing jsDoc[0].comment with jsDoc[0]?.comment will resolve the issue.

[1]

return getTextOfJSDocComment(jsDoc[0].comment);

@msimon
Copy link
Contributor Author

msimon commented May 3, 2023

@nicohaenggi Thanks!

I can confirm the code shared by @nicohaenggi is fixing the issue. I'll do a PR.

msimon added a commit to HeroJourneyClub/nest-graphql that referenced this issue May 3, 2023
msimon added a commit to HeroJourneyClub/nest-graphql that referenced this issue May 3, 2023
@kamilmysliwiec
Copy link
Member

Let's track this here #2809

kamilmysliwiec added a commit that referenced this issue May 15, 2023
[#2800] Fix graphql schema generation with plugin introspectComments enabled
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants