Skip to content

Circular dependency issue when using functional syntax #1449

@zacharyclaysmith

Description

@zacharyclaysmith

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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions