Skip to content

Feature request: prettyPrint #1799

@mjmahone

Description

@mjmahone

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions