Apollo Codegen TypeScript Generator
Apollo Codegen TypeScript Generator is an internal module of the now-deprecated `apollographql/apollo-tooling` monorepo. Its primary function was to facilitate the generation of TypeScript types from GraphQL schemas and operation documents, predominantly via the `apollo client:codegen` CLI command. The package's last version, 0.40.9, was released around 2020. This package, and the `apollo` CLI commands it powered, are no longer actively maintained. Apollo has officially deprecated `apollo client:codegen`, directing users to adopt `GraphQL Code Generator` for type generation. This shift marks the end of its release cadence and dedicated support. Its key differentiator was its deep integration within the Apollo CLI ecosystem, providing a streamlined, opinionated approach to type generation for Apollo Client users. However, this opinionated nature also meant less flexibility compared to more customizable alternatives like `GraphQL Code Generator`.
Common errors
-
Error: Command 'codegen' is deprecated.
cause Attempting to use the `apollo codegen:generate` command from a recent or deprecated version of the Apollo CLI.fixUpdate your `package.json` scripts to use `graphql-code-generator` CLI instead. Consult the GraphQL Code Generator documentation for migration guides. -
Cannot find module 'apollo-codegen-typescript'
cause This package is an internal module of `apollo-tooling` and not intended for direct installation or import by most applications. It's likely you're trying to use it in a way it wasn't designed for, or it's simply not installed as a dependency.fixAvoid direct imports. If you need GraphQL type generation, use `GraphQL Code Generator` and install its relevant packages (e.g., `@graphql-codegen/cli`). -
TypeError: Cannot read property 'kind' of undefined
cause This error often occurs when the `apollo-codegen-typescript` module, or the `apollo` CLI, encounters an invalid or unexpected GraphQL schema or operation definition that it cannot parse, especially with newer GraphQL features it doesn't support.fixEnsure your GraphQL schema and operation documents are valid. More importantly, migrate to `GraphQL Code Generator` to benefit from modern GraphQL spec support and active maintenance.
Warnings
- breaking The `apollo client:codegen` command, which was the primary way to use this module, has been officially deprecated. The entire `apollo-tooling` repository is in maintenance mode and slated for full deprecation.
- gotcha This package is no longer actively maintained. Security vulnerabilities and bug fixes are unlikely to be addressed. Using it in production environments is risky due to potential unpatched issues.
- gotcha Compatibility issues may arise with newer versions of Node.js, TypeScript, or the `graphql` library due to lack of updates. The package's last release (0.40.9) is from around 2020.
Install
-
npm install apollo-codegen-typescript -
yarn add apollo-codegen-typescript -
pnpm add apollo-codegen-typescript
Imports
- generate
const generate = require('apollo-codegen-typescript');// Direct programmatic import is not recommended as this package is abandoned. // Most users interacted via the Apollo CLI. If you absolutely must, it would be: // import { generate } from 'apollo-codegen-typescript';
Quickstart
/*
This quickstart demonstrates the historical usage of Apollo Codegen via the Apollo CLI.
This command and the underlying tooling are now deprecated and abandoned.
Recommended alternative: GraphQL Code Generator (https://www.graphql-code-generator.com/)
*/
// 1. Install the (now deprecated) Apollo CLI globally or locally
// npm install -g apollo@2 # Or use npx for one-off execution
// 2. Define your GraphQL schema (e.g., schema.graphql or introspect from a URL)
// For demonstration, let's create a dummy schema.graphql
// You would typically replace this with your actual schema.
// echo 'type Query { hello: String }' > schema.graphql
// 3. Define your GraphQL operations (e.g., in a .tsx or .graphql file)
// For this example, let's assume you have a query in src/MyComponent.tsx:
// import { gql } from '@apollo/client';
// const HELLO_QUERY = gql`query HelloQuery { hello }`;
// 4. Run the deprecated codegen command to generate TypeScript types
// This command assumes a schema.graphql file and operations in src/**/*.tsx
// It will output types to 'types' directory and a global types file.
npx apollo codegen:generate \
--localSchemaFile=schema.graphql \
--target=typescript \
--includes=src/**/*.tsx \
--tagName=gql \
--addTypename \
--globalTypesFile=src/types/global-types.ts \
types
console.log("If successful, TypeScript types for your GraphQL operations would be generated.");
console.log("Remember, this tool is deprecated. Migrate to GraphQL Code Generator.");