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

Feature request: Enable support for promises in typeORM when using Lazy loading #10

Closed
abd-almofleh opened this issue Jun 14, 2024 · 6 comments

Comments

@abd-almofleh
Copy link

Currently, the package is supporting types without Promises

@Entity('my_data')
export class MyData {
  @Field(() => MyTypeObject)
  @ManyToOne(() => MyTypeObject)
  @JoinColumn()
  group: MyTypeObject;
}

And since this a relationship and according to TypeORM docs Lazy Loading to be able to load the relationship object you should mark the type as a promise. Like this:

@Entity('my_data')
export class MyData {
  @Field(() => MyTypeObject,{nullable: false})
  @ManyToOne(() => MyTypeObject,{nullable: false})
  @JoinColumn()
  group: Promise<MyTypeObject>;
}

And this is totally a valid and acceptable form and type, yet it shows this error:

Type of group in PowerTableRow is not consistent with the TypeORM relation type ManyToOne (expected type: PowerTableGroup).eslint [typeorm-typescript/enforce-relation-types]

Suggestion

The package should get the type inside the promise and check if it's the same as the type in the Field decorator

@daniel7grant
Copy link
Owner

Thanks for bringing this to my attention, I was unaware that you could do lazy loading with Promise types. I added checking for Promises in #11. I am collecting the current issues into a bigger release, it will be out in a few days, I hope.

@abd-almofleh
Copy link
Author

Perfect, thanks, I will test it once it's released.
Thanks again for the quick update

@daniel7grant
Copy link
Owner

I released v0.3.0, and added the Promise changes as a feature. Feel free to close if it works for you.

@abd-almofleh
Copy link
Author

abd-almofleh commented Jun 16, 2024

@daniel7grant Unfortunately it's still not working. It gives this error:

Type of group in PowerTableRow is not consistent with the TypeORM relation type ManyToOne (expected type: PowerTableGroup).

for the code

  @Field(() => PowerTableGroup, { nullable: false })
  @ManyToOne(() => PowerTableGroup, {  nullable: false })
  @JoinColumn()
  group!: Promise<PowerTableGroup>;

Note:

When I remove the Promise it does not give the error

@daniel7grant
Copy link
Owner

daniel7grant commented Jun 16, 2024

Are you sure that you are on the 0.3.0 version, and you restarted your editor (or ran eslint from the terminal)? For me both locally, and from a brand new CodeSandbox, it works. It passes without issue, and if I change it to be incorrect, it suggest the type with Promise (note the expected type):

image

Do you know what the difference may be?

@abd-almofleh
Copy link
Author

My mistake. NPM was stuck and not updating node_modules I had to delete the node_modules folder and run npm install to download the update.
It is working now, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants