MichalLytek / MichalLytek/type-graphql

Circular dependency issue when using functional syntax

Open
#1,449 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Community :family_man_girl: Need More Info :man_shrugging: Question :grey_question:
Dominant language
TypeScript
Stars
8.1k
Forks
672
PR merge metrics
No merged PRs in 30d

Description

**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:

```typescript
@ObjectType()
export class User {
//...

// NOTE: comment out the next line to remove the error.
@Field(() => [Item])
items: Item[];
}
```

```typescript
@ObjectType()
export class Item {
//...

@Field(() => User)
owner: User;
}
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the ReferenceError in the linked type-graphql-circular-dependency-repro repository with the User and Item classes using @Field(() => [Item]) and @Field(() => User). Read the @Field decorator handling for functional type syntax and compare its behavior under Node's "type": "module" configuration. Done means the circular example starts without the ReferenceError while preserving the expected generated schema.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, typescript
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.