/apollo-client-maven-plugin

Generate a Java GraphQL client based on introspection data and predefined queries.

Primary LanguageKotlinMIT LicenseMIT

Apollo GraphQL Client Code Generation Maven Plugin

CircleCI Download Codacy Badge Known Vulnerabilities FOSSA Status License: MIT

Usage

A full usage example can be found in the test project

Getting Started

NOTE: This plugin requires a nodejs environment to execute the bundled apollo-codegen node module.

  1. Add the apollo runtime library and guava to your project's depedencies:

    <dependency>
        <groupId>com.apollographql.apollo</groupId>
        <artifactId>apollo-runtime</artifactId>
        <version>1.0.0-alpha5</version>
    </dependency>
  2. Add the code generator plugin to your project's build (if codegen is desired):

    <plugin>
        <groupId>com.github.sparow199</groupId>
        <artifactId>apollo-client-maven-plugin</artifactId>
        <version>1.2.4</version>
        <executions>
            <execution>
                <goals>
                    <goal>generate</goal>
                </goals>
                <configuration>
                    <basePackage>com.my.package.graphql.client</basePackage>
                </configuration>
            </execution>
        </executions>
    </plugin>
  3. Create a file src/main/graphql/schema.json with the JSON results of an introspection query

  4. Create files for each query you'd like to generate classes for under src/main/graphql: 1. Query file names must match the name of the query they contain 2. Query files must end with .graphql 3. Any subdirectories under src/main/graphql are treated as extra package names to append to packageName in the plugin config.

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

Configuration Options

All plugin options and their defaults:

<configuration>
    <skip>false</skip>
    <addSourceRoot>true</addSourceRoot>
    <basePackage>com.example.graphql.client</basePackage>
    <introspectionFile>${project.basedir}/src/main/graphql/schema.json</introspectionFile>
    <outputPackage>com.example.graphql.client</basePackage>
    <outputDirectory>${project.build.directory}/generated-sources/graphql-client</outputDirectory>
    <generateModelBuilder>true</generateModelBuilder>
    <useJavaBeansSemanticNaming>true</useJavaBeansSemanticNaming>
    <useSemanticNaming>true</useSemanticNaming>
    <nullableValueType>JAVA_OPTIONAL</nullableValueType>
    <suppressRawTypesWarning>false</suppressRawTypesWarning>
    <customTypeMap></customTypeMap>
</configuration>

Nullable Types

Available nullable types:

    ANNOTATED
    APOLLO_OPTIONAL
    GUAVA_OPTIONAL
    JAVA_OPTIONAL
    INPUT_TYPE

Custom Types

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

<configuration>
    ...
    <customTypeMap>
        <Long>java.lang.Long</Long>
    </customTypeMap>
</configuration>

Using the Client

Assuming a file named src/main/graphql/GetBooks.graphql is defined that contains a query named GetBooks against the given schema.json, the following code demonstrates how that query could be executed.

ApolloClient client = ApolloClient.builder()
    .serverUrl("https://example.com/graphql")
    .okHttpClient(new OkHttpClient.Builder()
        .addInterceptor(new Interceptor() {
            @Override
            Response intercept(Interceptor.Chain chain) throws IOException {
                chain.proceed(chain.request().newBuilder().addHeader("Authorization", "Basic cnllYnJ5ZTpidWJibGVzMTIz").build())
            }
        })
        .build())
    .build()

client.newCall(new GetBooks())
    .enqueue(new ApolloCall.Callback<GetBooks.Data>() {

    @Override public void onResponse(@NotNull Response<GetBooks.Data> response) {
        ...
    }

    @Override public void onFailure(@NotNull ApolloException t) {
        ...
    }
    });

Properties specified as nullable in the schema will have an java 8 java.util.optional type.

Contributors

  • Andrew Potter => apottere
  • William Yu => wiyu
  • Moncef AOUDIA => sparow199
  • Ryan Gardner => ryangardner
  • Abdullah Diab => mpcabd
  • Unknown => ddekkers
  • Unknown => mgrossmanexp