Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

graphql-print

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-print

Print any GraphQL AST node, pretty or minified, with or without comments

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
28
decreased by-83.03%
Maintainers
1
Weekly downloads
 
Created
Source

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
 *   )
 * }
 */

Keywords

FAQs

Package last updated on 18 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc