Graphql-code-generator: Typescript: Generate enums of all named Queries and Mutations

Created on 7 May 2020  路  3Comments  路  Source: dotansimha/graphql-code-generator

Referencing named queries using string literals can be cumbersome/bug-prone. Having an auto-generated enum containing all named queries would help with that.

Apollo example, but might be applicable more widely:

mutation MyMutation { 
  # ...
}
query MyQuery {
  # ...
}

```ts
myMutationGQL
.mutate(
{ id, patch },
{ refetchQueries: ['MyQuery'] } // <---- 'MyQuery' literal
);

An auto-generated enum would ease this a little:
```ts
// graphql.ts
export enum NamedQueries {
  MyQuery = 'MyQuery'
}

// usage
import { NamedQueries } from './graphql.ts'

myMutationGQL
  .mutate(
    { id, patch },
    { refetchQueries: [NamedQueries.MyQuery] } // <---- No more typos + auto-completion
  );
waiting-for-release

Most helpful comment

All 3 comments

Available in v1.14.0.

Thanks! :)

Was this page helpful?
0 / 5 - 0 ratings