Graphql-code-generator: Possible to produce classes vs types ?

Created on 8 Apr 2019  路  6Comments  路  Source: dotansimha/graphql-code-generator

Hi,

I am using the typescript plugin. All types are used by using type keyword. In fact my tslint complains about

Use an interface instead of a type literal. (interface-over-type-literal)tslint(1)

Although what I really need is for the generator to create classes. I use mappers and passing in the type isn't valid.

ie.

  this.mapperService.mapper
      .createMap<Responses.IUserAudit, GraphQL.UserAudit>(
        MapperHelper.signatureFromStrings("Entity.IUserAudit", "ViewModel.UserAudit"),
        GraphQL.UserAudit
      )
      .forMember("who", opt => opt.mapFrom(source => source.who))
      .forMember("when", opt => opt.mapFrom(source => source.when))
      )

GraphQL.UserAudit isn't valid as it is this

export type UserAudit = {
  what?: Maybe<UserAuditWhat>;
  relatedId?: Maybe<Scalars["String"]>;
  when?: Maybe<Scalars["String"]>;
  who?: Maybe<Scalars["String"]>;
};

and the createMap takes

export declare type TGenericClass<T = any> = {
    new (...args: any[]): T;
};

so it needs a concrete class.

Is there some kind of configuration to switch between interfaces and classes and types ?

Thanks in advance

enhancement plugins waiting-for-release

Most helpful comment

@borremosch @appsolutegeek I see no reason to move from types to interfaces. How is one different form another if it comes to your example?

All 6 comments

I suppose if classes can't be produced, it would be nice to be able to produce interfaces.

+1, I am using graphql-code-generator in conjunction with type-graphql. I have a service which has both a GraphQL server and a GraphQL client. I need to get some of the types that are received on the client side of this service (graphql-code-generated) and expose them again on the server side of the service. But for this I require the types to be classes as well, otherwise I cannot use them in a type-graphql-specific decorator.

@borremosch @appsolutegeek I see no reason to move from types to interfaces. How is one different form another if it comes to your example?

@kamilkisiela in my case, it would only help to have classes. Interfaces would not add anything over types. But actually it will require more than just classes (i.e. the proper decorators) to be able to use graphql-code-generator types in conjunction with a type-graphql server, so it would not make a lot of sense to include that type of functionality in the core graphql-codegen typescript plugin.

Sounds like a plugin, if you want I could help you implement it :)

Available in 1.5.0 馃帀

Was this page helpful?
0 / 5 - 0 ratings