GraphQL Import Macro

1.0.0 · abandoned · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

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));

view raw JSON →