Skip to content
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

Add SchemaVisitor and QueryVisitor traits and functions #26

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod ast;
mod error;
mod format;
mod grammar;
pub mod query_visitor;


pub use self::grammar::parse_query;
Expand Down
227 changes: 227 additions & 0 deletions src/query/query_visitor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
//! Query syntax tree traversal.
//!
//! Each method of [`QueryVisitor`] is a hook that can be overridden to customize the behavior when
//! visiting the corresponding type of node. By default, the methods don't do anything. The actual
//! walking of the ast is done by the `walk_*` functions. So to run a visitor over the whole
//! document you should use [`walk_document`].
//!
//! Example:
//!
//! ```
//! use graphql_parser::query::{
//! Field,
//! parse_query,
//! query_visitor::{QueryVisitor, walk_document},
//! };
//!
//! struct FieldsCounter {
//! count: usize,
//! }
//!
//! impl FieldsCounter {
//! fn new() -> Self {
//! Self { count: 0 }
//! }
//! }
//!
//! impl<'ast> QueryVisitor<'ast> for FieldsCounter {
//! fn visit_field(&mut self, node: &'ast Field) {
//! self.count += 1
//! }
//! }
//!
//! fn main() {
//! let mut number_of_type = FieldsCounter::new();
Copy link

@spawnia spawnia Apr 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//! let mut number_of_type = FieldsCounter::new();
//! let mut fields_counter = FieldsCounter::new();

number_of_type seems to indicate this is supposed to count types, not fields, which does not really make sense when walking over a query.

//!
//! let doc = parse_query(r#"
//! query TestQuery {
//! users {
//! id
//! country {
//! id
//! }
//! }
//! }
//! "#).expect("Failed to parse query");
//!
//! walk_document(&mut number_of_type, &doc);
//!
//! assert_eq!(number_of_type.count, 2);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right. There are four fields if visiting recursively: query, id, country, id and single one if not (users). Currently it looks like you just counting users twice.

//! }
//! ```
//!
//! [`QueryVisitor`]: /graphql_parser/query/query_visitor/trait.QueryVisitor.html
//! [`walk_document`]: /graphql_parser/query/query_visitor/fn.walk_document.html

#![allow(unused_variables)]

use super::ast::*;

/// Trait for easy query syntax tree traversal.
///
/// See [module docs](/graphql_parser/query/query_visitor/index.html) for more info.
pub trait QueryVisitor<'ast> {
fn visit_document(&mut self, node: &'ast Document) {}

fn visit_definition(&mut self, node: &'ast Definition) {}

fn visit_fragment_definition(&mut self, node: &'ast FragmentDefinition) {}

fn visit_operation_definition(&mut self, node: &'ast OperationDefinition) {}

fn visit_query(&mut self, node: &'ast Query) {}

fn visit_mutation(&mut self, node: &'ast Mutation) {}

fn visit_subscription(&mut self, node: &'ast Subscription) {}

fn visit_selection_set(&mut self, node: &'ast SelectionSet) {}

fn visit_variable_definition(&mut self, node: &'ast VariableDefinition) {}

fn visit_selection(&mut self, node: &'ast Selection) {}

fn visit_field(&mut self, node: &'ast Field) {}

fn visit_fragment_spread(&mut self, node: &'ast FragmentSpread) {}

fn visit_inline_fragment(&mut self, node: &'ast InlineFragment) {}
}


/// Walk a query syntax tree and call the visitor methods for each type of node.
///
/// This function is how you should initiate a visitor.
pub fn walk_document<'ast, V: QueryVisitor<'ast>>(visitor: &mut V, node: &'ast Document) {
visitor.visit_document(node);
for def in &node.definitions {
walk_definition(visitor, def);
}
}

fn walk_definition<'ast, V: QueryVisitor<'ast>>(visitor: &mut V, node: &'ast Definition) {
use super::ast::Definition::*;

visitor.visit_definition(node);
match node {
Operation(inner) => {
visitor.visit_operation_definition(inner);
walk_operation_definition(visitor, inner);
},
Fragment(inner) => {
visitor.visit_fragment_definition(inner);
walk_fragment_definition(visitor, inner);
},
}
}

fn walk_fragment_definition<'ast, V: QueryVisitor<'ast>>(visitor: &mut V, node: &'ast FragmentDefinition) {
visitor.visit_fragment_definition(node);
walk_selection_set(visitor, &node.selection_set);
}
Copy link
Member

@tomhoule tomhoule Sep 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to further walk down the fragment's selection?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in db9310e 👍


fn walk_operation_definition<'ast, V: QueryVisitor<'ast>>(visitor: &mut V, node: &'ast OperationDefinition) {
use super::ast::OperationDefinition::*;

visitor.visit_operation_definition(node);
match node {
SelectionSet(inner) => {
visitor.visit_selection_set(inner);
walk_selection_set(visitor, inner);
}
Query(inner) => {
visitor.visit_query(inner);
walk_query(visitor, inner);
}
Mutation(inner) => {
visitor.visit_mutation(inner);
walk_mutation(visitor, inner);
}
Subscription(inner) => {
visitor.visit_subscription(inner);
walk_subscription(visitor, inner);
}
}
}

fn walk_query<'ast, V: QueryVisitor<'ast>>(visitor: &mut V, node: &'ast Query) {
visitor.visit_query(node);

for var_def in &node.variable_definitions {
visitor.visit_variable_definition(var_def);
walk_variable_definition(visitor, var_def);
}

visitor.visit_selection_set(&node.selection_set);
walk_selection_set(visitor, &node.selection_set);
}

fn walk_mutation<'ast, V: QueryVisitor<'ast>>(visitor: &mut V, node: &'ast Mutation) {
visitor.visit_mutation(node);

for var_def in &node.variable_definitions {
visitor.visit_variable_definition(var_def);
walk_variable_definition(visitor, var_def);
}

visitor.visit_selection_set(&node.selection_set);
walk_selection_set(visitor, &node.selection_set);
}

fn walk_subscription<'ast, V: QueryVisitor<'ast>>(visitor: &mut V, node: &'ast Subscription) {
visitor.visit_subscription(node);

for var_def in &node.variable_definitions {
visitor.visit_variable_definition(var_def);
walk_variable_definition(visitor, var_def);
}

visitor.visit_selection_set(&node.selection_set);
walk_selection_set(visitor, &node.selection_set);
}

fn walk_selection_set<'ast, V: QueryVisitor<'ast>>(visitor: &mut V, node: &'ast SelectionSet) {
visitor.visit_selection_set(node);

for selection in &node.items {
visitor.visit_selection(selection);
walk_selection(visitor, selection);
}
}

fn walk_variable_definition<'ast, V: QueryVisitor<'ast>>(visitor: &mut V, node: &'ast VariableDefinition) {
visitor.visit_variable_definition(node)
}

fn walk_selection<'ast, V: QueryVisitor<'ast>>(visitor: &mut V, node: &'ast Selection) {
use super::ast::Selection::*;

visitor.visit_selection(node);
match node {
Field(inner) => {
visitor.visit_field(inner);
walk_field(visitor, inner);
Comment on lines +202 to +203
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this pattern. You first do visit_field and then walk_field which calls visit_field again. Same with fragments. Am I misunderstanding something?

Copy link

@vladinator1000 vladinator1000 Jul 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a similar question. It seems that every walk function calls visit_node and then then calls a walk_node function that calls the same visit_node. For example walk_subscription calls visitor.visit_selection_set and then calls walk_selection_set which in turn calls visitor.visit_selection_set a second time.

Is that how it's supposed to work? I thought you only need to visit once.

}
FragmentSpread(inner) => {
visitor.visit_fragment_spread(inner);
walk_fragment_spread(visitor, inner);
}
InlineFragment(inner) => {
visitor.visit_inline_fragment(inner);
walk_inline_fragment(visitor, inner);
}
}
}

fn walk_field<'ast, V: QueryVisitor<'ast>>(visitor: &mut V, node: &'ast Field) {
visitor.visit_field(node)
}

fn walk_fragment_spread<'ast, V: QueryVisitor<'ast>>(visitor: &mut V, node: &'ast FragmentSpread) {
visitor.visit_fragment_spread(node)
}

fn walk_inline_fragment<'ast, V: QueryVisitor<'ast>>(visitor: &mut V, node: &'ast InlineFragment) {
visitor.visit_inline_fragment(node);
walk_selection_set(visitor, &node.selection_set);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as for fragments, would it make sense to visit the selection set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in cb71a7e 👍

1 change: 1 addition & 0 deletions src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod ast;
mod grammar;
mod error;
mod format;
pub mod schema_visitor;

pub use self::ast::*;
pub use self::error::ParseError;
Expand Down
Loading