-
-
Notifications
You must be signed in to change notification settings - Fork 675
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
Generic types support #180
Comments
Available in Docs for |
Amazing news ;) thanks @19majkel94 |
@19majkel94 According to your example:
This way you propose to generate unique names for generic types. But what if I specified another name for |
@Veetaha You can have a function that generates a classes (no function Foo(TItem) {
@ObjectType({ name: "Foo" + TItem.name })
class Foo {
@Field()
bar: string;
}
return Foo;
}
const FooBar = Foo(Bar); or as a factory for base classes ( function Foo(TItem) {
@ObjectType({ isAbstract: true })
abstract class Foo {
@Field()
bar: string;
}
return Foo;
}
@ObjectType()
class FooBar extends Foo(Bar) {} Could you describe your use case in more details, instead of trying to propose the solution for the unknown problem? |
@19majkel94 Sorry if I was not verbose. The version of typegraphql that I use (namely 0.16.0) exposes function ObjectType(): ClassDecorator;
function ObjectType(options: ObjectOptions): ClassDecorator;
function ObjectType(name: string, options?: ObjectOptions): ClassDecorator; Thus, if you decorate a class the following way: @ObjectType()
class UserType { /* ... */ } the generated GraphQL type name will be If you want to leave your TypeScript class name being @ObjectType('User')
class UserType { /* ... */ } Now the generated GraphQL type name is And when I have the following factory function (taken from your examples): export default function PaginatedResponse<TItem>(TItemClass: ClassType<TItem>) {
@ObjectType(`Paginated${TItemClass.name}Response`)
class PaginatedResponseClass {
}
return PaginatedResponseClass;
} I can't be sure that @ObjectType('Book')
class BookType { } So when I do this: const PaginatedBookResponse = PaginatedResponse(BookType); The generated graphql type will be Thus I would like a function that returns a respective GraphQL type name for a given type, so that I could correct import { getMetadata } from 'type-graphql';
export default function PaginatedResponse<TItem>(TItemClass: ClassType<TItem>) {
// should throw if TItemClass was not decorated with either @ObjectType() or @InputType()
const graphqlTypeName = getMetadata(TItemClass).name;
@ObjectType(`Paginated${graphqlTypeName}Response`)
class PaginatedResponseClass {
}
return PaginatedResponseClass;
} P.S. I don't know why you invoke I think that |
@Veetaha function PaginatedResponse<TItem>(TItemClass: ClassType<TItem>, itemName: string) {}
const PaginatedBookResponse = PaginatedResponse(BookType, "Book"); The Accessing metadata is part of the #134. |
@19majkel94 As I have said in my first comment,
Exposing some interface to access the metadata you define would be way better than reimplementing the wheel. I could wrap |
Right now we can only extend other object type, input type and args type classes. But this doesn't allow us to overwrite a property type so we can't create a truly generic types like the one used for pagination.
So, to the higher order class pattern we have to introduce
isAbstract
option that works the same way as in resolvers inheritance. Then we can create generic types:We can then easily extend the generic class:
So this would generate following GraphQL type:
The text was updated successfully, but these errors were encountered: