【问题标题】:apollo-codegen output is emptyapollo-codegen 输出为空
【发布时间】:2017-09-27 01:28:32
【问题描述】:

我遇到了 apollo-codegen 无法成功生成打字稿代码的情况。

对于 graphql 文件 (generated/schema.graphql):

type Author {
  id: Int!
  firstName: String
  lastName: String
  posts: [Post]
}

type Post {
  id: Int!
  title: String
  author: Author
  votes: Int
}

然后我运行:

$apollo-codegen introspect-schema generated/schema.graphql --output generated/schema.json

这会生成一个似乎包含相关信息的./generated/schema.json(我看到了有关作者及其属性、帖子及其属性的信息)。

然后我运行

$apollo-codegen generate generated/schema.graphql --schema generated/schema.json --target typescript 并获得(有效的)空输出。

//  This file was automatically generated and should not be edited.
/* tslint:disable */
/* tslint:enable */

我也尝试过生成 .swift 文件,输出类似的空输出。

Apollo 代码生成版本是:

"apollo-codegen": "^0.11.2",

有人看看我做错了什么吗?

【问题讨论】:

    标签: graphql


    【解决方案1】:

    我是apollo-codegen 的合作者。很高兴听到您正在尝试!

    您没有看到任何输出,因为 apollo-codegen 根据您项目中的 GraphQL 操作(查询、变异)生成类型,而不仅仅是基于您的架构中的类型。

    根据我们的经验,您很少会针对完整的 GraphQL 类型发送查询。相反,我们发现基于 graphql 操作的类型是最有用的。

    例如,给定您提供的类型,您可以编写一个查询:

    query AuthorQuery {
      authors {
        firstName,
        lastName
      }
    }
    

    将生成的类型(以及您可能希望在使用此查询结果的代码中使用的类型是

    type AuthorQuery = {
      authors: Array<{
        firstName: string,
        lastName: string
      }>
    }
    

    请注意您将如何在 React 组件(或类似组件)中使用 AuthorQuery 类型,而您不会使用 Author 类型,因为它包含的字段比您实际请求的要多。

    但是,如果您确实有从您的 graphql 架构到 typescript 的 1:1 类型的用例,请在项目本身上提交问题,我很乐意在那里讨论 :)

    【讨论】:

    • 我试过这个,但是当我生成模式时它看起来被过滤了。你能看看吗? github.com/benjaminabbitt/graphql-experiments
    • 我也很难理解这一点,因为示例是“--output scema.ts”,但实际上输出的是查询类型,而不是模式类型。也许在示例中将输出文件命名为“query-types.ts”会更清楚。
    • 你有关于 .graphql 文件中内容的任何文档吗?
    猜你喜欢
    • 2022-07-10
    • 2019-12-27
    • 2019-07-19
    • 2021-09-02
    • 2019-02-10
    • 2023-01-04
    • 2020-12-21
    • 2019-07-30
    • 2019-11-16
    相关资源
    最近更新 更多