-
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.
extract functions related to the normalization ast text
- Loading branch information
Showing
3 changed files
with
101 additions
and
93 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
mod artifact_file_contents; | ||
mod generate_artifacts; | ||
mod iso_overload_file; | ||
mod normalization_ast_text; | ||
mod query_text; | ||
|
||
pub use generate_artifacts::get_artifact_path_and_content; |
95 changes: 95 additions & 0 deletions
95
crates/graphql_artifact_generation/src/normalization_ast_text.rs
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,95 @@ | ||
use common_lang_types::WithSpan; | ||
use isograph_schema::{ | ||
MergedInlineFragmentSelection, MergedLinkedFieldSelection, MergedScalarFieldSelection, | ||
MergedServerFieldSelection, ValidatedSchema, | ||
}; | ||
|
||
use crate::generate_artifacts::{get_serialized_field_arguments, NormalizationAstText}; | ||
|
||
pub(crate) fn generate_normalization_ast_text<'schema>( | ||
schema: &'schema ValidatedSchema, | ||
selection_set: &[WithSpan<MergedServerFieldSelection>], | ||
indentation_level: u8, | ||
) -> NormalizationAstText { | ||
let mut normalization_ast_text = "[\n".to_string(); | ||
for item in selection_set.iter() { | ||
let s = generate_normalization_ast_node(item, schema, indentation_level + 1); | ||
normalization_ast_text.push_str(&s); | ||
} | ||
normalization_ast_text.push_str(&format!("{}]", " ".repeat(indentation_level as usize))); | ||
NormalizationAstText(normalization_ast_text) | ||
} | ||
|
||
fn generate_normalization_ast_node( | ||
item: &WithSpan<MergedServerFieldSelection>, | ||
schema: &ValidatedSchema, | ||
indentation_level: u8, | ||
) -> String { | ||
match &item.item { | ||
MergedServerFieldSelection::ScalarField(scalar_field) => { | ||
let MergedScalarFieldSelection { | ||
name, arguments, .. | ||
} = scalar_field; | ||
let indent = " ".repeat(indentation_level as usize); | ||
let indent_2 = " ".repeat((indentation_level + 1) as usize); | ||
let serialized_arguments = | ||
get_serialized_field_arguments(arguments, indentation_level + 1); | ||
// TODO this is bad, name is a WithLocation and impl's Display, we should fix | ||
let name = name.item; | ||
|
||
format!( | ||
"{indent}{{\n\ | ||
{indent_2}kind: \"Scalar\",\n\ | ||
{indent_2}fieldName: \"{name}\",\n\ | ||
{indent_2}arguments: {serialized_arguments},\n\ | ||
{indent}}},\n" | ||
) | ||
} | ||
MergedServerFieldSelection::LinkedField(linked_field) => { | ||
let MergedLinkedFieldSelection { | ||
name, | ||
selection_set, | ||
arguments, | ||
.. | ||
} = linked_field; | ||
let indent = " ".repeat(indentation_level as usize); | ||
let indent_2 = " ".repeat((indentation_level + 1) as usize); | ||
let serialized_arguments = | ||
get_serialized_field_arguments(arguments, indentation_level + 1); | ||
|
||
let selections = | ||
generate_normalization_ast_text(schema, selection_set, indentation_level + 1); | ||
|
||
// TODO this is bad, name is a WithLocation which impl's Display | ||
let name = name.item; | ||
|
||
format!( | ||
"{indent}{{\n\ | ||
{indent_2}kind: \"Linked\",\n\ | ||
{indent_2}fieldName: \"{name}\",\n\ | ||
{indent_2}arguments: {serialized_arguments},\n\ | ||
{indent_2}selections: {selections},\n\ | ||
{indent}}},\n" | ||
) | ||
} | ||
MergedServerFieldSelection::InlineFragment(inline_fragment) => { | ||
let MergedInlineFragmentSelection { | ||
type_to_refine_to, | ||
selection_set, | ||
} = inline_fragment; | ||
let indent = " ".repeat(indentation_level as usize); | ||
let indent_2 = " ".repeat((indentation_level + 1) as usize); | ||
|
||
let selections = | ||
generate_normalization_ast_text(schema, selection_set, indentation_level + 1); | ||
|
||
format!( | ||
"{indent}{{\n\ | ||
{indent_2}kind: \"InlineFragment\",\n\ | ||
{indent_2}type: \"{type_to_refine_to}\",\n\ | ||
{indent_2}selections: {selections},\n\ | ||
{indent}}},\n" | ||
) | ||
} | ||
} | ||
} |