简体   繁体   中英

How can I generate base, flat, inline TypeScript types, based on GraphQL schema

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM