如何通过 Graphql 模式生成 java 实体

How to generate java entity by Graphql schema

我是 graphql java 客户端开发的新手。最近我遇到一个问题,如何通过 graphql schema 定义生成一个 java 实体。因为在我这边,架构总是在另一边发生变化,所以我想要一种方法来自动生成 java 实体 class 而不是手动读取架构然后更改代码。是否有关于此类要求的实践?谢谢

您可以查看以下内容maven plugin。它将帮助您从 .graphqls 文件生成 Java 类。

假设您只有一个定义 GraphQL 模型的文件,名为 myGraphqls,我将按如下方式使用此插件:

<build>
    <plugins>
        <plugin>
            <groupId>io.github.kobylynskyi</groupId>
            <artifactId>graphql-codegen-maven-plugin</artifactId>
            <version>1.2.3</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <graphqlSchemaPaths>
                            <graphqlSchemaPath>${project.basedir}/src/main/resources/graphql/myGraphqls.graphqls</graphqlSchemaPath>
                        </graphqlSchemaPaths>
                        <outputDir>${project.build.directory}/generated-sources/graphql</outputDir>
                        <packageName>com.graphql.model</packageName>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

在 运行 maven 之后,您将能够在 generated-sources 文件夹中找到一个名为 graphql 的文件夹在com.graphql.model.

包下生成类