[英]Override from GraphQL Schema generated Typescript types
Im using the graphql-code-generator to generate typescript types out of my GraphQL schema.我使用graphql-code-generator从我的 GraphQL 模式中生成打字稿类型。 In GraphQL you can create custom scalar types which will generate the following type:
/graphql.ts
在 GraphQL 中,您可以创建自定义标量类型,这些类型将生成以下类型:
/graphql.ts
export type Scalars = {
String: string;
Boolean: boolean;
NodeDescription: any;
};
export type CodeResource = {
ast: Scalars["NodeDescription"];
};
As you can see the GraphQL default scalar types like String, Boolean etc. will be mapped on the equivalent Typescript type.如您所见,GraphQL 默认标量类型(如 String、Boolean 等)将映射到等效的 Typescript 类型上。 Custom scalar types like
NodeDescription
will get the typescript type any
.像
NodeDescription
这样的自定义标量类型将获得NodeDescription
类型any
。
In my Angular Client there already is a NodeDescription
type (in ../app/shared/syntaxtree) which I want to use instead of the generated any
type.在我的 Angular 客户端中,已经有一个
NodeDescription
类型(在 ../app/shared/syntaxtree 中),我想使用它而不是生成的any
类型。 Is there a way to override the NodeDescription
field in Scalars type?有没有办法覆盖标量类型中的
NodeDescription
字段? So in the end I want the ast
field of the generated CodeResource type to have the type NodeDescription
instead of any
.所以最后我希望生成的 CodeResource 类型的
ast
字段具有类型NodeDescription
而不是any
。
My first idea was to override the the Scalars["NodeDescription"]
type with NodeDescription
.我的第一个想法是用
NodeDescription
覆盖Scalars["NodeDescription"]
类型。 So I tried to import all types in a new file and override them like: /types.ts
所以我尝试在新文件中导入所有类型并覆盖它们,例如:
/types.ts
import {Scalars} from "./graphql";
import {NodeDescription} from "../app/shared/syntaxtree";
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
type Scalar = Overwrite<Scalars, {NodeDescription: NodeDescription}>
This actually works, but the ast
field of the CodeResource
type still is type any
.这实际上有效,但
CodeResource
类型的ast
字段仍然是 type any
。
Another idea is to use a bash script with a well thought out sed
command so the generated file will be edited like:另一个想法是使用带有深思熟虑的
sed
命令的 bash 脚本,这样生成的文件将被编辑如下:
import {NodeDescription} from "../app/shared/syntaxtree";
export type Scalars = {
String: string;
Boolean: boolean;
NodeDescription: NodeDescription;
};
However before I implement that approach I would like to know if there is a smart typescript way to override the generated types.但是,在实施该方法之前,我想知道是否有一种智能打字稿方式来覆盖生成的类型。 Thanks for help.
感谢帮助。
I found a specific solution if you have the same problem using graphql-code-generator .如果您使用graphql-code-generator 遇到同样的问题,我找到了一个特定的解决方案。 There is a plugin called add .
有一个名为add的插件。 So in the codegen.yml you are able to add imports and address the custom scalar types.
因此,在 codegen.yml 中,您可以添加导入并处理自定义标量类型。 My codegen.yml now looks like that:
我的 codegen.yml 现在看起来像这样:
overwrite: true
schema: "../schema/graphql/schema.json"
documents: "../schema/graphql/**/*.graphql"
generates:
src/generated/graphql.ts:
plugins:
- "typescript"
- "typescript-operations"
- "typescript-apollo-angular"
- add:
content: 'import { NodeDescription } from "../app/shared/syntaxtree";'
config:
enumsAsTypes: true
scalars:
NodeDescription: NodeDescription
ISO8601DateTime: string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.