Created
February 18, 2022 18:12
-
-
Save lrlna/2ce2cbac2452c0ce6a2ee5bf44aa6d18 to your computer and use it in GitHub Desktop.
apollo-rs/issues/171
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
use apollo_encoder::*; | |
use apollo_parser::{ast, Parser}; | |
fn main() { | |
let src = r#" | |
{ | |
myQuery { | |
id | |
foo { | |
alpha | |
beta | |
} | |
}"#; | |
let parser = Parser::new(src); | |
let ast = parser.parse(); | |
dbg!(&ast); | |
let mut id_query = Document::new(); | |
let mut foo_query = Document::new(); | |
for definition in ast.document().definitions() { | |
if let ast::Definition::OperationDefinition(def) = definition { | |
for selection in def | |
.selection_set() | |
.expect("expected selection set") | |
.selections() | |
{ | |
if let ast::Selection::Field(top_level_field) = selection { | |
for selection in top_level_field | |
.selection_set() | |
.expect("expected selection set") | |
.selections() | |
{ | |
if let ast::Selection::Field(field) = selection { | |
match field.selection_set() { | |
Some(selections) => { | |
let mut query_field = Field::new( | |
top_level_field | |
.name() | |
.expect("expected field name") | |
.to_string(), | |
); | |
let mut foo_field = Field::new( | |
field.name().expect("expected field name").to_string(), | |
); | |
let foo_selections: Vec<Selection> = selections | |
.selections() | |
.filter_map(|sel| { | |
if let ast::Selection::Field(field) = sel { | |
let selection_field = Field::new( | |
field | |
.name() | |
.expect("expected a field name") | |
.to_string(), | |
); | |
Some(Selection::Field(selection_field)) | |
} else { | |
None | |
} | |
}) | |
.collect(); | |
let foo_sel_set = SelectionSet::with_selections(foo_selections); | |
foo_field.selection_set(Some(foo_sel_set)); | |
let foo_query_sel_set = | |
SelectionSet::with_selections(vec![Selection::Field( | |
foo_field, | |
)]); | |
query_field.selection_set(Some(foo_query_sel_set)); | |
let id_sel_set = | |
SelectionSet::with_selections(vec![Selection::Field( | |
query_field, | |
)]); | |
let op_def = | |
OperationDefinition::new(OperationType::Query, id_sel_set); | |
foo_query.operation(op_def) | |
} | |
None => { | |
let mut query_field = Field::new( | |
top_level_field | |
.name() | |
.expect("expected field name") | |
.to_string(), | |
); | |
let id_field = Field::new( | |
field.name().expect("expected field name").to_string(), | |
); | |
dbg!(&id_field); | |
query_field.selection_set(Some(SelectionSet::with_selections( | |
vec![Selection::Field(id_field)], | |
))); | |
let id_sel_set = | |
SelectionSet::with_selections(vec![Selection::Field( | |
query_field, | |
)]); | |
let op_def = | |
OperationDefinition::new(OperationType::Query, id_sel_set); | |
id_query.operation(op_def) | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
println!("{:?}", foo_query.to_string()); | |
println!("{:?}", id_query.to_string()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment