-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trying to find an example of how to modify the AST #856
Comments
Hi! Could you say a bit more about what you’re trying to do? Replacing all string values is maybe not the easiest example because apollo-rs currently does not provide a visitor, and string values are potentially found in a number of different places. (And arguably, do you really want to indiscriminately transform field arguments and directive arguments and variable defaults etc in the same way?). As for the more general question: First of all, consider using apollo-compiler version 1.0 even though it is still in beta at this time. Versions 0.x are not as convenient and won’t get further development. Next, pick a level of abstraction:
Either way, many things are wrapped in either https://github.com/apollographql/apollo-rs/blob/apollo-compiler%401.0.0-beta.15/crates/apollo-compiler/examples/rename.rs provides an example (using use apollo_compiler::ast::Definition;
use apollo_compiler::ast::Selection;
use apollo_compiler::ast::Value;
let query = r#"query { field(arg: "string") }"#;
let mut doc = apollo_compiler::ast::Document::parse(query, "example.graphql").unwrap();
for def in &mut doc.definitions {
match def {
Definition::OperationDefinition(operation) => {
let operation = operation.make_mut();
modify_selection_set(&mut operation.selection_set)
}
Definition::FragmentDefinition(_fragment) => todo!(),
// Other definition kinds are for schemas
_ => {}
}
}
println!("{doc}")
fn modify_selection_set(selection_set: &mut [Selection]) {
for selection in selection_set {
match selection {
Selection::Field(field) => {
let field = field.make_mut();
for arg in &mut field.arguments {
if let Value::String(string) = arg.make_mut().value.make_mut() {
*string = format!("{string}‽").into()
}
}
modify_selection_set(&mut field.selection_set);
}
Selection::FragmentSpread(_spread) => todo!(),
Selection::InlineFragment(_inline) => todo!(),
}
}
} |
Thank you, this was what I needed. |
First of all I apologize if this issue was opened in the wrong section.
I am trying out the parser in a small scale personal project in which I'd like to make some changes to the initial query. For the sake of keeping things simple let's assume I am trying to replace all occurrences of string values "John" to "John Smith" in the user provided query for all query arguments.
While looking through the opened issues I found an inspiration for a Visitor module which I could use to parse the AST nicely. Kudos to the owner of that issue for putting it up.
However, mutating the AST with or without such a module is still something I'm confused about. Would it possible to provide an example of how to alter the AST using the simple scenario in the first paragraph?
The text was updated successfully, but these errors were encountered: