0

Is it possible to generate from GraphQL schema TypeScript types, that can be used with common REST API?

We are working with a CMS, that provides GraphQL and REST API-s. It was decided by higher-ups to use REST. But we still want to have some code generation to make our work easier. With REST we need to create/update our types manually whenever CMS types change. GraphQL schema is updated by CMS itself, and it'd be great to just run some import/generate job and get correct types.

We tried graphql-codegen with different plugins, presets, and that's what we get:

export type Bar = {
  __typename?: 'Bar';
  title?: Maybe<Scalars['String']>;
};

export type Foo = {
  __typename?: 'Foo';
  footerConnection?: Maybe<FooBarConnection>;
  title?: Maybe<Scalars['String']>;
};

export type FooBarConnection = {
  __typename?: 'FooBarConnection';
  edges?: Maybe<Array<Maybe<FooBarEdge>>>;
  totalCount?: Maybe<Scalars['Int']>;
};

export type FooBarEdge = {
  __typename?: 'FooBarEdge';
  node?: Maybe<FooBarNode>;
};

export type FooBarNode = Bar;

That's great for graphql queries, but we, sadly, don't use graphql in our code.

Here is what we need (or something close to it, without edges, connection, node):

export type Bar = {
  __typename?: 'Bar';
  title?: Maybe<Scalars['String']>;
};

export type Foo = {
  __typename?: 'Foo';
  bar?: Maybe<Bar>;
  title?: Maybe<Scalars['String']>;
};

Is there a tool, or graphql-codegen preset/plugin, or some specific configuration for graphql-codegen, that can help with this task?

Thanks in advance.

Aliaks
  • 1

1 Answers1

1

There is currently no way to get GraphQL Code Generator to generate only used types. This is a request that we are aware of and that is planned for the next major version: https://github.com/dotansimha/graphql-code-generator/issues/8296

Charly Poly
  • 384
  • 1
  • 8
  • Thanks for clarification. Any info on when the next major release will happen, or, maybe, when this feature will be implemented for some RC? Also, sorry to bother, but maybe you are aware, if there are any other tools, that can do only used types generation? – Aliaks Nov 22 '22 at 16:07
  • We don't have a clear idea of a release date for now and I'm not aware of any other tool. In the meantime, you can set the `onlyOperationTypes: true` config option, this will reduce the number of generated types. – Charly Poly Nov 23 '22 at 10:00