-
-
Notifications
You must be signed in to change notification settings - Fork 674
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Extend a typedef in another file #228
Comments
I am planing to add better modules (decoupling) support but this case is the one where I haven't found a good, typesafe API yet. You can read this issue about the problem how to have module-scoped typedefs. The only idea that I can think now is to have // modules/user/user.ts
@ObjectType()
export default class User {
@Field()
email: string;
@Field()
password: string;
}
// modules/profile/user.ts
import BaseUser from "@modules/user";
@ObjectType({ extends: BaseUser })
class User extends BaseUser {
@Field()
profilePictureUrl: string;
} But the problem with that approach is that it's hard to create a module that rely on two other modules that modify the |
I personally thought about using a mixin https://www.typescriptlang.org/docs/handbook/mixins.html |
TS mixins are awful and (I hope) a deprecated pattern 😖 ES6 class mixins are the only solution: I plan adding to TypeGraphQL a type-safe port of |
Okay good so this way you can extend a class in another one, however, unlike an internal module (which would be merged by the language) you still need the parent class to know the subclasses to explicitly extend them. |
TS is not able to merge classes in namespaces (internal modules): // a.ts
namespace MyApp {
export class User {
name!: string;
}
}
// b.ts
namespace MyApp {
export class User {
age!: number;
}
} To achieve this, TS would have to support partial classes: |
Hi all, TypeORM solved that already with "@ChildEntity()" https://github.com/typeorm/typeorm/blob/master/docs/entity-inheritance.md Would be awesome if Type-Graphql could implement it the same since alot is already inspired by the same stuff.
TypeORM creates a Column in the "InvoiceDocument" Table here. |
@Logic-Bits So you propose the same solution as I showed earlier? // modules/user/user.ts
@ObjectType()
export default class User {
@Field()
email: string;
@Field()
password: string;
}
// modules/profile/user.ts
import BaseUser from "@modules/user";
@ObjectType({ extends: true })
class User extends BaseUser {
@Field()
profilePictureUrl: string;
} The This solution is great but again - how then create a module that rely on two other modules that modify the |
@19majkel94 maybe it can be solved in code by using type unions? It's not perfectly safe but I don't see how it would impact any further improvement if we can find a better way to do type checking here. |
What is the final solution ? |
@kaousheik i believe the current "best" solution is to: example: // modules/user/user.ts
@ObjectType()
class User {
@Field()
email: string;
// ideally this would be defined in the Profile module, but its currently not supported
@Field()
profilePictureUrl: string;
}
// modules/profile/user.resolver.ts
import User from "@modules/user";
@Resolver((of) => User)
class UserResolver {
@ResolveField()
profilePictureUrl() { ...logic }
}
This way you get some separation of concerns (you don't need to pass around your services), but you will need to pass around the entity classes. For reference, the problem this issue is trying to solve is the equivalent of Nexus' |
+1 for this |
@valerii15298 The purpose of |
Sometimes you want to add a property to whatever model but you want to keep all the logic (typedef and resolver) in a folder appart. This can be done using the ‘extend’ keyword in a typedef.
https://www.apollographql.com/docs/graphql-tools/generate-schema.html#extend-types Actually this is more a mixin than an extend. How could you achieve the same using type-graphql ?
The text was updated successfully, but these errors were encountered: