![](/img/trans.png)
[英]How to generate typescript interfaces for graphql types using a graphql schema.json without operations?
[英]How can I generate base, flat, inline TypeScript types, based on GraphQL schema
是否可以从 GraphQL 模式 TypeScript 类型生成可与普通 REST API 一起使用的类型?
我们正在使用 CMS,它提供 GraphQL 和 REST API-s。 上级决定使用REST。但是我们还是希望有一些代码生成,方便我们的工作。 对于 REST,我们需要在 CMS 类型更改时手动创建/更新我们的类型。 GraphQL 架构由 CMS 本身更新,运行一些导入/生成作业并获得正确的类型会很棒。
我们用不同的插件、预设尝试了 graphql-codegen ,这就是我们得到的:
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;
这对于 graphql 查询非常有用,但遗憾的是,我们没有在我们的代码中使用 graphql。
这是我们需要的(或接近它的东西,没有edges
, connection
, node
):
export type Bar = {
__typename?: 'Bar';
title?: Maybe<Scalars['String']>;
};
export type Foo = {
__typename?: 'Foo';
bar?: Maybe<Bar>;
title?: Maybe<Scalars['String']>;
};
是否有工具或 graphql-codegen 预设/插件或 graphql-codegen 的某些特定配置可以帮助完成此任务?
提前致谢。
目前没有办法让 GraphQL 代码生成器只生成使用过的类型。 这是我们知道的请求,并计划在下一个主要版本中使用: https://github.com/dotansimha/graphql-code-generator/issues/8296
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.