Relay Compiler TypeScript Language Plugin
This package served as a language plugin for `relay-compiler`, providing TypeScript support, including the emission of type definitions for Relay artifacts. It enabled Relay users to leverage TypeScript for their GraphQL operations and component props. The current stable version is 15.0.1. Historically, it filled a critical gap by adding TypeScript capabilities to Relay's build process. However, as of Relay v13, TypeScript support is natively integrated into the core `relay-compiler`, rendering this standalone plugin obsolete. Its release cadence was tied to major Relay versions and bug fixes, but active development has ceased due to its superseded status. Developers are now advised to use the built-in TypeScript features of `relay-compiler` directly.
Common errors
-
babel-plugin-relay cannot find the locations where the `graphql` function is being used (e.g., 'react_relay_1.graphql').
cause The TypeScript compiler (`tsc`) generated CommonJS-style imports and namespaces the `graphql` function, making it unidentifiable by `babel-plugin-relay`.fixIn your `tsconfig.json`, set `compilerOptions.module` to `ES2015` or higher, and `compilerOptions.target` to `ES2020` or a compatible modern target. This ensures `tsc` outputs ES module syntax that `babel-plugin-relay` can correctly parse. -
Unexpected module loading errors or type conflicts in the browser during development with React Hot Loader (RHL).
cause React Hot Loader's module replacement mechanism can struggle with dynamically generated TypeScript type definition files, leading to runtime errors or incorrect hot module updates.fixThis package has known incompatibilities with RHL. The recommended solution is to transition to `react-refresh` (used in tools like Create React App v4+), which offers more robust hot reloading capabilities for modern React applications. Alternatively, configure RHL to exclude Relay-generated artifact directories from its processing. -
Error: `relay-compiler-language-typescript` requires `typescript@>=4.5.0` to be installed.
cause Your project's `typescript` version is older than the minimum required by `relay-compiler-language-typescript` v15 and above.fixUpdate your project's `typescript` dependency to `^4.5.0` or a newer compatible version (e.g., `npm install typescript@^4.5.0` or `yarn add typescript@^4.5.0`).
Warnings
- breaking This package is explicitly marked as obsolete. Relay version 13.0.0 and later now include native TypeScript support directly within the `relay-compiler`. Continued use of `relay-compiler-language-typescript` is discouraged.
- breaking `relay-compiler-language-typescript` v15.0.0 introduced breaking changes related to TypeScript 4.5 type generation, and requires a minimum TypeScript version of 4.5.0.
- breaking `relay-compiler-language-typescript` v14.0.0 dropped support for TypeScript 3.
- gotcha Improper `tsconfig.json` module settings can prevent `babel-plugin-relay` from finding `graphql` function usages, leading to compilation failures.
- gotcha React Hot Loader (RHL) is known to have compatibility issues with generated code, including Relay's typing artifacts, which can cause module loading errors in the browser during hot reloading.
Install
-
npm install relay-compiler-language-typescript -
yarn add relay-compiler-language-typescript -
pnpm add relay-compiler-language-typescript
Imports
- Relay Compiler Configuration
import * as RelayTS from 'relay-compiler-language-typescript';
relay-compiler --language typescript --src ./src --schema data/schema.graphql --artifactDirectory ./src/__generated__
- TypeScript Type Artifacts
import { MyComponent_data$key } from 'relay-compiler-language-typescript';import { MyComponent_data$key } from './__generated__/MyComponent_data.graphql'; - Custom Headers (banner option)
import { addBanner } from 'relay-compiler-language-typescript'{ "compilerOptions": { "banner": "/* © 2021 Example.org - @generated code */" } }
Quickstart
{
"devDependencies": {
"graphql": "^15.7.0",
"relay-compiler": ">=12.0.0",
"typescript": ">=4.5.0",
"relay-compiler-language-typescript": "^15.0.0"
},
"scripts": {
"relay": "relay-compiler --src ./src --schema data/schema.graphql --language typescript --artifactDirectory ./src/__generated__"
}
}
// tsconfig.json
{
"compilerOptions": {
"module": "ES2015",
"target": "ES2020"
}
}
// .babelrc
{
"plugins": [["relay", { "artifactDirectory": "./src/__generated__" }]]
}