benawad / type-graphql-series

Typescript GraphQL Server built with TypeGraphQL
326 stars 129 forks source link

Decorators/methods #6

Open paean99 opened 5 years ago

paean99 commented 5 years ago

I have to go, so i will share here the temporary code that i have got (not so clean...). It may help give some ideas.

It does work to dynamically create type-graphql queries (it shows on the playground), but it is still incomplete and untested.

For ease of dev, the types are cast to 'any' and i also took out the product resolver temporarily to avoid potential conflicts

benawad commented 5 years ago

This looks like what I've been wanting to do, but been unsure how to approach it. Thank you! I think this has a lot of potential.

paean99 commented 5 years ago

Adding the correct type signature to the code gives correct auto-completion (somewhat, as i haven't tested thoroughly) thanks to the interface. I have taken it out because i was always changing the code and it was always giving me problems.

I still have doubts about the correct way to go about creating the methods. I don't think that the framework should have to much freedom to create anything that the user wants. Not only because of the limitations of typescript (in particular the limited dynamic possibilities of the interfaces and types), but also because i think that a good framework should be minimally opinionated about what it wants to achieve. That is why i added the 'methodList'.

Another thing i have doubts are the parameters passed to the decorators. It is easy to just pass them when creating the resolver. But some of them (the type-graphql for example) are more about type safety then anything else. Would it be possible or sustainable to create them dynamically? Probably not. After all type-graphql do not try to do it and it must have some good reasons.

As for the creation workflow, the parameters to create the resolver (methods, decorators, ...) could be all be easily passed by an array of objects, each one a configuration of the methods and it proprieties (like an array of decorators). What i don't like is that it could be a very 'weighty' config array. Would there be a better way? An easy solution could be: the more we limit the user and the more the resolver do dynamically, the more 'light' the config for each resolver...

paean99 commented 5 years ago

I finally had a little time and took a spin to see how it would look following the path of the previous code. I must say that ( as i was afraid) it is not pretty :(

The code is working for queries but should not be correct yet for mutations, since the @args are not parsed. But i am not sure that i want to continue with it. The methodList object is not maintainable and the way that i pass the decorator are hurting my eyes.

It needs an urgent refactoring with for ex an intelligent parser. :)

But all in all i think that the dynamic decorator idea was transmitted and it just lacks some implementation details that would depend on the app.

paean99 commented 5 years ago

I just though of an alternative for the parsing 'problem' adapted to the type-graphql package. One can just create some decorators that are applied directly to the entity to automatically create the resolvers form a given list. For example:

import { findAll, findOne, findPaginated} from "./myQueries";
import { create, delete, update} from "./myMutations";

// resolver decorators AutoQuery and AutoMutation.
// mutations should be given more though on how to declare its params
// possibly with an added decorator to the fields of the entity?
@AutoQuery([findAll, findOne])
@AutoMutation([create, update])
@ObjectType()
@Entity()
export class User extends BaseEntity {
}

Probably more of a though for type-graphql package than anything else, but the possibility remains and it is more pleasing to my eyes :)

benawad commented 5 years ago

now that is a nice api