1.0.1 • Published 2 years ago

graphql-print v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

graphql-print

The printer that GraphQL always deserved.

✅ Pretty and minified printing

✅ Print comments

✅ Configure line length

✅ Choose custom indentation

Install

Note that graphql is a peer dependency of this package and needs to be installed as well.

npm i graphql graphql-print

Usage

This package supports printing any valid GraphQL AST node, as well as printing a list of nodes.

By default, the printer outputs a pretty-printed version of the given AST nodes, but it can be configured in different ways. The following table shows the supported options.

OptionTypeDefaultDescription
minifiedbooleanfalseReturns a minified version of the AST, optimizing for shortest string length
preserveCommentsbooleanfalseAll comments are stripped from the AST by default but can be perserved using this option
maxLineLengthnumber80The printer tries to output lists of nodes in a single line where possible and only breaks lists items into individual lines when they become too long. This option controls the threshold for when a line should be broken up into multiple ones.
indentationStepstring" "By default the printer uses two spaces for indentation, this option allows you to configure that. (Note that passing any characters other than ignored tokens will result in a string that does not represent the original AST anymore and might potentially be invalid.)

Example

import { parse } from "graphql";
import { print } from "graphql-print";

const ast = parse(`
  query MyQuery { 
    # select a field
    myField(myArg: 42, myOtherArg: null)
  }
`);

console.log(print(ast));
/**
 * Will print the following:
 *
 * query MyQuery {
 *   myField(myArg: 42, myOtherArg: null)
 * }
 */

console.log(print(ast, { minified: true }));
/**
 * Will print the following:
 *
 * query MyQuery{myField(myArg:42,myOtherArg:null)}
 */

console.log(print(ast, { preserveComments: true }));
/**
 * Will print the following:
 *
 * query MyQuery {
 *   # select a field
 *   myField(myArg: 42, myOtherArg: null)
 * }
 */

console.log(print(ast, { maxLineLength: 20 }));
/**
 * Will print the following:
 *
 * query MyQuery {
 *   myField(
 *     myArg: 42
 *     myOtherArg: null
 *   )
 * }
 */
1.0.1

2 years ago

1.0.0

2 years ago

0.0.0

2 years ago