Kotlin
| Package name | Weekly Downloads | Version | License | Updated |
|---|---|---|---|---|
@graphql-codegen/kotlin | Mar 8th, 2026 |
Installation
npm i -D @graphql-codegen/kotlinConfig API Reference
package
type: string
Customize the Java package name. The default package name will be generated according to the output file path.
Usage Examples
generates:
src/main/kotlin/my-org/my-app/Resolvers.kt:
plugins:
- kotlin
config:
package: custom.package.nameenumValues
type: EnumValuesMap
Overrides the default value of enum values declared in your GraphQL schema.
Usage Examples
config:
enumValues:
MyEnum:
A: 'foo'listType
type: string
default: Iterable
Allow you to customize the list type
Usage Examples
generates:
src/main/kotlin/my-org/my-app/Types.kt:
plugins:
- kotlin
config:
listType: MapwithTypes
type: boolean
default: false
Allow you to enable generation for the types
Usage Examples
generates:
src/main/kotlin/my-org/my-app/Types.kt:
plugins:
- kotlin
config:
withTypes: trueomitJvmStatic
type: boolean
default: false
Allow you to omit JvmStatic annotation
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
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
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
defaultScalarType: 'unknown'
},
},
},
};
export default config;scalars
type: ScalarsMap_1
Extends or overrides the built-in scalars and custom GraphQL scalars to a custom type.
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
scalars: {
ID: {
input: 'string',
output: 'string | number'
}
DateTime: 'Date',
JSON: '{ [key: string]: any }',
}
},
},
},
};
export default config;namingConvention
type: NamingConvention_1
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
Usage Examples
Override All Names
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
namingConvention: 'change-case-all#lowerCase',
},
},
},
};
export default config;Upper-case enum values
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
namingConvention: {
typeNames: 'change-case-all#pascalCase',
enumValues: 'change-case-all#upperCase',
}
},
},
},
};
export default config;Keep names as is
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
namingConvention: 'keep',
},
},
},
};
export default config;Remove Underscores
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
namingConvention: {
typeNames: 'change-case-all#pascalCase',
transformUnderscore: true
}
},
},
},
};
export default config;typesPrefix
type: string
default: (empty)
Prefixes all the generated types.
Usage Examples
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
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
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
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
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
useTypeImports: true
},
},
},
};
export default config;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). “mask” transforms the types for use with fragment masking. Useful when masked types are needed when not using the “client” preset e.g. such as combining it with Apollo Client’s data masking feature.
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.
importExtension
type: string[] | string (values: )
Append this extension to all imports. Useful for ESM environments that require file extensions in import statements.
extractAllFieldsToTypes
type: boolean
default: false
Extract all field types to their own types, instead of inlining them. This helps to reduce type duplication, and makes type errors more readable. It can also significantly reduce the size of the generated code, the generation time, and the typechecking time.
printFieldsOnNewLines
type: boolean
default: false
If you prefer to have each field in generated types printed on a new line, set this to true. This can be useful for improving readability of the resulting types, without resorting to running tools like Prettier on the output.
includeExternalFragments
type: boolean
default: false
Whether to include external fragments in the generated code. External fragments are not defined in the same location as the operation definition.
The kotlin plugin generates Kotlin classes for Enums and Input types.
You can use this plugin to generate Java enums based on your GraphQL schema, and it also generates a
type-safe Kotlin transformer for GraphQL input types.
Prepare your environment
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:
npm init --yes
npm install graphql
npm install -D @graphql-codegen/cli @graphql-codegen/kotlinThen, create codegen.yml with the following configuration:
schema: POINT_TO_YOUR_SCHEMA
documents: POINT_TO_YOUR_GRAPHQL_OPERATIONS
generates:
./app/src/Types.kt:
plugins:
- kotlinnode_modules to your .gitignore file.