forked from facebook/relay
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize required_directive, and add a directive_finder trait
Reviewed By: kassens Differential Revision: D32378752 fbshipit-source-id: 0eaa5ea30b816fd03cdc638dd0f3654837957712
- Loading branch information
1 parent
38283e3
commit 16c84f7
Showing
4 changed files
with
172 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
pub trait EarlyReturnVisitor { | ||
fn visit(selections: &[Selection]) { | ||
let mut to_visit = Vec::from(selections); | ||
while let Some(selection) = to_visit.pop() { | ||
match selection { | ||
Selection::FragmentSpread(s) => {} | ||
Selection::InlineFragment(_) => todo!(), | ||
Selection::LinkedField(_) => todo!(), | ||
Selection::ScalarField(_) => todo!(), | ||
Selection::Condition(_) => todo!(), | ||
} | ||
} | ||
} | ||
|
||
fn visit_fragment( | ||
&mut self, | ||
to_visit: &mut Vec<&Selection>, | ||
fragment: &FragmentDefinition, | ||
) -> bool { | ||
to_visit.push(fragment.selections); | ||
false | ||
} | ||
|
||
fn visit_inline_fragment( | ||
&mut self, | ||
to_visit: &mut Vec<&Selection>, | ||
fragment: &InlineFragment, | ||
) -> bool { | ||
to_visit.push(fragment.selections); | ||
} | ||
|
||
fn visit_linkedField( | ||
&mut self, | ||
to_visit: &mut Vec<&Selection>, | ||
fragment: &InlineFragment, | ||
) -> bool { | ||
to_visit.push(fragment.selections); | ||
} | ||
} |
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,76 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
use graphql_ir::{Directive, FragmentSpread, InlineFragment, LinkedField, Selection}; | ||
|
||
/// A trait for implementating finding if a directive exists in an IR. | ||
/// Returns `true` if the directive is found. | ||
/// We can make it more general, if there are more similar use cases | ||
pub trait DirectiveFinder { | ||
fn find(&mut self, mut to_visit: Vec<&Selection>) -> bool { | ||
while let Some(selection) = to_visit.pop() { | ||
match selection { | ||
Selection::FragmentSpread(f) => { | ||
if self.visit_fragment_spread(f) { | ||
return true; | ||
} | ||
} | ||
Selection::InlineFragment(f) => { | ||
if self.visit_inline_fragment(&mut to_visit, f) { | ||
return true; | ||
} | ||
} | ||
Selection::LinkedField(f) => { | ||
if self.visit_linked_field(&mut to_visit, f) { | ||
return true; | ||
} | ||
} | ||
Selection::ScalarField(f) => { | ||
if self.visit_directives(&f.directives) { | ||
return true; | ||
} | ||
} | ||
Selection::Condition(c) => { | ||
to_visit.extend(c.selections.iter()); | ||
} | ||
} | ||
} | ||
false | ||
} | ||
|
||
fn visit_directive(&self, directive: &Directive) -> bool; | ||
|
||
fn visit_directives(&self, directives: &[Directive]) -> bool { | ||
directives.iter().any(|d| self.visit_directive(d)) | ||
} | ||
|
||
fn visit_fragment_spread(&mut self, _fragment_spread: &FragmentSpread) -> bool; | ||
|
||
fn visit_inline_fragment<'a>( | ||
&mut self, | ||
to_visit: &mut Vec<&'a Selection>, | ||
fragment: &'a InlineFragment, | ||
) -> bool { | ||
if self.visit_directives(&fragment.directives) { | ||
return true; | ||
} | ||
to_visit.extend(fragment.selections.iter()); | ||
false | ||
} | ||
|
||
fn visit_linked_field<'a>( | ||
&mut self, | ||
to_visit: &mut Vec<&'a Selection>, | ||
field: &'a LinkedField, | ||
) -> bool { | ||
if self.visit_directives(&field.directives) { | ||
return true; | ||
} | ||
to_visit.extend(field.selections.iter()); | ||
false | ||
} | ||
} |
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