Graphql-code-generator: [Apollo Angular] Generate a wrapper SDK service

Created on 23 Nov 2019  路  2Comments  路  Source: dotansimha/graphql-code-generator

Is your feature request related to a problem? Please describe.

When using graphql-codegen with the typescript-apollo-angular plugin I always find myself writing a wrapper class that exposes all the generated operationGQL services. Let's refer to this as my SDK Service.

That way I only always have to inject that one SDK service if I want to access any data from GraphQL.

This is an example of what my SDK services look like:

@Injectable({ providedIn: 'root' })
export class ApolloAngularSDK {
  constructor(
    private myActivitiesGql: MyActivitiesGQL,
    private myActivityFeedGql: MyActivityFeedGQL,
    private registerGql: RegisterGQL,
    private loginGql: LoginGQL,
  ) {
  }

  myActivities(variables?: MyActivitiesQueryVariables, options?: QueryOptionsAlone<MyActivitiesQueryVariables>) {
    return this.myActivitiesGql.fetch(variables, options)
  }

  myActivitiesWatch(variables?: MyActivitiesQueryVariables, options?: WatchQueryOptionsAlone<MyActivitiesQueryVariables>) {
    return this.myActivitiesGql.watch(variables, options)
  }

  myActivityFeed(variables?: MyActivityFeedQueryVariables, options?: QueryOptionsAlone<MyActivityFeedQueryVariables>) {
    return this.myActivityFeedGql.fetch(variables, options)
  }

  myActivityFeedWatch(variables?: MyActivityFeedQueryVariables, options?: WatchQueryOptionsAlone<MyActivityFeedQueryVariables>) {
    return this.myActivityFeedGql.watch(variables, options)
  }

  register(variables: RegisterMutationVariables, options?: MutationOptionsAlone<RegisterMutation, RegisterMutationVariables>) {
    return this.registerGql.mutate(variables, options)
  }

  login(variables: LoginMutationVariables, options?: MutationOptionsAlone<LoginMutation, LoginMutationVariables>) {
    return this.loginGql.mutate(variables, options)
  }
}

Describe the solution you'd like

In order to not repeat myself I've patched the typescript-apollo-angular plugin and added functionality to create such an SDK service. The reason that I open this feature request is to see if it would be considered as a PR.

Describe alternatives you've considered

I could fork the plugin and publish it myself but I'd rather see it upstream :)

Additional context

enhancement plugins waiting-for-release

Most helpful comment

Available in v1.10.0.

All 2 comments

It seems like a nice addition to the generated API. @beeman feel free to open a PR :) Thanks!

Available in v1.10.0.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fvisticot picture fvisticot  路  3Comments

jackhkmatthews picture jackhkmatthews  路  3Comments

edorivai picture edorivai  路  3Comments

leebenson picture leebenson  路  3Comments

steebchen picture steebchen  路  3Comments