graphql-js icon indicating copy to clipboard operation
graphql-js copied to clipboard

Feature request: prettyPrint

Open mjmahone opened this issue 6 years ago • 3 comments

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, print is as high-performance as possible. I imagine calculating line-width will be a non-trivial performance regression, as would preserving comments.
  • print currently 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 print re-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

mjmahone avatar Mar 22 '19 20:03 mjmahone

Was just about to create an issue for this. 😄 👍

imolorhe avatar Mar 23 '19 13:03 imolorhe

In case it's been forgotten since it's a couple years later, this feature would be super helpful. Thanks for all you do!

lesleydreyer avatar Feb 04 '25 01:02 lesleydreyer

This might depend on https://github.com/graphql/graphql-js/issues/2241 ?

yaacovCR avatar Feb 06 '25 12:02 yaacovCR