简体   繁体   English

覆盖 GraphQL Schema 生成的 Typescript 类型

[英]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.

相关问题 为 GraphQL 输出解析自动生成的 typescript-mongodb 类型 - Resolving auto-generated typescript-mongodb types for GraphQL output TypeScript 使用 GraphQL 生成的类型将 Enum 转换为另一个 Emum - TypeScript Convert Enum to another Emum with GraphQL generated types 如何在没有操作的情况下使用 graphql schema.json 为 graphql 类型生成打字稿接口? - How to generate typescript interfaces for graphql types using a graphql schema.json without operations? 从 Typescript 类型/接口生成 Joi 模式 - Generate Joi Schema from Typescript types/interfaces 从 OpenAPI 的 `oneOf` 关键字生成了不受欢迎的 Flow/TypeScript 类型。 是否有另一个 OpenAPI 模式会产生更好的结果? - Undesirable Flow/TypeScript types being generated from OpenAPI's `oneOf` keyword. Is there another OpenAPI schema that would have better results? 覆盖从NPM @Types下载的V2.2.2中的TypeScript类型 - Override TypeScript types in V2.2.2 downloaded from NPM @Types 如何从 Typescript 模式创建 GraphQL 类型 SDK? - How to create a GraphQL typed SDK from a Typescript schema object? 访问从 GraphQL 生成的多个嵌套 TypeScript 类型 - Access multiple nested TypeScript type generated from GraphQL GraphQL typescript 参数类型不兼容 - GraphQL typescript parameters types are incompatible 如何将 GraphQL 与 TypeScript 和从 graphql-code-generator 生成的类型一起使用? - How to use GraphQL with TypeScript and typings generated from graphql-code-generator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM