GraphQL JS Tree Parser
graphql-js-tree is a JavaScript library for parsing GraphQL Schema Definition Language (SDL) and GraphQL queries into a simplified, pure graph JSON structure. Unlike the standard `graphql-js` parser, which retains extensive text-specific Abstract Syntax Tree (AST) information, `graphql-js-tree` focuses on generating a more streamlined representation. This simplified structure makes it significantly easier to work with programmatically, abstracting away much of the raw AST complexity. The current stable version is 3.0.4. It serves as a foundational component for larger projects like `graphql-zeus` and `graphql-editor`, providing a pragmatic approach to GraphQL parsing. While a precise release cadence isn't explicitly specified, its integral role in other active projects suggests ongoing maintenance and development.
Common errors
-
Cannot find module 'graphql-js-tree' or its corresponding type declarations.
cause The `graphql-js-tree` package is not installed in the project, or the import path is incorrect, or TypeScript cannot find its declaration files.fixEnsure the package is installed: `npm install graphql-js-tree` or `yarn add graphql-js-tree`. Verify the import statement for correct path and symbol names. -
TypeError: Parser.parse is not a function
cause Attempting to call `Parser` directly as a function or importing `Parser` incorrectly (e.g., as a default import when it's a named export).fixEnsure `Parser` is imported as a named export: `import { Parser } from 'graphql-js-tree';` and then called as `Parser.parse(...)`.
Warnings
- breaking Upgrading from `graphql-js-tree` v1.x to v3.x involves significant breaking changes. The internal API for `Parser` and related utilities has been refactored to achieve a 'simplier structure' as described in release notes. Code written for v1.x will likely require substantial updates.
- gotcha Ensure your project's `graphql` package version precisely matches the peer dependency requirement (`^16.0.0 || ^17.0.0`). Mismatches can lead to cryptic runtime errors due to different GraphQL AST node structures or API incompatibilities from the underlying `graphql-js` library.
Install
-
npm install graphql-js-tree -
yarn add graphql-js-tree -
pnpm add graphql-js-tree
Imports
- Parser, TreeToGraphQL
const { Parser, TreeToGraphQL } = require('graphql-js-tree');import { Parser, TreeToGraphQL } from 'graphql-js-tree'; - parseGql
import parseGql from 'graphql-js-tree';
import { parseGql } from 'graphql-js-tree'; - parseGqlTrees
const gqlString = parseGqlTrees(parsedTrees); // Missing import
import { parseGqlTrees } from 'graphql-js-tree';
Quickstart
import { Parser, TreeToGraphQL } from 'graphql-js-tree';
const schemaFileContents = `
type Query{
hello: String!
greeting(name: String!): String!
}
type Mutation {
setName(name: String!): String!
}
schema{
query: Query
mutation: Mutation
}
`;
// Parse a GraphQL SDL schema into the simplified tree structure
const parsedSchema = Parser.parse(schemaFileContents);
// In a real application, you might process or transform 'parsedSchema' here.
// For demonstration, we'll log a portion of it.
// console.log(JSON.stringify(parsedSchema, null, 2));
// Convert the simplified tree back into a GraphQL SDL string
const graphqlString = TreeToGraphQL.parse(parsedSchema);
console.log("Original Schema:\n", schemaFileContents);
console.log("----------------------------------");
console.log("Parsed and Reconstructed Schema:\n", graphqlString);
// This demonstrates both parsing into the internal tree format and
// reconstructing the SDL, validating the transformation process.