Registry / serialization / graphql-import-macro

graphql-import-macro

JSON →
library1.0.0jsnpmunverified

graphql-import-macro is a utility library designed to parse and expand GraphQL import statements within GraphQL document definitions. It aims to provide a reference implementation for a relatively standardized GraphQL import syntax, emphasizing minimal dependencies. The library enables the DRY principle in GraphQL documents by allowing modularization and reuse of schema fragments. The current stable version is 1.0.0, released in April 2020. There have been no subsequent updates, suggesting it is no longer actively maintained. Its primary differentiator is its focus on being a lightweight, standalone parser for tools that need to support GraphQL imports, without aiming to become part of the official GraphQL specification.

npm install graphql-import-macro
INSTALL
IMPORT
SIG · GRAPHQL-IMPORT-MAC
G
graphql-import-macro
serializationjavascriptv1.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
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 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

processDocumentImports
import { processDocumentImports } from 'graphql-import-macro';
const { processDocumentImports } = require('graphql-import-macro');
Primarily designed for ES modules. CommonJS `require` might work but isn't the idiomatic usage shown in documentation.
parse
import { parse } from 'graphql';
import parse from 'graphql';
The `parse` function is a named export from the `graphql` package, not a default export.
Source
import { Source } from 'graphql';
const { Source } = require('graphql');
The `Source` class is a named export from the `graphql` package, essential for attaching path information to ASTs processed by this macro.

Demonstrates how to parse a GraphQL document, process its imports, and log the resolved AST.

import { parse, Source } from "graphql"; import { processDocumentImports } from "graphql-import-macro"; async function loadAndProcessGraphQL(content, path) { // Ensure the Source is instantiated with both content and path const ast = parse(new Source(content, path)); // Process and expand GraphQL import directives const resolvedAst = await processDocumentImports(ast); return resolvedAst; } // Example usage (replace with actual file content and path) const sampleGraphQLContent = ` #import './another.graphql' type Query { hello: String } `; const sampleGraphQLPath = 'schema.graphql'; loadAndProcessGraphQL(sampleGraphQLContent, sampleGraphQLPath) .then(ast => console.log(JSON.stringify(ast, null, 2))) .catch(error => console.error('Error processing GraphQL:', error));
Debug
Known issues
gotchaThe library has not been updated since April 2020. It may not be compatible with newer versions of the `graphql` package or recent changes in the GraphQL specification, potentially leading to parsing errors or unexpected behavior.
fix
Thoroughly test with your specific `graphql` package version. Consider alternative solutions for GraphQL imports if compatibility issues arise with newer GraphQL versions.
affects: >=1.0.0
gotchaThe `#import` syntax utilized by this library is a convention and not part of the official GraphQL specification. Tools or clients that do not explicitly support this convention will not recognize these directives.
fix
Ensure all tools in your GraphQL workflow (e.g., build tools, linters, servers) are aware of and correctly process the expanded GraphQL documents, or implement custom pre-processing steps.
affects: >=1.0.0
gotchaThe library expects `DocumentNode` instances to be created from `Source` objects that include path information. Omitting the path will result in errors when `processDocumentImports` attempts to resolve relative imports.
fix
Always instantiate `graphql.Source` with both the content string and the file path (e.g., `new Source(content, path)`).
affects: >=1.0.0
Errors
Common errors & fixes
Error: DocumentNode must be created from a Source instantiated with at least two parameters.
The `parse` function was called with a `Source` object missing the `name` (path) argument.
fix
Ensure `graphql.Source` is created with both the GraphQL content and its originating file path: `new Source(content, 'path/to/file.graphql')`.
TypeError: Cannot read properties of undefined (reading 'processDocumentImports')
Incorrect import statement or `graphql-import-macro` not installed/found.
fix
Verify that `graphql-import-macro` is installed (`npm install graphql-import-macro`) and that you are using the correct named import: `import { processDocumentImports } from 'graphql-import-macro';`.
Cannot find module 'graphql'
The peer dependency `graphql` is not installed in the project.
fix
Install the `graphql` package: `npm install graphql` (or `yarn add graphql`).
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
graphqlrequiredCore dependency for parsing and working with GraphQL ASTs. This library builds upon its DocumentNode structure.
Agent activity
6 hits · last 30 days
node
6
Resources
graphql-import-macro — npm install graphql-import-macro · libregistry