Registry / serialization / graphql.macro

graphql.macro

JSON →
library1.4.2jsnpmunverified

`graphql.macro` is a build-time tool that compiles GraphQL Abstract Syntax Trees (AST) using `babel-plugin-macros`. Its primary function is to eliminate the runtime parsing overhead associated with GraphQL query strings, transforming them into pre-parsed AST objects during the build process. The current stable version is 1.4.2. While not actively developed with frequent releases, it provides a stable solution for projects leveraging Babel macros to optimize GraphQL operations. Key differentiators include its integration with the `babel-plugin-macros` ecosystem, offering a declarative way to handle GraphQL documents without explicit Webpack loaders or complex Babel plugins, and replacing runtime libraries like `graphql-tag` for parsing. This approach streamlines the development workflow by allowing GraphQL documents to be imported directly or defined inline, with the macro handling the static transformation.

npm install graphql.macro
INSTALL
IMPORT
SIG · GRAPHQL.MACRO
G
graphql.macro
serializationjavascriptv1.4.2
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

loader
✓ import { loader } from 'graphql.macro';
✗ const { loader } = require('graphql.macro');
Used to import GraphQL files (e.g., `.graphql`, `.gql`) directly, compiling them into an AST object at build time. The CommonJS `require` syntax will not be processed by the macro system.
gql
✓ import { gql } from 'graphql.macro';
✗ import gql from 'graphql-tag';
Provides a tagged template literal similar to `graphql-tag` but performs the AST compilation at build time via the macro. Using `graphql-tag` imports runtime parsing.

Demonstrates how to configure Babel for `graphql.macro` and use both the `loader` and `gql` functions to compile GraphQL documents into AST objects at build time.

// .babelrc.js or babel.config.js module.exports = { plugins: ['babel-plugin-macros'], }; // src/myQuery.graphql // query UserProfile($id: ID!) { // user(id: $id) { // name // email // } // } // src/app.ts import { loader, gql } from 'graphql.macro'; import type { DocumentNode } from 'graphql'; // Compile a .graphql file at build time const UserProfileQuery: DocumentNode = loader('./myQuery.graphql'); // Compile an inline GraphQL string at build time const AllUsersQuery: DocumentNode = gql` query AllUsers { users { id name } } `; console.log('User Profile Query AST:', JSON.stringify(UserProfileQuery, null, 2)); console.log('All Users Query AST:', JSON.stringify(AllUsersQuery, null, 2)); // UserProfileQuery and AllUsersQuery are now plain JavaScript objects representing the GraphQL AST.
Debug
Known issues
breakingThis package requires `babel-plugin-macros` to be correctly installed and configured in your Babel environment. `graphql.macro` will not function without this prerequisite, as it is fundamentally a macro.
fix
Install `babel-plugin-macros` (`yarn add -D babel-plugin-macros` or `npm install --save-dev babel-plugin-macros`) and add it to your Babel configuration (e.g., `plugins: ['babel-plugin-macros']` in `.babelrc` or `babel.config.js`).
affects: >=1.0.0
gotcha`graphql.macro` performs build-time compilation, meaning the GraphQL AST is embedded directly into your compiled JavaScript bundle. While this eliminates runtime parsing overhead, any changes to GraphQL documents (e.g., `.graphql` files or `gql` template literals) require a full rebuild to take effect.
fix
Ensure your development build pipeline is efficient for rapid iteration. For projects with hot module reloading, confirm that changes to GraphQL files are properly detected and trigger a macro re-processing.
affects: >=1.0.0
deprecatedThis package provides a build-time alternative to runtime GraphQL parsing libraries like `graphql-tag`. While `graphql-tag` is not deprecated, using both `graphql.macro` and `graphql-tag` for similar purposes can lead to inconsistencies, larger bundle sizes, or confusion regarding parsing behavior.
fix
Standardize on either build-time macros (`graphql.macro`) or a runtime parsing library (`graphql-tag`) consistently across your project to maintain a clear parsing strategy and optimize bundle size.
affects: >=1.0.0
Errors
Common errors & fixes
Module build failed (from ../node_modules/babel-loader/lib/index.js): Error: Cannot find module 'babel-plugin-macros/dist/macro-context'
`babel-plugin-macros` is not installed or not correctly configured in your Babel setup, preventing the macro from being resolved.
fix
Run `yarn add -D babel-plugin-macros` or `npm install --save-dev babel-plugin-macros` and ensure your Babel configuration (e.g., `.babelrc` or `babel.config.js`) explicitly includes `babel-plugin-macros` in the plugins array.
Cannot find module './myQuery.graphql' or its corresponding type declarations.ts(2307)
TypeScript does not natively understand or provide type declarations for `.graphql` or `.gql` file imports, leading to a module resolution error.
fix
Create a declaration file (e.g., `src/custom.d.ts`) in your project with `declare module '*.graphql' { const content: import('graphql').DocumentNode; export default content; }` to inform TypeScript about the module type for GraphQL files. Adjust for `.gql` if necessary.
TypeError: Object(...) is not a function (when calling gql or loader)
The files containing `graphql.macro` imports are not being processed by Babel with `babel-plugin-macros` enabled. The macro transformation is not occurring, leaving the imports as unresolved or empty objects.
fix
Verify your Babel configuration to ensure that the files using `graphql.macro` are within Babel's transpilation scope and that `babel-plugin-macros` is listed and active in your plugin configuration.
Upgrade
Version history
1.4.2latest on npm
Audit
Dependencies
babel-plugin-macrosrequired`graphql.macro` is implemented as a Babel macro and fundamentally requires `babel-plugin-macros` to be installed and configured in your Babel setup to function.
Agent activity
4 hits · last 30 days
node
4
Resources
graphql.macro — npm install graphql.macro · libregistry