graphql-js
graphql-js copied to clipboard
Feature request: prettyPrint
Request: Add a Pretty-Printing function to go from AST => well-formatted string
This is a utility I've wanted to exist as part of the core library for a long time. @adek05 began a simple implementation of this in #1557. The Prettier library has even implemented a version of this: https://github.com/prettier/prettier/tree/master/src/language-graphql
Ideal API (I think)
- Should take in a "max-width" as configuration (default to 80 chars?)
- Should preserve comments from the original source
- Should be extensible in the future to support prettier-like configs
Why not re-use print?
- Ideally,
printis as high-performance as possible. I imagine calculating line-width will be a non-trivial performance regression, as would preserving comments. printcurrently does not preserve comments, and it would potentially break tooling that depends on this comment-stripping feature.- This would likely be experimental. We should make sure the output is "ideal" before making the default print functionality be "pretty".
What are potential use cases?
- Codemod tooling that wants to do a one-time insertion of an AST node into existing GraphQL source files. The current
printre-formats and strips comments, and leads to super-long lines if, for instance, your query has more than 4 variable definitions. - Test output snapshots would be easier to read, and easier to see how they change over time
- Prettier and prettier-like tooling could consume this new print function directly
Was just about to create an issue for this. 😄 👍
In case it's been forgotten since it's a couple years later, this feature would be super helpful. Thanks for all you do!
This might depend on https://github.com/graphql/graphql-js/issues/2241 ?