-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
runtime: refactor two printer logics into one
- Loading branch information
Showing
4 changed files
with
71 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::{ | ||
json_printer::{print_to_json_string, ExecutableDefinitionRef}, | ||
utils::fragment_names_in_selection_set, | ||
}; | ||
use nitrogql_ast::operation::{FragmentDefinition, OperationDefinition}; | ||
use sourcemap_writer::SourceMapWriter; | ||
|
||
/// Print the runtime value of given operation. | ||
pub fn print_operation_runtime( | ||
writer: &mut impl SourceMapWriter, | ||
operation: &OperationDefinition, | ||
fragments: &HashMap<&str, &FragmentDefinition>, | ||
) { | ||
let fragments_to_include = fragment_names_in_selection_set(&operation.selection_set, |name| { | ||
fragments.get(name).copied() | ||
}) | ||
.into_iter() | ||
.map(|name| { | ||
ExecutableDefinitionRef::FragmentDefinition( | ||
fragments.get(name).expect("fragment not found"), | ||
) | ||
}); | ||
let this_document = vec![ExecutableDefinitionRef::OperationDefinition(operation)] | ||
.into_iter() | ||
.chain(fragments_to_include) | ||
.collect::<Vec<_>>(); | ||
writer.write(&print_to_json_string(&this_document[..])); | ||
} | ||
|
||
/// Print the runtime value of given fragment. | ||
pub fn print_fragment_runtime( | ||
writer: &mut impl SourceMapWriter, | ||
fragment: &FragmentDefinition, | ||
fragments: &HashMap<&str, &FragmentDefinition>, | ||
) { | ||
let fragments_to_include = fragment_names_in_selection_set(&fragment.selection_set, |name| { | ||
fragments.get(name).copied() | ||
}) | ||
.into_iter() | ||
.filter(|f| { | ||
// Filter out the fragment we are currently processing | ||
*f != fragment.name.name | ||
}) | ||
.map(|name| { | ||
ExecutableDefinitionRef::FragmentDefinition( | ||
fragments.get(name).expect("fragment not found"), | ||
) | ||
}); | ||
// Generated document is the collection of all relevant fragments, | ||
// and the fragment we are currently processing | ||
// comes first in the list | ||
let this_document = vec![ExecutableDefinitionRef::FragmentDefinition(fragment)] | ||
.into_iter() | ||
.chain(fragments_to_include) | ||
.collect::<Vec<_>>(); | ||
writer.write(&print_to_json_string(&this_document[..])); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters