Skip to content

Commit da005fc

Browse files
Add field_original_name and field_alias to LookAheadMethods (#1199)
Co-authored-by: Kai Ren <[email protected]>
1 parent e76b961 commit da005fc

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

juniper/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ All user visible changes to `juniper` crate will be documented in this file. Thi
6565
- [`rust_decimal` crate] integration behind `rust_decimal` [Cargo feature]. ([#1060])
6666
- `js` [Cargo feature] enabling `js-sys` and `wasm-bindgen` support for `wasm32-unknown-unknown` target. ([#1118], [#1147])
6767
- `LookAheadMethods::applies_for()` method. ([#1138], [#1145])
68+
- `LookAheadMethods::field_original_name()` and `LookAheadMethods::field_alias()` methods. ([#1199])
6869

6970
### Changed
7071

@@ -126,6 +127,7 @@ All user visible changes to `juniper` crate will be documented in this file. Thi
126127
[#1188]: /../../pull/1188
127128
[#1190]: /../../pull/1190
128129
[#1193]: /../../pull/1193
130+
[#1199]: /../../pull/1199
129131
[#1203]: /../../pull/1203
130132
[ba1ed85b]: /../../commit/ba1ed85b3c3dd77fbae7baf6bc4e693321a94083
131133
[CVE-2022-31173]: /../../security/advisories/GHSA-4rx6-g5vg-5f3j

juniper/src/executor/look_ahead.rs

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -333,44 +333,60 @@ pub struct ConcreteLookAheadSelection<'a, S: 'a> {
333333
/// `'sel` lifetime is intended to point to the data that this `LookAheadSelection` (or
334334
/// `ConcreteLookAheadSelection`) points to.
335335
pub trait LookAheadMethods<'sel, S> {
336-
/// Get the (potentially aliased) name of the field represented by the current selection
336+
/// Returns the original name of the field, represented by the current selection.
337+
fn field_original_name(&self) -> &'sel str;
338+
339+
/// Returns the alias of the field, represented by the current selection, if any.
340+
fn field_alias(&self) -> Option<&'sel str>;
341+
342+
/// Returns the (potentially aliased) name of the field, represented by the current selection.
337343
fn field_name(&self) -> &'sel str;
338344

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

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

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

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

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

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

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

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

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

373381
impl<'a, S> LookAheadMethods<'a, S> for ConcreteLookAheadSelection<'a, S> {
382+
fn field_original_name(&self) -> &'a str {
383+
self.name
384+
}
385+
386+
fn field_alias(&self) -> Option<&'a str> {
387+
self.alias
388+
}
389+
374390
fn field_name(&self) -> &'a str {
375391
self.alias.unwrap_or(self.name)
376392
}
@@ -408,6 +424,14 @@ impl<'a, S> LookAheadMethods<'a, S> for ConcreteLookAheadSelection<'a, S> {
408424
}
409425

410426
impl<'a, S> LookAheadMethods<'a, S> for LookAheadSelection<'a, S> {
427+
fn field_original_name(&self) -> &'a str {
428+
self.name
429+
}
430+
431+
fn field_alias(&self) -> Option<&'a str> {
432+
self.alias
433+
}
434+
411435
fn field_name(&self) -> &'a str {
412436
self.alias.unwrap_or(self.name)
413437
}
@@ -1419,6 +1443,8 @@ query Hero {
14191443
)
14201444
.unwrap();
14211445

1446+
assert_eq!(look_ahead.field_original_name(), "hero");
1447+
assert!(look_ahead.field_alias().is_none());
14221448
assert_eq!(look_ahead.field_name(), "hero");
14231449

14241450
assert!(look_ahead.has_arguments());
@@ -1436,8 +1462,8 @@ query Hero {
14361462
let name_child = children.next().unwrap();
14371463
assert!(look_ahead.has_child("name"));
14381464
assert_eq!(name_child, look_ahead.select_child("name").unwrap());
1439-
assert_eq!(name_child.name, "name");
1440-
assert_eq!(name_child.alias, None);
1465+
assert_eq!(name_child.field_original_name(), "name");
1466+
assert_eq!(name_child.field_alias(), None);
14411467
assert_eq!(name_child.field_name(), "name");
14421468
assert!(!name_child.has_arguments());
14431469
assert!(!name_child.has_children());
@@ -1448,17 +1474,17 @@ query Hero {
14481474
aliased_name_child,
14491475
look_ahead.select_child("aliasedName").unwrap()
14501476
);
1451-
assert_eq!(aliased_name_child.name, "name");
1452-
assert_eq!(aliased_name_child.alias, Some("aliasedName"));
1477+
assert_eq!(aliased_name_child.field_original_name(), "name");
1478+
assert_eq!(aliased_name_child.field_alias(), Some("aliasedName"));
14531479
assert_eq!(aliased_name_child.field_name(), "aliasedName");
14541480
assert!(!aliased_name_child.has_arguments());
14551481
assert!(!aliased_name_child.has_children());
14561482

14571483
let friends_child = children.next().unwrap();
14581484
assert!(look_ahead.has_child("friends"));
14591485
assert_eq!(friends_child, look_ahead.select_child("friends").unwrap());
1460-
assert_eq!(friends_child.name, "friends");
1461-
assert_eq!(friends_child.alias, None);
1486+
assert_eq!(friends_child.field_original_name(), "friends");
1487+
assert_eq!(friends_child.field_alias(), None);
14621488
assert_eq!(friends_child.field_name(), "friends");
14631489
assert!(!friends_child.has_arguments());
14641490
assert!(friends_child.has_children());
@@ -1470,8 +1496,8 @@ query Hero {
14701496
let child = friends_children.next().unwrap();
14711497
assert!(friends_child.has_child("name"));
14721498
assert_eq!(child, friends_child.select_child("name").unwrap());
1473-
assert_eq!(child.name, "name");
1474-
assert_eq!(child.alias, None);
1499+
assert_eq!(child.field_original_name(), "name");
1500+
assert_eq!(child.field_alias(), None);
14751501
assert_eq!(child.field_name(), "name");
14761502
assert!(!child.has_arguments());
14771503
assert!(!child.has_children());

0 commit comments

Comments
 (0)