GraphQL JS Tree Parser

3.0.4 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates parsing a GraphQL SDL schema into `graphql-js-tree`'s simplified internal representation and then reconstructing it back into an SDL string, showcasing the core `Parser` and `TreeToGraphQL` functionalities.

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.

view raw JSON →