Back to Graphql Code Generator

java-apollo-android

website/src/pages/plugins/java/java-apollo-android.mdx

1.17.71.5 KB
Original Source

import { Callout } from '@theguild/components' import { PluginApiDocs, PluginHeader } from '@/components/plugin' import { pluginGetStaticProps } from '@/lib/plugin-get-static-props' export const getStaticProps = pluginGetStaticProps(__filename)

<PluginHeader />

Prepare your env

To get started with these plugins and preset, make sure you have the following installed:

  • Node.js (10 or later)
  • NPM or Yarn

Run the following in your Android project:

sh
npm init --yes
npm install graphql
npm install -D @graphql-codegen/cli @graphql-codegen/java-apollo-android

Then, create codegen.yml with the following configuration:

yaml
schema: POINT_TO_YOUR_SCHEMA
documents: POINT_TO_YOUR_GRAPHQL_OPERATIONS
generates:
  ./app/src/main/java/:
    preset: java-apollo-android
    config:
      package: 'com.my.app.generated.graphql'
      fragmentPackage: 'com.my.app.generated.graphql'
      typePackage: 'type'
    plugins:
      - java-apollo-android

<Callout>Also, make sure to add node_modules to your .gitignore file.</Callout>

To integrate with your Gradle build, you can add the following to your app Gradle file:

preBuild.doFirst {
  def proc = "yarn graphql-codegen".execute()
  proc.waitForProcessOutput(System.out, System.err)
}

build.dependsOn preBuild

This will make sure to run and generate output before each time you build your project.

Usage

You can find a repository with a working example using AppSync.

<PluginApiDocs />