2 unstable releases

0.2.0 Aug 24, 2024
0.1.0 Aug 22, 2024

#244 in HTTP server

Download history 189/week @ 2024-08-18 41/week @ 2024-08-25 1/week @ 2024-09-01

231 downloads per month

MIT license

105KB
3K SLoC

pretty_graphql is a configurable GraphQL formatter.

Basic Usage

You can format source code string by using format_text function.

use pretty_graphql::{config::FormatOptions, format_text};

let options = FormatOptions::default();
assert_eq!("{\n  field\n}\n", &format_text("{ field }", &options).unwrap());

For detailed documentation of configuration, please read configuration documentation.

If there're syntax errors in source code, it will return Err:

use pretty_graphql::{config::FormatOptions, format_text};

let options = FormatOptions::default();
assert!(format_text("{", &options).is_err());

Print Syntax Tree

If you have already parsed the syntax tree from apollo-parser, you can use print_tree to print it.

use pretty_graphql::{config::FormatOptions, print_tree};
use apollo_parser::{cst::Document, Parser};

let input = "{ field }";
let parser = Parser::new(input);
let cst = parser.parse();

let options = FormatOptions::default();
assert_eq!("{\n  field\n}\n", &print_tree(&cst.document(), &options));

Dependencies

~1.4–2.2MB
~44K SLoC