【问题标题】:How to generate typescript interfaces for graphql types using a graphql schema.json without operations?如何使用 graphql schema.json 为 graphql 类型生成 typescript 接口而不进行操作?
【发布时间】:2021-01-25 13:12:42
【问题描述】:

如何从 schema.json 生成 Typescript 文件。 graphql-codegenerator 和 apollo-codegen 似乎都没有提供从 schema.json 生成 typescript 接口的方法,而无需在操作中对类型进行特定使用。

我现在为此苦苦挣扎了一段时间。有没有办法做到这一点?

【问题讨论】:

  • 嗨,您能分享一下您在schema.json 中获得的内容以及您的预期输出吗?这将有助于更好地理解您的问题。

标签: typescript graphql


【解决方案1】:

我的 schema.json 有一个标准的自省输出。 我一直在努力寻找一种合适的方法来生成我的打字稿界面。

我实际上找到了一种方法:

codegen.yml

schema:
    - http://my-graphql-api.com/graphql
    - ./local/schema.graphql
documents:
    - ./src/**/*.graphql
overwrite: true
clientSchema: ./local/schema.graphql
generates:
    src/graphql-types.ts:
        plugins:
            - typescript
            - typescript-operations
        config:
            declarationKind:
                union: type
                type: interface
                input: interface
                scalar: interface
                arguments: interface
                interface: interface
            enumsAsTypes: true
            includeDirectives: true
            commentDescriptions: true
            flattenGeneratedTypes: true
            avoidOptionals:
                field: false
                object: false
                inputValue: false

然后运行: graphql-code-generator --config ./codegen.yml

https://graphql-code-generator.com/docs/plugins/typescript

有没有办法用 apollo-codegen cli 做到这一点?

【讨论】:

    猜你喜欢
    • 2019-09-01
    • 2016-02-04
    • 2018-04-25
    • 2019-11-12
    • 2020-01-15
    • 2020-12-05
    • 2020-02-13
    • 2018-09-04
    • 2019-03-30
    相关资源
    最近更新 更多