Registry / testing / graphql-document-analyzer

graphql-document-analyzer

JSON →
library0.7.0jsnpmunverified

A resilient GraphQL document analyzer that extends GraphQL's built-in `parse` to gracefully handle validation errors, top-level comments, and invalid sections. Unlike the standard parser, it never throws on malformed input, instead representing invalid parts as `InvalidOperationDefinition`, `InvalidFragmentDefinition`, and `Ignored` nodes. It also supports interpolation with previous valid revisions to recover approximate ASTs for in-progress edits, a section-aware visitor, and a print function that preserves all original content including comments and invalid parts. The package is currently at version 0.7.0 and ships TypeScript type definitions.

npm install graphql-document-analyzer
INSTALL
IMPORT
SIG · GRAPHQL-DOCUMENT-A
G
graphql-document-analyzer
testingjavascriptv0.7.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

analyze
import { analyze } from 'graphql-document-analyzer'
import analyze from 'graphql-document-analyzer'
Default export does not exist; use named import. TypeScript types are included.
interpolate
import { interpolate } from 'graphql-document-analyzer'
import { interpolate } from 'graphql-document-analyzer/interpolate'
All exports are at the package root; no subpath exports.
print
import { print } from 'graphql-document-analyzer'
import { print } from 'graphql'
Do not confuse with graphql's `print`; this one preserves comments and invalid nodes.
visit
import { visit } from 'graphql-document-analyzer'
import { visit } from 'graphql'
Section-aware visitor; different from graphql's `visit`.

Shows how to analyze a GraphQL document with a comment and an invalid query, iterate over sections, and print back the original content.

import { analyze, print } from 'graphql-document-analyzer'; const source = `# My comment query { field }`; const doc = analyze(source); doc.sections.forEach(section => { if (section.kind === 'Ignored') { console.log('Ignored:', section.value); } else if (section.kind === 'InvalidOperationDefinition') { console.log('Invalid:', section.value); } }); // Print back the original document including comments const printed = print(doc); console.log(printed === source); // true
Debug
Known issues
gotchaThe `analyze` function never throws even for completely invalid documents; check `definitions` length instead of catching errors.
fix
Use `doc.definitions.length > 0` to determine if valid definitions exist.
affects: >=0.0.0
gotcha`print` outputs the exact source including comments and invalid parts; it is not a pretty printer.
fix
If you need formatted output, use `graphql`'s `print` on the `definitions` only.
affects: >=0.0.0
gotchaInterpolation requires a reference document from a previous `analyze` call; passing a raw string will fail silently.
fix
Always pass the return value of `analyze` as the reference.
affects: >=0.0.0
breakingVersion 0.7.0 drops support for graphql@14; peer dependency is graphql@^15.
fix
Upgrade graphql to version 15 or later.
affects: >=0.7.0
Errors
Common errors & fixes
TypeError: graphql_document_analyzer_1.analyze is not a function
Default import instead of named import.
fix
Use `import { analyze } from 'graphql-document-analyzer'`
Cannot find module 'graphql-document-analyzer/interpolate'
Subpath import does not exist.
fix
Use `import { interpolate } from 'graphql-document-analyzer'`
Error: Cannot use GraphQL v14 with this package. Must be >= 15.
Peer dependency mismatch.
fix
Update graphql to version 15 or later.
Upgrade
Version history
0.7.0latest on npm
Audit
Dependencies
graphqloptionalPeer dependency for GraphQL types and utilities
Agent activity
4 hits · last 30 days
node
4
Resources
graphql-document-analyzer — npm install graphql-document-analyzer · libregistry