Skip to content

Commit

Permalink
Add field_original_name and field_alias to LookAheadMethods (#1199
Browse files Browse the repository at this point in the history
)

Co-authored-by: Kai Ren <[email protected]>
  • Loading branch information
soerenmeier and tyranron authored Nov 2, 2023
1 parent e76b961 commit da005fc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
2 changes: 2 additions & 0 deletions juniper/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ All user visible changes to `juniper` crate will be documented in this file. Thi
- [`rust_decimal` crate] integration behind `rust_decimal` [Cargo feature]. ([#1060])
- `js` [Cargo feature] enabling `js-sys` and `wasm-bindgen` support for `wasm32-unknown-unknown` target. ([#1118], [#1147])
- `LookAheadMethods::applies_for()` method. ([#1138], [#1145])
- `LookAheadMethods::field_original_name()` and `LookAheadMethods::field_alias()` methods. ([#1199])

### Changed

Expand Down Expand Up @@ -126,6 +127,7 @@ All user visible changes to `juniper` crate will be documented in this file. Thi
[#1188]: /../../pull/1188
[#1190]: /../../pull/1190
[#1193]: /../../pull/1193
[#1199]: /../../pull/1199
[#1203]: /../../pull/1203
[ba1ed85b]: /../../commit/ba1ed85b3c3dd77fbae7baf6bc4e693321a94083
[CVE-2022-31173]: /../../security/advisories/GHSA-4rx6-g5vg-5f3j
Expand Down
66 changes: 46 additions & 20 deletions juniper/src/executor/look_ahead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,44 +333,60 @@ pub struct ConcreteLookAheadSelection<'a, S: 'a> {
/// `'sel` lifetime is intended to point to the data that this `LookAheadSelection` (or
/// `ConcreteLookAheadSelection`) points to.
pub trait LookAheadMethods<'sel, S> {
/// Get the (potentially aliased) name of the field represented by the current selection
/// Returns the original name of the field, represented by the current selection.
fn field_original_name(&self) -> &'sel str;

/// Returns the alias of the field, represented by the current selection, if any.
fn field_alias(&self) -> Option<&'sel str>;

/// Returns the (potentially aliased) name of the field, represented by the current selection.
fn field_name(&self) -> &'sel str;

/// Get the the child selection for a given field
/// If a child has an alias, it will only match if the alias matches `name`
/// Returns the child selection for the specified field.
///
/// If a child has an alias, it will only match if the alias matches the specified `name`.
fn select_child(&self, name: &str) -> Option<&Self>;

/// Check if a given child selection with a name exists
/// If a child has an alias, it will only match if the alias matches `name`
/// Checks if a child selection with the specified `name` exists.
///
/// If a child has an alias, it will only match if the alias matches the specified `name`.
fn has_child(&self, name: &str) -> bool {
self.select_child(name).is_some()
}

/// Does the current node have any arguments?
/// Indicates whether the current node has any arguments.
fn has_arguments(&self) -> bool;

/// Does the current node have any children?
/// Indicates whether the current node has any children.
fn has_children(&self) -> bool;

/// Get the top level arguments for the current selection
/// Returns the top level arguments from the current selection.
fn arguments(&self) -> &[LookAheadArgument<S>];

/// Get the top level argument with a given name from the current selection
/// Returns the top level argument with the specified `name` from the current selection.
fn argument(&self, name: &str) -> Option<&LookAheadArgument<S>> {
self.arguments().iter().find(|a| a.name == name)
}

/// Get the (possibly aliased) names of the top level children for the current selection
/// Returns the (possibly aliased) names of the top level children from the current selection.
fn child_names(&self) -> Vec<&'sel str>;

/// Get an iterator over the children for the current selection
/// Returns an [`Iterator`] over the children from the current selection.
fn children(&self) -> Vec<&Self>;

/// Get the parent type in case there is any for this selection
/// Returns the parent type, in case there is any for the current selection.
fn applies_for(&self) -> Option<&str>;
}

impl<'a, S> LookAheadMethods<'a, S> for ConcreteLookAheadSelection<'a, S> {
fn field_original_name(&self) -> &'a str {
self.name
}

fn field_alias(&self) -> Option<&'a str> {
self.alias
}

fn field_name(&self) -> &'a str {
self.alias.unwrap_or(self.name)
}
Expand Down Expand Up @@ -408,6 +424,14 @@ impl<'a, S> LookAheadMethods<'a, S> for ConcreteLookAheadSelection<'a, S> {
}

impl<'a, S> LookAheadMethods<'a, S> for LookAheadSelection<'a, S> {
fn field_original_name(&self) -> &'a str {
self.name
}

fn field_alias(&self) -> Option<&'a str> {
self.alias
}

fn field_name(&self) -> &'a str {
self.alias.unwrap_or(self.name)
}
Expand Down Expand Up @@ -1419,6 +1443,8 @@ query Hero {
)
.unwrap();

assert_eq!(look_ahead.field_original_name(), "hero");
assert!(look_ahead.field_alias().is_none());
assert_eq!(look_ahead.field_name(), "hero");

assert!(look_ahead.has_arguments());
Expand All @@ -1436,8 +1462,8 @@ query Hero {
let name_child = children.next().unwrap();
assert!(look_ahead.has_child("name"));
assert_eq!(name_child, look_ahead.select_child("name").unwrap());
assert_eq!(name_child.name, "name");
assert_eq!(name_child.alias, None);
assert_eq!(name_child.field_original_name(), "name");
assert_eq!(name_child.field_alias(), None);
assert_eq!(name_child.field_name(), "name");
assert!(!name_child.has_arguments());
assert!(!name_child.has_children());
Expand All @@ -1448,17 +1474,17 @@ query Hero {
aliased_name_child,
look_ahead.select_child("aliasedName").unwrap()
);
assert_eq!(aliased_name_child.name, "name");
assert_eq!(aliased_name_child.alias, Some("aliasedName"));
assert_eq!(aliased_name_child.field_original_name(), "name");
assert_eq!(aliased_name_child.field_alias(), Some("aliasedName"));
assert_eq!(aliased_name_child.field_name(), "aliasedName");
assert!(!aliased_name_child.has_arguments());
assert!(!aliased_name_child.has_children());

let friends_child = children.next().unwrap();
assert!(look_ahead.has_child("friends"));
assert_eq!(friends_child, look_ahead.select_child("friends").unwrap());
assert_eq!(friends_child.name, "friends");
assert_eq!(friends_child.alias, None);
assert_eq!(friends_child.field_original_name(), "friends");
assert_eq!(friends_child.field_alias(), None);
assert_eq!(friends_child.field_name(), "friends");
assert!(!friends_child.has_arguments());
assert!(friends_child.has_children());
Expand All @@ -1470,8 +1496,8 @@ query Hero {
let child = friends_children.next().unwrap();
assert!(friends_child.has_child("name"));
assert_eq!(child, friends_child.select_child("name").unwrap());
assert_eq!(child.name, "name");
assert_eq!(child.alias, None);
assert_eq!(child.field_original_name(), "name");
assert_eq!(child.field_alias(), None);
assert_eq!(child.field_name(), "name");
assert!(!child.has_arguments());
assert!(!child.has_children());
Expand Down

0 comments on commit da005fc

Please sign in to comment.