Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

Generate a Java/Kotlin GraphQL client based on introspection data and predefined queries.
https://github.com/aoudiamoncef/apollo-client-maven-plugin

apollo apollo-client apollographql graphql graphql-client graphql-java graphql-query-builder graphql-tools maven-plugin query-builder query-generator

Last synced: about 2 months ago
JSON representation

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

Awesome Lists containing this project

README

        

= Apollo GraphQL Client Code Generation Maven Plugin

:project-owner: aoudiamoncef
:project-name: apollo-client-maven-plugin
:project-groupId: com.github.aoudiamoncef
:project-artifactId: apollo-client-maven-plugin-parent
:project-version: 7.0.0

image:https://github.com/{project-owner}/{project-name}/workflows/Build/badge.svg["Build Status",link="https://github.com/{project-owner}/{project-name}/actions"]
image:https://img.shields.io/maven-central/v/{project-groupId}/{project-artifactId}.svg[Download,link="https://search.maven.org/#search|ga|1|g:{project-groupId} AND a:{project-artifactId}"]
image:https://snyk.io/test/github/{project-owner}/{project-name}/badge.svg[Known Vulnerabilities,link=https://snyk.io/test/github/{project-owner}/{project-name}]
image:https://img.shields.io/badge/License-MIT-yellow.svg[License: MIT,link=https://opensource.org/licenses/MIT]

Maven plugin that calls the https://github.com/apollographql/apollo-kotlin[Apollo Kotlin compiler] to generate your Java/Kotlin sources.

== Usage

* A simple usage example can be found in https://github.com/{project-owner}/{project-name}/tree/main/apollo-client-maven-plugin-tests[plugin tests].

* A complete project with https://github.com/{project-owner}/spring-boot-apollo-graphql[Spring Boot].

=== Apollo 2 support

* Go to support https://github.com/aoudiamoncef/apollo-client-maven-plugin/tree/support/apollo-2[branch].

=== Getting Started

. Add the apollo runtime library to your project's dependencies:
+
[source,xml]
----


com.apollographql.apollo3
apollo-runtime
3.8.2

----

. Add the code generator plugin to your project's build:
+
[source,xml,subs="attributes+"]
----


com.github.aoudiamoncef
apollo-client-maven-plugin
{project-version}



generate





example

com.example.graphql.client







----

. Create a file `src/main/graphql/SERVICE_NAME/schema.json` with the JSON results of an https://gist.github.com/aoudiamoncef/a59527016e16a2d56309d62e01ff2348[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`.
. Create files for each query you'd like to generate classes for under `src/main/graphql/SERVICE_NAME`:
.. Query file names must match the name of the query they contain
.. Query files must end with `.graphql` OR `.gql` by default.
.. Any subdirectories under `src/main/graphql/SERVICE_NAME` are treated as extra package names to append to `schemaPackageName` in the plugin config.
. Run `mvn generate-sources` to generate classes for your queries.

. If you run into compilation issues such as `package com.apollographql.apollo3.api does not exist`, you may need to explicitly add the following runtime libraries to your pom.xml
+
[source,xml]
---

com.apollographql.apollo3
apollo-api-jvm
3.8.2


com.apollographql.apollo3
apollo-runtime-jvm
3.8.2

---

=== Configuration Options

All plugin configuration properties and their defaults:

[source,xml]
----




true
true
false
${project.basedir}/src/main/graphql/lahzouz
${project.basedir}/src/main/graphql/lahzouz/schema.json

**/*.graphql
**/*.gql
**/*.json
**/*.sdl"



lahzouz

${project.build.directory}/generated-sources/graphql-client/lahzouz/


${project.build.directory}/generated-sources/graphql-client/lahzouz/test/


${project.build.directory}/generated-sources/graphql-client/lahzouz/debug/

false


${project.basedir}/src/main/graphql/lahzouz/



persistedQueryManifest
com.lahzouz.apollo.graphql.client
com.lahzouz.apollo.graphql.client
OPERATION
true
false
false
false
false
false
false
true
false
false
false
false
false
NONE
true
JAVA



${project.build.directory}/generated/metadata/apollo/lahzouz/metadata.json




false


${project.basedir}/src/main/graphql/lahzouz/schema.json
10
10
10
false
false
false




lahzouz



----

==== Custom Types

To use https://www.apollographql.com/docs/kotlin/essentials/custom-scalars[custom Scalar Types] you need to
define mapping configuration then register your custom adapter:

[source,xml]
----

...

java.time.LocalDate

...

----

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

[source,java]
----
import com.apollographql.apollo3.api.Adapter;
import com.apollographql.apollo3.api.CustomScalarAdapters;
import com.apollographql.apollo3.api.json.JsonReader;
import com.apollographql.apollo3.api.json.JsonWriter;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class LocalDateGraphQLAdapter implements Adapter {
private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ISO_DATE;

@Override
public LocalDate fromJson(@NotNull final JsonReader reader, @NotNull final CustomScalarAdapters customScalarAdapters) throws IOException {
String dateString = reader.nextString();
return LocalDate.parse(dateString, dateFormatter);
}

@Override
public void toJson(@NotNull final JsonWriter writer, @NotNull final CustomScalarAdapters customScalarAdapters, final LocalDate value) throws IOException {
writer.value(value.format(dateFormatter));
}
}
----

[source,xml]
----

...


java.time.LocalDate
new com.example.LocalDateGraphQLAdapter()


...

----

==== Test Sources

To generate sources for tests:
[source,xml]
----

...
false
true

example
${project.basedir}/target/generated-test-sources/apollo/example
${project.basedir}/target/generated-test-sources/apollo/example/debug
${project.basedir}/target/generated-test-sources/apollo/example/debug

...

----

=== Using Apollo Client

See https://www.apollographql.com/docs/kotlin/[documentation]