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 25, 2022
1 parent 3968341 commit 9916c24
Show file tree
Hide file tree
Showing 28 changed files with 816 additions and 154 deletions.
5 changes: 3 additions & 2 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
156 changes: 154 additions & 2 deletions compiler/crates/graphql-ir/src/node_identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

use crate::*;
use common::WithLocation;
use graphql_syntax::OperationKind;
use intern::string_key::StringKey;
use schema::SDLSchema;
use schema::{FieldID, SDLSchema, Type};
use std::{
fmt,
hash::{Hash, Hasher},
Expand Down Expand Up @@ -275,7 +276,7 @@ impl<T: LocationAgnosticPartialEq> LocationAgnosticPartialEq for WithLocation<T>
}
}

impl<T: LocationAgnosticPartialEq> LocationAgnosticPartialEq for Option<&T> {
impl<T: LocationAgnosticPartialEq> LocationAgnosticPartialEq for Option<T> {
fn location_agnostic_eq<B: LocationAgnosticBehavior>(&self, other: &Self) -> bool {
match (self, other) {
(Some(l), Some(r)) => l.location_agnostic_eq::<B>(r),
Expand All @@ -302,6 +303,9 @@ pub trait DirectlyComparableIR {}
impl DirectlyComparableIR for Value {}
impl DirectlyComparableIR for ConstantArgument {}
impl DirectlyComparableIR for ConstantValue {}
impl DirectlyComparableIR for Selection {}
impl DirectlyComparableIR for ExecutableDefinition {}
impl DirectlyComparableIR for VariableDefinition {}

impl<T: LocationAgnosticPartialEq + DirectlyComparableIR> LocationAgnosticPartialEq for Vec<T> {
fn location_agnostic_eq<B: LocationAgnosticBehavior>(&self, other: &Self) -> bool {
Expand Down Expand Up @@ -405,6 +409,16 @@ impl LocationAgnosticPartialEq for Argument {
}
}

impl LocationAgnosticPartialEq for Option<&Argument> {
fn location_agnostic_eq<B: LocationAgnosticBehavior>(&self, other: &Self) -> bool {
match (self, other) {
(Some(l), Some(r)) => l.location_agnostic_eq::<B>(r),
(None, None) => true,
_ => false,
}
}
}

impl LocationAgnosticHash for Value {
fn location_agnostic_hash<H: Hasher, B: LocationAgnosticBehavior>(&self, state: &mut H) {
match self {
Expand Down Expand Up @@ -517,3 +531,141 @@ impl LocationAgnosticPartialEq for ConditionValue {
}
}
}

impl LocationAgnosticPartialEq for ExecutableDefinition {
fn location_agnostic_eq<B: LocationAgnosticBehavior>(&self, other: &Self) -> bool {
match (self, other) {
(ExecutableDefinition::Operation(left), ExecutableDefinition::Operation(right)) => {
left.location_agnostic_eq::<B>(right)
}
(ExecutableDefinition::Fragment(left), ExecutableDefinition::Fragment(right)) => {
left.location_agnostic_eq::<B>(right)
}
_ => false,
}
}
}

impl LocationAgnosticPartialEq for VariableDefinition {
fn location_agnostic_eq<B: LocationAgnosticBehavior>(&self, other: &Self) -> bool {
self.name.location_agnostic_eq::<B>(&other.name)
&& self.type_ == other.type_
&& self
.default_value
.location_agnostic_eq::<B>(&other.default_value)
&& self.directives.location_agnostic_eq::<B>(&other.directives)
}
}

impl LocationAgnosticPartialEq for OperationDefinition {
fn location_agnostic_eq<B: LocationAgnosticBehavior>(&self, other: &Self) -> bool {
self.kind.location_agnostic_eq::<B>(&other.kind)
&& self.name.location_agnostic_eq::<B>(&other.name)
&& self.type_.location_agnostic_eq::<B>(&other.type_)
&& self.kind.location_agnostic_eq::<B>(&other.kind)
&& self
.variable_definitions
.location_agnostic_eq::<B>(&other.variable_definitions)
&& self.directives.location_agnostic_eq::<B>(&other.directives)
&& self.selections.location_agnostic_eq::<B>(&other.selections)
}
}

impl LocationAgnosticPartialEq for OperationKind {
fn location_agnostic_eq<B: LocationAgnosticBehavior>(&self, other: &Self) -> bool {
self == other
}
}
impl LocationAgnosticPartialEq for Type {
fn location_agnostic_eq<B: LocationAgnosticBehavior>(&self, other: &Self) -> bool {
self == other
}
}

impl LocationAgnosticPartialEq for Selection {
fn location_agnostic_eq<B: LocationAgnosticBehavior>(&self, other: &Self) -> bool {
match (self, other) {
(Selection::FragmentSpread(l), Selection::FragmentSpread(r)) => {
l.location_agnostic_eq::<B>(r)
}
(Selection::InlineFragment(l), Selection::InlineFragment(r)) => {
l.location_agnostic_eq::<B>(r)
}
(Selection::LinkedField(l), Selection::LinkedField(r)) => {
l.location_agnostic_eq::<B>(r)
}
(Selection::ScalarField(l), Selection::ScalarField(r)) => {
l.location_agnostic_eq::<B>(r)
}
(Selection::Condition(l), Selection::Condition(r)) => l.location_agnostic_eq::<B>(r),
_ => false,
}
}
}

impl LocationAgnosticPartialEq for FragmentSpread {
fn location_agnostic_eq<B: LocationAgnosticBehavior>(&self, other: &Self) -> bool {
self.fragment.location_agnostic_eq::<B>(&other.fragment)
&& self.arguments.location_agnostic_eq::<B>(&other.arguments)
&& self.directives.location_agnostic_eq::<B>(&other.directives)
}
}

impl LocationAgnosticPartialEq for InlineFragment {
fn location_agnostic_eq<B: LocationAgnosticBehavior>(&self, other: &Self) -> bool {
self.type_condition
.location_agnostic_eq::<B>(&other.type_condition)
&& self.directives.location_agnostic_eq::<B>(&other.directives)
&& self.selections.location_agnostic_eq::<B>(&other.selections)
}
}

impl LocationAgnosticPartialEq for LinkedField {
fn location_agnostic_eq<B: LocationAgnosticBehavior>(&self, other: &Self) -> bool {
self.alias.location_agnostic_eq::<B>(&other.alias)
&& self.definition.location_agnostic_eq::<B>(&other.definition)
&& self.arguments.location_agnostic_eq::<B>(&other.arguments)
&& self.directives.location_agnostic_eq::<B>(&other.directives)
&& self.selections.location_agnostic_eq::<B>(&other.selections)
}
}

impl LocationAgnosticPartialEq for ScalarField {
fn location_agnostic_eq<B: LocationAgnosticBehavior>(&self, other: &Self) -> bool {
self.alias.location_agnostic_eq::<B>(&other.alias)
&& self.definition.location_agnostic_eq::<B>(&other.definition)
&& self.arguments.location_agnostic_eq::<B>(&other.arguments)
&& self.directives.location_agnostic_eq::<B>(&other.directives)
}
}

impl LocationAgnosticPartialEq for Condition {
fn location_agnostic_eq<B: LocationAgnosticBehavior>(&self, other: &Self) -> bool {
self.selections.location_agnostic_eq::<B>(&other.selections)
&& self.value.location_agnostic_eq::<B>(&other.value)
&& self.passing_value == other.passing_value
}
}

impl LocationAgnosticPartialEq for FragmentDefinition {
fn location_agnostic_eq<B: LocationAgnosticBehavior>(&self, other: &Self) -> bool {
self.name.location_agnostic_eq::<B>(&other.name)
&& self
.variable_definitions
.location_agnostic_eq::<B>(&other.variable_definitions)
&& self
.used_global_variables
.location_agnostic_eq::<B>(&other.used_global_variables)
&& self
.type_condition
.location_agnostic_eq::<B>(&other.type_condition)
&& self.directives.location_agnostic_eq::<B>(&other.directives)
&& self.selections.location_agnostic_eq::<B>(&other.selections)
}
}

impl LocationAgnosticPartialEq for FieldID {
fn location_agnostic_eq<B: LocationAgnosticBehavior>(&self, other: &Self) -> bool {
self == other
}
}
1 change: 1 addition & 0 deletions compiler/crates/graphql-text-printer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ schema = { path = "../schema" }
[dev-dependencies]
fixture-tests = { path = "../fixture-tests" }
relay-test-schema = { path = "../relay-test-schema" }
relay-transforms = { path = "../relay-transforms" }
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
Loading

0 comments on commit 9916c24

Please sign in to comment.