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

Circular dependency issue when using functional syntax #1449

Open
zacharyclaysmith opened this issue Apr 27, 2023 · 4 comments
Open

Circular dependency issue when using functional syntax #1449

zacharyclaysmith opened this issue Apr 27, 2023 · 4 comments
Labels
Community 👨‍👧 Something initiated by a community Need More Info 🤷‍♂️ Further information is requested Question ❔ Not future request, proposal or bug issue

Comments

@zacharyclaysmith
Copy link

zacharyclaysmith commented Apr 27, 2023

Describe the Bug
A ReferenceError is thrown at startup due to circular references. These circular references occur while using the @Field(type => SomeType) syntax.

To Reproduce
Example Repository: https://github.com/zacharyclaysmith/type-graphql-circular-dependency-repro

(Key code extracts in Additional Context below)

Expected Behavior
The "functional syntax" should keep this kind of issue from happening:

From https://typegraphql.com/docs/0.17.1/types-and-fields.html:

Why use function syntax and not a simple { type: Rate } config object? Because, by using function syntax we solve the problem of circular dependencies (e.g. Post <--> User), so it was adopted as a convention. You can use the shorthand syntax @field(() => Rate) if you want to save some keystrokes but it might be less readable for others.

Logs
If applicable, add some console logs to help explain your problem.
You can paste the errors with stack trace that were printed when the error occurred.

Environment (please complete the following information):

  • OS: OSX
  • Node version: v18.5.0
  • type-graphql version: 2.0.0-beta.1
  • TypeScript version: 4.9.5
    • Additionally, using "type": "module" in package.json

Additional Context
This example was recreated from a larger project where I'm running into this issue. I tried to remove any unnecessary cruft when copying over pieces of code/dependency lists.

The original project also uses mikro-orm, and I avoid circular dependencies with their library due to the ability to use a string version of the type name, e.g. @Property('User').

Key Code extracts:

@ObjectType()
export class User {
  //...
	
  // NOTE: comment out the next line to remove the error.
  @Field(() => [Item])
  items: Item[];
}
@ObjectType()
export class Item {
  //...
	
  @Field(() => User)
  owner: User;
}
@zacharyclaysmith zacharyclaysmith changed the title Circular dependency issues on minimal code. Circular dependency issue when using functional syntax Apr 27, 2023
@carlocorradini
Copy link
Contributor

carlocorradini commented Apr 27, 2023

type: module is not currently supported in V2 beta until PR #1400 is merged.
I think this is the main cause of the issue 🙃

@carlocorradini
Copy link
Contributor

Does it work if you remove type: module?

@zacharyclaysmith
Copy link
Author

Tried it in this branch: https://github.com/zacharyclaysmith/type-graphql-circular-dependency-repro/tree/remove-type-module

It caused some cascading issues that had to be addressed (changing to commonjs and rewriting the index.ts file to not have a top-level await), but I think it technically works. I'll try similar in my actual application, though it's likely to not be as easy to solve with other major libraries in play.

@zacharyclaysmith
Copy link
Author

Now getting "TypeError: Class extends value undefined is not a constructor or null" in my main application after trying to make those changes (I'm using interfaces there that I didn't include in my original example). I'll see if I can recreate the issue in the example repo.

@MichalLytek MichalLytek added Question ❔ Not future request, proposal or bug issue Community 👨‍👧 Something initiated by a community Need More Info 🤷‍♂️ Further information is requested labels May 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community 👨‍👧 Something initiated by a community Need More Info 🤷‍♂️ Further information is requested Question ❔ Not future request, proposal or bug issue
Projects
None yet
Development

No branches or pull requests

4 participants
@zacharyclaysmith @MichalLytek @carlocorradini and others