GraphQL Import Macro
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.
Common errors
-
Error: DocumentNode must be created from a Source instantiated with at least two parameters.
cause The `parse` function was called with a `Source` object missing the `name` (path) argument.fixEnsure `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')
cause Incorrect import statement or `graphql-import-macro` not installed/found.fixVerify 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'
cause The peer dependency `graphql` is not installed in the project.fixInstall the `graphql` package: `npm install graphql` (or `yarn add graphql`).
Warnings
- gotcha The 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.
- gotcha The `#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.
- gotcha The 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.
Install
-
npm install graphql-import-macro -
yarn add graphql-import-macro -
pnpm add graphql-import-macro
Imports
- processDocumentImports
const { processDocumentImports } = require('graphql-import-macro');import { processDocumentImports } from 'graphql-import-macro'; - parse
import parse from 'graphql';
import { parse } from 'graphql'; - Source
const { Source } = require('graphql');import { Source } from 'graphql';
Quickstart
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));