GithubHelp home page GithubHelp logo

siathalysedi / apollo-client-maven-plugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cox-automotive/apollo-client-maven-plugin

0.0 3.0 0.0 29 KB

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

License: MIT License

Groovy 43.44% Kotlin 56.56%

apollo-client-maven-plugin's Introduction

Apollo GraphQL Client Code Generation Maven Plugin

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>0.4.0</version>
    </dependency>
  2. Add the code generator plugin to your project's build (if codegen is desired):
    <plugin>
        <groupId>com.coxautodev</groupId>
        <artifactId>apollo-client-maven-plugin</artifactId>
        <version>1.1.0</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>
    <outputDirectory>${project.build.directory}/generated-sources/graphql-client</outputDirectory>
    <basePackage>com.example.graphql.client</basePackage>
    <schemaFile>${project.basedir}/src/main/graphql/schema.json</schemaFile>
    <addSourceRoot>true</addSourceRoot>
</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()
    
Optional<GetBooks.Data> data = client.newCall(new GetBooks()).execute().data()

if(data.isPresent()) {
    System.out.println("Book count: " + data.get().books().size());
}

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

apollo-client-maven-plugin's People

Contributors

apottere avatar ryangardner avatar ddekkers avatar

Watchers

Arnstein Henriksen avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.