Registry / serialization / graphql-tag.macro

graphql-tag.macro

JSON →
library2.0.1jsnpmunverified

graphql-tag.macro is a Babel macro that inlines GraphQL query parsing at build time, replacing the runtime call to graphql-tag's gql tag function with the pre-parsed AST. Version 2.0.1 is current, with TypeScript definitions added in 2.1.0. It requires babel-plugin-macros and works with any Babel-based build system. Unlike runtime graphql-tag, this macro eliminates parsing overhead in production bundles. It supports fragment interpolation and is primarily used in GraphQL client projects (Apollo, URQL). Release cadence is low; last major version bumped deps for compatibility.

npm install graphql-tag.macro
INSTALL
IMPORT
SIG · GRAPHQL-TAG.MACRO
G
graphql-tag.macro
serializationjavascriptv2.0.1
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.

gql
✓ import gql from 'graphql-tag.macro'
✗ import { gql } from 'graphql-tag'
Default import from the macro package. Named import from graphql-tag is the standard runtime version, not the macro.
gql
✓ const gql = require('graphql-tag.macro').default
✗ const gql = require('graphql-tag.macro')
In CommonJS, the macro exports as default, so you need .default. The non-default export may not work as expected.

Shows installation, Babel macro setup, and usage of gql tag from the macro to inline parsed AST.

// Install: npm install --save-dev graphql-tag.macro babel-plugin-macros // Then configure babel (in .babelrc or babel.config.js): // { // "plugins": ["macros"] // } // Now in your code: import gql from 'graphql-tag.macro'; const HELLO_QUERY = gql` query Hello { hello { world } } `; export default HELLO_QUERY; // At build time, HELLO_QUERY becomes a pre-parsed AST object. // No graphql-tag runtime needed in bundle.
Debug
Known issues
gotchaImport path must be 'graphql-tag.macro' not 'graphql-tag' – using the runtime version will not inline and adds bundle size.
fix
Use import gql from 'graphql-tag.macro'
affects: >=0.0.0
gotchaRequire() in Node requires .default – const gql = require('graphql-tag.macro').default
fix
Add .default to the require() result
affects: >=2.0.0
breakingv2.0.0 dropped support for older versions of babel-plugin-macros; requires at least version 2.0.0 of the plugin.
fix
Update babel-plugin-macros to >=2.0.0
affects: <2.0.0
Errors
Common errors & fixes
Cannot find module 'graphql-tag.macro'
Package not installed or missing from dependencies
fix
npm install --save-dev graphql-tag.macro
gql is not a function
Incorrect import – using named import instead of default, or missing .default in require
fix
Use import gql from 'graphql-tag.macro' or require('graphql-tag.macro').default
Error: BabelMacro: Macro 'graphql-tag.macro' is not activated. Did you forget to add babel-plugin-macros to your plugins?
babel-plugin-macros not configured in Babel
fix
Add 'macros' to Babel plugins: { "plugins": ["macros"] }
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies
graphql-tagoptionalRuntime dependency for graphql-tag itself used by some configurations
babel-plugin-macrosrequiredRequired peer – the macro system needed to run this macro
Agent activity
6 hits · last 30 days
node
6
Resources
graphql-tag.macro — npm install graphql-tag.macro · libregistry