A Babel plugin that transforms GraphQL tagged template literals (like graphql`...`) in Relay applications into runtime artifacts. Currently at version 20.1.1, it is part of the Relay framework maintained by Meta, with regular releases every few months. Key differentiators vs. alternatives: it works seamlessly with Relay's compiler and runtime, supports modern JavaScript and TypeScript, and integrates with Babel's plugin system. Unlike generic GraphQL tools, this plugin is tailored specifically for Relay's data masking and fragment composition patterns, requiring the Relay compiler to generate the necessary artifacts.
npm install babel-plugin-relayVerified import paths — ran on the pinned version, not inferred.
Configures babel-plugin-relay with Babel and demonstrates usage with a React component that fetches a GraphQL query.
Add @alias directive to all fragments that are conditionally fetched. Use relay-compiler's --validate to check.
Rename `customScalars` to `customScalarTypes` in relay config.
Add relay-compiler to your build pipeline (e.g., npm run relay or integrate with webpack custom plugin).
Use require('babel-plugin-relay') or reference as 'relay' in Babel plugins array.Order plugins so 'relay' appears before '@babel/plugin-transform-template-literals' and '@babel/preset-react'.
Remove `enforceSchema` from babel-plugin-relay options and define schema path in relay.config.js.
Ensure relay-compiler has run and an artifact file exists for the query. Check schema path and run relay-compiler.
Ensure babel-plugin-relay is added to Babel plugins and that the bundle's Babel configuration includes it.
Run relay-compiler with correct config; check artifactDirectory option in babel-plugin-relay config.
Check the GraphQL string for syntax errors, ensure only one operation per template literal.