Skip to content

Commit

Permalink
Minor refactor for exported OperationPrinter
Browse files Browse the repository at this point in the history
Reviewed By: kassens

Differential Revision: D20463667

fbshipit-source-id: 7b0f8f1edfd427fe74efbe98c072e98402693888
  • Loading branch information
Juan Tejada authored and facebook-github-bot committed Mar 16, 2020
1 parent 027fbd1 commit 50dcfba
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions compiler/crates/graphql-text-printer/src/lib.rs
Expand Up @@ -9,10 +9,10 @@
#![deny(rust_2018_idioms)]
#![deny(clippy::all)]

mod operation_printer;
mod print_full_operation;
mod print_to_text;

pub use operation_printer::OperationPrinter;
pub use print_full_operation::print_full_operation;
pub use print_to_text::{
print_arguments, print_definition, print_directives, print_fragment, print_ir, print_operation,
write_arguments, write_directives,
Expand Down
Expand Up @@ -13,7 +13,12 @@ use graphql_ir::{
use interner::StringKey;
use std::sync::Arc;

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

struct OperationPrinter<'s> {
fragment_result: FnvHashMap<StringKey, String>,
reachable_fragments: FnvHashMap<StringKey, Arc<FragmentDefinition>>,
program: &'s Program<'s>,
Expand Down
Expand Up @@ -9,23 +9,22 @@ use common::FileKey;
use fixture_tests::Fixture;
use graphql_ir::{build, ExecutableDefinition, Program};
use graphql_syntax::parse;
use graphql_text_printer::OperationPrinter;
use graphql_text_printer::print_full_operation;
use test_schema::TEST_SCHEMA;

pub fn transform_fixture(fixture: &Fixture) -> Result<String, String> {
let file_key = FileKey::new(fixture.file_name);
let ast = parse(fixture.content, file_key).unwrap();
let ir = build(&TEST_SCHEMA, &ast.definitions).unwrap();
let program = Program::from_definitions(&TEST_SCHEMA, ir);
let mut printer = OperationPrinter::new(&program);

build(&TEST_SCHEMA, &ast.definitions)
.map(|definitions| {
definitions
.into_iter()
.filter_map(|definition| {
if let ExecutableDefinition::Operation(definitions) = definition {
Some(printer.print(&definitions))
if let ExecutableDefinition::Operation(operation) = definition {
Some(print_full_operation(&program, &operation))
} else {
None
}
Expand Down
Expand Up @@ -9,7 +9,7 @@ use super::apply_transforms::Programs;
use crate::config::ConfigProject;
use crate::errors::BuildProjectError;
use graphql_ir::FragmentDefinition;
use graphql_text_printer::OperationPrinter;
use graphql_text_printer::print_full_operation;
use interner::StringKey;
use persist_query::persist;
use signedsource::{sign_file, SIGNING_TOKEN};
Expand All @@ -24,16 +24,14 @@ pub async fn generate_artifacts(
project_config: &ConfigProject,
programs: &Programs<'_>,
) -> Result<Vec<Artifact>, BuildProjectError> {
let mut printer = OperationPrinter::new(&programs.operation_text);

let mut artifacts = Vec::new();
for node in programs.normalization.operations() {
let name = node.name.item;
let print_operation_node = programs
.operation_text
.operation(name)
.expect("a query text operation should be generated for this operation");
let text = printer.print(print_operation_node);
let text = print_full_operation(&programs.operation_text, print_operation_node);
let id = if let Some(ref persist_config) = project_config.persist {
persist(&text, &persist_config.url, &persist_config.params)
.await
Expand Down

0 comments on commit 50dcfba

Please sign in to comment.