Skip to content

Commit

Permalink
Compact GraphQL query text output
Browse files Browse the repository at this point in the history
Wires the pre-existing `compact` printing into a feature flag.

Additionally disables indenting when printing in compact form
  • Loading branch information
tomgasson committed Jun 23, 2022
1 parent 534b66d commit 79bd3d5
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 35 deletions.
22 changes: 14 additions & 8 deletions compiler/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions compiler/crates/common/src/feature_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ pub struct FeatureFlags {
/// Enable support for the experimental `@alias` directive on fragment spreads.
#[serde(default)]
pub enable_fragment_aliases: FeatureFlag,

/// Print queries in compact form
#[serde(default)]
pub compact_query_text: FeatureFlag,
}

#[derive(Debug, Deserialize, Clone, Serialize)]
Expand Down
18 changes: 12 additions & 6 deletions compiler/crates/graphql-text-printer/src/print_full_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,42 @@
* LICENSE file in the root directory of this source tree.
*/

use crate::{print_fragment, print_operation};
use crate::{print_fragment, print_operation, PrinterOptions};
use fnv::FnvHashMap;
use graphql_ir::{
FragmentDefinition, FragmentSpread, OperationDefinition, Program, ScalarField, Visitor,
};
use intern::string_key::StringKey;
use std::sync::Arc;

pub fn print_full_operation(program: &Program, operation: &OperationDefinition) -> String {
let mut printer = OperationPrinter::new(program);
pub fn print_full_operation(
program: &Program,
operation: &OperationDefinition,
options: PrinterOptions,
) -> String {
let mut printer = OperationPrinter::new(program, options);
printer.print(operation)
}

pub struct OperationPrinter<'s> {
fragment_result: FnvHashMap<StringKey, String>,
reachable_fragments: FnvHashMap<StringKey, Arc<FragmentDefinition>>,
program: &'s Program,
options: PrinterOptions,
}

impl<'s> OperationPrinter<'s> {
pub fn new(program: &'s Program) -> Self {
pub fn new(program: &'s Program, options: PrinterOptions) -> Self {
Self {
fragment_result: Default::default(),
reachable_fragments: Default::default(),
program,
options,
}
}

pub fn print(&mut self, operation: &OperationDefinition) -> String {
let mut result = print_operation(&self.program.schema, operation, Default::default());
let mut result = print_operation(&self.program.schema, operation, self.options);
self.visit_operation(operation);
let mut fragments: Vec<(StringKey, Arc<FragmentDefinition>)> =
self.reachable_fragments.drain().collect();
Expand All @@ -51,7 +57,7 @@ impl<'s> OperationPrinter<'s> {
let schema = &self.program.schema;
self.fragment_result
.entry(fragment.name.item)
.or_insert_with(|| print_fragment(schema, fragment, Default::default()))
.or_insert_with(|| print_fragment(schema, fragment, self.options))
}
}

Expand Down
53 changes: 42 additions & 11 deletions compiler/crates/graphql-text-printer/src/print_to_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ impl<'schema, 'writer, W: Write> Printer<'schema, 'writer, W> {
fn print_selections(&mut self, selections: &[Selection]) -> FmtResult {
let len = selections.len();
if len > 0 {
write!(self.writer, " {{")?;
if !self.options.compact {
write!(self.writer, " ")?;
}
write!(self.writer, "{{")?;
self.indentation += 1;
self.next_line()?;

Expand Down Expand Up @@ -372,7 +375,10 @@ impl<'schema, 'writer, W: Write> Printer<'schema, 'writer, W> {
}

fn print_directive(&mut self, directive: &Directive) -> FmtResult {
write!(self.writer, " @{}", directive.name.item)?;
if !self.options.compact {
write!(self.writer, " ")?;
}
write!(self.writer, "@{}", directive.name.item)?;
self.print_arguments(&directive.arguments)?;

if self.options.debug_directive_data {
Expand All @@ -390,9 +396,12 @@ impl<'schema, 'writer, W: Write> Printer<'schema, 'writer, W> {

fn print_condition_directives(&mut self, conditions: Vec<&Condition>) -> FmtResult {
for condition in conditions {
if !self.options.compact {
write!(self.writer, " ")?;
}
write!(
self.writer,
" @{}",
"@{}",
if condition.passing_value {
"include"
} else {
Expand All @@ -401,10 +410,18 @@ impl<'schema, 'writer, W: Write> Printer<'schema, 'writer, W> {
)?;
match &condition.value {
ConditionValue::Constant(value) => {
write!(self.writer, "(if: {})", value)?;
write!(self.writer, "(if:")?;
if !self.options.compact {
write!(self.writer, " ")?;
}
write!(self.writer, "{})", value)?;
}
ConditionValue::Variable(variable) => {
write!(self.writer, "(if: ${})", variable.name.item)?;
write!(self.writer, "(if:")?;
if !self.options.compact {
write!(self.writer, " ")?;
}
write!(self.writer, "${})", variable.name.item)?;
}
}
}
Expand All @@ -421,12 +438,21 @@ impl<'schema, 'writer, W: Write> Printer<'schema, 'writer, W> {
for var_def in variable_definitions.iter() {
self.next_line()?;
let type_name = self.schema.get_type_string(&var_def.type_);
write!(self.writer, "${}: {}", var_def.name.item, type_name)?;

write!(self.writer, "${}:", var_def.name.item)?;
if !self.options.compact {
write!(self.writer, " ")?;
}
write!(self.writer, "{}", type_name)?;
match &var_def.default_value {
None => {}
Some(default_value) => {
write!(self.writer, " = ")?;
if !self.options.compact {
write!(self.writer, " ")?;
}
write!(self.writer, "=")?;
if !self.options.compact {
write!(self.writer, " ")?;
}
self.print_constant_value(&default_value.item)?;
}
}
Expand Down Expand Up @@ -638,16 +664,21 @@ impl<'schema, 'writer, W: Write> Printer<'schema, 'writer, W> {
) -> FmtResult {
if let Some(alias) = alias {
if alias.item != name {
write!(self.writer, "{}: ", alias.item)?;
write!(self.writer, "{}:", alias.item)?;
if !self.options.compact {
write!(self.writer, " ")?;
}
}
}
write!(self.writer, "{}", name)
}

fn next_line(&mut self) -> FmtResult {
writeln!(self.writer)?;
for _ in 0..self.indentation {
write!(self.writer, " ")?;
if !self.options.compact {
for _ in 0..self.indentation {
write!(self.writer, " ")?;
}
}
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
.into_iter()
.filter_map(|definition| {
if let ExecutableDefinition::Operation(operation) = definition {
Some(print_full_operation(&program, &operation))
Some(print_full_operation(
&program,
&operation,
Default::default(),
))
} else {
None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::config::{Config, ProjectConfig};
use common::{NamedItem, SourceLocationKey};
use fnv::FnvHashMap;
use graphql_ir::{FragmentDefinition, OperationDefinition};
use graphql_text_printer::OperationPrinter;
use graphql_text_printer::{OperationPrinter, PrinterOptions};
use intern::string_key::StringKey;
use relay_transforms::{
ClientEdgeGeneratedQueryMetadataDirective, Programs, RefetchableDerivedFromMetadata,
Expand All @@ -37,7 +37,14 @@ pub fn generate_artifacts(
programs: &Programs,
source_hashes: Arc<SourceHashes>,
) -> Vec<Artifact> {
let mut operation_printer = OperationPrinter::new(&programs.operation_text);
let printer_options = PrinterOptions {
compact: project_config
.feature_flags
.compact_query_text
.is_fully_enabled(),
..Default::default()
};
let mut operation_printer = OperationPrinter::new(&programs.operation_text, printer_options);
return group_operations(programs)
.into_iter()
.map(|(_, operations)| -> Artifact {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
enable_client_edges: FeatureFlag::Enabled,
skip_printing_nulls: FeatureFlag::Disabled,
enable_fragment_aliases: FeatureFlag::Enabled,
compact_query_text: FeatureFlag::Disabled,
};

let default_project_config = ProjectConfig {
Expand Down Expand Up @@ -191,7 +192,11 @@ pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
let text = print_operation_node.map_or_else(
|| "Query Text is Empty.".to_string(),
|print_operation_node| {
print_full_operation(&programs.operation_text, print_operation_node)
print_full_operation(
&programs.operation_text,
print_operation_node,
Default::default(),
)
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
enable_client_edges: FeatureFlag::Enabled,
skip_printing_nulls: FeatureFlag::Disabled,
enable_fragment_aliases: FeatureFlag::Enabled,
compact_query_text: FeatureFlag::Disabled,
};

let project_config = ProjectConfig {
Expand Down Expand Up @@ -122,7 +123,11 @@ pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
let text = print_operation_node.map_or_else(
|| "Query Text is Empty.".to_string(),
|print_operation_node| {
print_full_operation(&programs.operation_text, print_operation_node)
print_full_operation(
&programs.operation_text,
print_operation_node,
Default::default(),
)
},
);

Expand Down
1 change: 1 addition & 0 deletions compiler/crates/relay-lsp/src/graphql_tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ fn print_full_operation_text(programs: Programs, operation_name: StringKey) -> O
Some(print_full_operation(
&programs.operation_text,
print_operation_node,
Default::default(),
))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,10 @@ impl<'program> FragmentAliasTransform<'program> {
FragmentAliasMetadata {
alias,
type_condition,
selection_type:
type_condition.unwrap_or(
self.parent_type
.expect("Selection should be within a parent type."),
),
selection_type: type_condition.unwrap_or(
self.parent_type
.expect("Selection should be within a parent type."),
),
}
.into(),
)
Expand Down

0 comments on commit 79bd3d5

Please sign in to comment.