Java Apollo Android

Package nameWeekly DownloadsVersionLicenseUpdated
@graphql-codegen/java-apollo-androidDownloadsVersionLicenseNov 1st, 2022

Installation

yarn add -D @graphql-codegen/java-apollo-android

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:

yarn init --yes
yarn add graphql
yarn add -D @graphql-codegen/cli @graphql-codegen/java-apollo-android

Then, create codegen.yml with the following configuration:

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
💡
Also, make sure to add node_modules to your .gitignore file.

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.

This plugin and presets creates generated mappers and parsers for a complete type-safe GraphQL requests, for developers that uses Apollo Android runtime.

Config API Reference

package

type: string

Customize the Java package name for the generated operations. The default package name will be generated according to the output file path.

Usage Examples

generates:
  ./app/src/main/java/:
    preset: java-apollo-android
    config:
      package: 'com.my.package.generated.graphql'
    plugins:
      - java-apollo-android
typePackage

type: string

Customize the Java package name for the types generated based on input types.

Usage Examples

generates:
  ./app/src/main/java/:
    preset: java-apollo-android
    config:
      typePackage: 'com.my.package.generated.graphql'
    plugins:
      - java-apollo-android
fragmentPackage

type: string

Customize the Java package name for the fragments generated classes.

Usage Examples

generates:
  ./app/src/main/java/:
    preset: java-apollo-android
    config:
      fragmentPackage: 'com.my.package.generated.graphql'
    plugins:
      - java-apollo-android
fileType

type: FileType

strictScalars

type: boolean default: false

Makes scalars strict.

If scalars are found in the schema that are not defined in scalars an error will be thrown during codegen.

Usage Examples

codegen.ts
 import type { CodegenConfig } from '@graphql-codegen/cli';
 
 const config: CodegenConfig = {
   // ...
   generates: {
     'path/to/file': {
       // plugins...
       config: {
         strictScalars: true,
       },
     },
   },
 };
 export default config;
defaultScalarType

type: string default: any

Allows you to override the type that unknown scalars will have.

Usage Examples

codegen.ts
 import type { CodegenConfig } from '@graphql-codegen/cli';
 
 const config: CodegenConfig = {
   // ...
   generates: {
     'path/to/file': {
       // plugins...
       config: {
         defaultScalarType: 'unknown'
       },
     },
   },
 };
 export default config;
scalars

type: ScalarsMap

Extends or overrides the built-in scalars and custom GraphQL scalars to a custom type.

namingConvention

type: NamingConvention default: change-case-all#pascalCase

Allow you to override the naming convention of the output. You can either override all namings, or specify an object with specific custom naming convention per output. The format of the converter must be a valid module#method. Allowed values for specific output are: typeNames, enumValues. You can also use "keep" to keep all GraphQL names as-is. Additionally, you can set transformUnderscore to true if you want to override the default behavior, which is to preserve underscores.

Available case functions in change-case-all are camelCase, capitalCase, constantCase, dotCase, headerCase, noCase, paramCase, pascalCase, pathCase, sentenceCase, snakeCase, lowerCase, localeLowerCase, lowerCaseFirst, spongeCase, titleCase, upperCase, localeUpperCase and upperCaseFirst See more

typesPrefix

type: string default: (empty)

Prefixes all the generated types.

Usage Examples

codegen.ts
 import type { CodegenConfig } from '@graphql-codegen/cli';
 
 const config: CodegenConfig = {
   // ...
   generates: {
     'path/to/file': {
       // plugins...
       config: {
         typesPrefix: 'I',
       },
     },
   },
 };
 export default config;
typesSuffix

type: string default: (empty)

Suffixes all the generated types.

Usage Examples

codegen.ts
 import type { CodegenConfig } from '@graphql-codegen/cli';
 
 const config: CodegenConfig = {
   // ...
   generates: {
     'path/to/file': {
       // plugins...
       config: {
         typesSuffix: 'I',
       },
     },
   },
 };
 export default config;
skipTypename

type: boolean default: false

Does not add __typename to the generated types, unless it was specified in the selection set.

Usage Examples

codegen.ts
 import type { CodegenConfig } from '@graphql-codegen/cli';
 
 const config: CodegenConfig = {
   // ...
   generates: {
     'path/to/file': {
       // plugins...
       config: {
         skipTypename: true
       },
     },
   },
 };
 export default config;
nonOptionalTypename

type: boolean default: false

Automatically adds __typename field to the generated types, even when they are not specified in the selection set, and makes it non-optional

Usage Examples

codegen.ts
 import type { CodegenConfig } from '@graphql-codegen/cli';
 
 const config: CodegenConfig = {
   // ...
   generates: {
     'path/to/file': {
       // plugins...
       config: {
         nonOptionalTypename: true
       },
     },
   },
 };
 export default config;
useTypeImports

type: boolean default: false

Will use import type {} rather than import {} when importing only types. This gives compatibility with TypeScript's "importsNotUsedAsValues": "error" option

Usage Examples

codegen.ts
 import type { CodegenConfig } from '@graphql-codegen/cli';
 
 const config: CodegenConfig = {
   // ...
   generates: {
     'path/to/file': {
       // plugins...
       config: {
         useTypeImports: true
       },
     },
   },
 };
 export default config;
dedupeFragments

type: boolean default: false

Removes fragment duplicates for reducing data transfer. It is done by removing sub-fragments imports from fragment definition Instead - all of them are imported to the Operation node.

inlineFragmentTypes

type: string default: inline

Whether fragment types should be inlined into other operations. "inline" is the default behavior and will perform deep inlining fragment types within operation type definitions. "combine" is the previous behavior that uses fragment type references without inlining the types (and might cause issues with deeply nested fragment that uses list types).

emitLegacyCommonJSImports

type: boolean default: true

Emit legacy common js imports. Default it will be true this way it ensure that generated code works with non-compliant bundlers.

Last updated on August 10, 2022