• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

aoudiamoncef/apollo-client-maven-plugin: Generate a Java/Kotlin GraphQL client b ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

Cox-Automotive/apollo-client-maven-plugin

开源软件地址(OpenSource Url):

https://github.com/aoudiamoncef/apollo-client-maven-plugin

开源编程语言(OpenSource Language):

Kotlin 98.3%

开源软件介绍(OpenSource Introduction):

Apollo GraphQL Client Code Generation Maven Plugin

Build Status Download Known Vulnerabilities License: MIT

Maven plugin that calls the Apollo Kotlin compiler to generate your Java/Kotlin sources.

Usage

Apollo 2 support

Getting Started

  1. Add the apollo runtime library to your project’s dependencies:

    <dependencies>
       <dependency>
           <groupId>com.apollographql.apollo3</groupId>
           <artifactId>apollo-runtime</artifactId>
           <version>3.0.0</version>
       </dependency>
    </dependencies>
  2. Add the code generator plugin to your project’s build:

    <plugins>
        <plugin>
            <groupId>com.github.aoudiamoncef</groupId>
            <artifactId>apollo-client-maven-plugin</artifactId>
            <version>5.0.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <services>
                            <example-api>
                                <compilationUnit>
                                    <name>example</name>
                                    <compilerParams>
                                        <schemaPackageName>com.example.graphql.client</schemaPackageName>
                                    </compilerParams>
                                </compilationUnit>
                            </example-api>
                        </services>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
  3. Create a file src/main/graphql/SERVICE_NAME/schema.json with the JSON results of an introspection query, OR you can automatically generate this file by setting introspection.enabled to true and endpointUrl to your GraphQL endpoint. At build time, the plugin will query the server and install this file per the value of schemaFile.

  4. Create files for each query you’d like to generate classes for under src/main/graphql/SERVICE_NAME:

    1. Query file names must match the name of the query they contain

    2. Query files must end with .graphql OR .gql by default.

    3. Any subdirectories under src/main/graphql/SERVICE_NAME are treated as extra package names to append to schemaPackageName in the plugin config.

  5. Run mvn generate-sources to generate classes for your queries.

Configuration Options

All plugin configuration properties and their defaults:

<configuration>
    <services>
        <!--Complete default configuration-->
        <lahzouz-complete-api>
            <enabled>true</enabled>
            <addSourceRoot>true</addSourceRoot>
            <sourceFolder>${project.basedir}/src/main/graphql/lahzouz</sourceFolder>
            <schemaPath>${project.basedir}/src/main/graphql/lahzouz/schema.json</schemaPath>
            <includes>
                <include>**/*.graphql</include>
                <include>**/*.gql</include>
                <include>**/*.json</include>
                <include>**/*.sdl"</include>
            </includes>
            <excludes></excludes>
            <compilationUnit>
                <name>lahzouz</name>
                <outputDirectory>
                    ${project.build.directory}/generated-sources/graphql-client/lahzouz/
                </outputDirectory>
                <testDirectory>
                    ${project.build.directory}/generated-sources/graphql-client/lahzouz/test/
                </testDirectory>
                <debugDirectory>
                    ${project.build.directory}/generated-sources/graphql-client/lahzouz/debug/
                </debugDirectory>
                <operationOutputFile>
                    ${project.build.directory}/generated/operationOutput/apollo/lahzouz/operationOutput.json
                </operationOutputFile>
                <generateOperationDescriptors>false</generateOperationDescriptors>
                <compilerParams>
                    <rootFolders>
                        <rootFolder>${project.basedir}/src/main/graphql/lahzouz/</rootFolder>
                    </rootFolders>
                    <customScalarsMapping></customScalarsMapping>
                    <operationIdGeneratorClass></operationIdGeneratorClass>
                    <schemaPackageName>com.lahzouz.apollo.graphql.client</schemaPackageName>
                    <packageName>com.lahzouz.apollo.graphql.client</packageName>
                    <codegenModels>OPERATION</codegenModels>
                    <flattenModels>true</flattenModels>
                    <generateApolloMetadata>false</generateApolloMetadata>
                    <generateAsInternal>false</generateAsInternal>
                    <generateFilterNotNull>false</generateFilterNotNull>
                    <generateFragmentImplementations>false</generateFragmentImplementations>
                    <generateKotlinModels>false</generateKotlinModels>
                    <generateOptionalOperationVariables>false</generateOptionalOperationVariables>
                    <generateQueryDocument>true</generateQueryDocument>
                    <generateResponseFields>false</generateResponseFields>
                    <generateSchema>false</generateSchema>
                    <generateTestBuilders>false</generateTestBuilders>
                    <moduleName>apollographql</moduleName>
                    <useSemanticNaming>true</useSemanticNaming>
                    <targetLanguage>JAVA</targetLanguage>
                    <sealedClassesForEnumsMatching></sealedClassesForEnumsMatching>
                    <alwaysGenerateTypesMatching></alwaysGenerateTypesMatching>
                    <metadataOutputFile>
                        ${project.build.directory}/generated/metadata/apollo/lahzouz/metadata.json
                    </metadataOutputFile>
                </compilerParams>
            </compilationUnit>
            <introspection>
                <enabled>false</enabled>
                <endpointUrl></endpointUrl>
                <headers></headers>
                <schemaFile>${project.basedir}/src/main/graphql/lahzouz/schema.json</schemaFile>
                <connectTimeoutSeconds>10</connectTimeoutSeconds>
                <readTimeoutSeconds>10</readTimeoutSeconds>
                <writeTimeoutSeconds>10</writeTimeoutSeconds>
                <useSelfSignedCertificat>false</useSelfSignedCertificat>
                <useGzip>false</useGzip>
                <prettyPrint>false</prettyPrint>
            </introspection>
        </lahzouz-complete-api>

        <!--Minimal configuration-->
        <lahzouz-min-api>
            <compilationUnit>
                <name>lahzouz</name>
            </compilationUnit>
        </lahzouz-min-api>

        <!--Auto configuration-->
        <lahzouz></lahzouz>
    </services>
</configuration>

Custom Types

To use custom Scalar Types you need to define mapping configuration then register your custom adapter:

<configuration>
    ...
    <customScalarsMapping>
        <Long>java.time.LocalDate</Long>
    </customScalarsMapping>
    ...
</configuration>

Implementation of a custom adapter for java.time.LocalDate:

public class DateGraphQLAdapter implements Adapter<Date> {
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public Date fromJson(@NotNull final JsonReader jsonReader, @NotNull final CustomScalarAdapters customScalarAdapters) throws IOException {
        try {
            return DATE_FORMAT.parse(jsonReader.nextString());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void toJson(@NotNull final JsonWriter jsonWriter, @NotNull final CustomScalarAdapters customScalarAdapters, final Date date) throws IOException {
        jsonWriter.value(DATE_FORMAT.format(date));
    }
}

Using Apollo Client




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
mvnaresh/maven发布时间:2022-08-17
下一篇:
Cox-Automotive/apollo-client-maven-plugin: NO LONGER MAINTAINED HERE, SEE FORK I ...发布时间:2022-08-17
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap