prismake / typegql

Create GraphQL schema with TypeScript classes.
https://prismake.github.io/typegql/
MIT License
423 stars 21 forks source link

mixin support #62

Closed capaj closed 6 years ago

capaj commented 6 years ago

typescript supports mixins like this: https://www.typescriptlang.org/docs/handbook/mixins.html

But this approach doesn't work with typegql decorators to enhance a class which should be a GQL object type. It would be nice if ObjectType() could take an array of mixins to apply before compiling the type.

pie6k commented 6 years ago

You can use class inheritance instead eg. https://github.com/prismake/typegql/blob/master/src/test/objectType/inheritance.spec.ts

eg

    @ObjectType()
    class Vehicle {
      @Field() color: string;
    }

    @ObjectType()
    class Car extends Vehicle {
      @Field() doorCount: number;
    }

    @ObjectType()
    class Lamborghini extends Car {
      @Field() speed: number;
    }
capaj commented 6 years ago

@pie6k I am well aware of class inheritance. It's just that I have an existing hierarchy and I would like to "sprinkle" a few methods onto a class in that hierarchy. I can't just add them to a parent because this parent is used even by Object types which should not have these methods.

Also too bad you removed the comment by @19majkel94-even though he linked to his own library, I think his comment contributed to the discussion.

pie6k commented 6 years ago

I'd like to avoid competetive discussion here and I will remove futher comments not related to point of this issue too.

@19majkel94 While there is nothing wrong with linking other libraries like type-graphql (which might be much better than typegql) - there should be some clear reason behind it, espetially if you point to some feature that is already included in typegql and might be exactly the one asked by author of the issue. That's missleading because someone is expecting solution related to scope of this library, while your link is pointing to analogous feature in totally different lib. That would be understandable if typegql would be missing such feature. Otherwise it's not helpful at all.

Also consider private channels of communication for personal comments like one just deleted.

@capaj I'm sorry, but there are no plans currently to add mixins support (eg. https://reactjs.org/blog/2016/07/13/mixins-considered-harmful.html)

capaj commented 6 years ago

@pie6k that article is about react-react is totally different case than backend code. I do agree with it-in fact I've never used mixins in my react code even when most of the react comunity did so.

Unfortunately I have some API modelling needs that I cannot satisfy in other way than mixins. I could try to write a decorator like I did with: https://github.com/capaj/ts-gql-objection-blog/blob/13afe9e98953e6a80a1e00201dbe70789a7dd615/utils/ExposeRelations.ts#L72 but that solution feels much more hacky than mixins. I understand your position-you can build pretty sophisticated APIs with just class inheritance.