【问题标题】:DGS Code Generation plugin generate only the types for the schemaDGS 代码生成插件仅生成模式的类型
【发布时间】:2021-11-26 08:48:07
【问题描述】:

我是 GraphQL 的新手。目前我正在尝试使用 Netflix 的 DGS 框架生成一个 GraphQL 客户端。我使用SWAPI 的模式作为我的域图服务的 GraphQL 模式文件。但是该工具只生成与类型相关的类和接口。但不要按预期生成查询 API。 这是我的 build.gradle 文件

plugins {
    id 'org.springframework.boot' version '2.5.5'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id("com.netflix.dgs.codegen") version "5.1.2"
}

group = 'com.clients.netflix'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web:2.5.5'
    developmentOnly 'org.springframework.boot:spring-boot-devtools:2.5.5'
    testImplementation 'org.springframework.boot:spring-boot-starter-test:2.5.5'
    implementation 'com.netflix.graphql.dgs:graphql-dgs-spring-boot-starter:4.8.3'
}

test {
    useJUnitPlatform()
}

generateJava {
    generateClient = true
    generateDataTypes = true
}

是否有正确的方法可以做到这一点,还是框架中的问题?

【问题讨论】:

    标签: graphql netflix-dgs


    【解决方案1】:

    这是正确的行为。正如您在文档中看到的,生成器会生成 exmaple API

    DGS 代码生成插件会在项目构建过程中根据域图服务的 GraphQL 架构文件生成代码。该插件生成以下内容: 类型、输入类型、枚举和接口的数据类型。 包含类型和字段名称的 DgsConstants 类 数据提取器示例 代表您的查询的类型安全查询 API

    DOC

    生成器不知道您想如何从数据库中获取数据或什么。

    如果要生成必须添加的示例

    generateJava {
      ...
      includeQueries = ["hello"]
      includeMutations = [""]
    }
    

    【讨论】:

      【解决方案2】:

      偶然发现了同样的问题!

      我认为查询/突变/订阅类型在 GQL 规范中的定义以及随后在 DGS 库中的处理方式存在细微差异:

      private fun generateJavaClientApi(definitions: Collection<Definition<*>>): CodeGenResult {
          return if (config.generateClientApi) {
              definitions.asSequence()
                  .filterIsInstance<ObjectTypeDefinition>()
                  .filter { it.name == "Query" || it.name == "Mutation" || it.name == "Subscription" }
                  .map { ClientApiGenerator(config, document).generate(it) }
                  .fold(CodeGenResult()) { t: CodeGenResult, u: CodeGenResult -> t.merge(u) }
          } else CodeGenResult()
      }
      

      以及它们是如何在 swapi gql 架构中指定的:

      schema {
        query: Root
      }
      

      哎呀!将对象类型定义重命名为 Query 可以解决问题。我认为这是 swapi 架构的错误...

      【讨论】:

        猜你喜欢
        • 2021-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-21
        • 2013-07-29
        • 2013-05-19
        • 2016-03-19
        • 2022-12-04
        相关资源
        最近更新 更多