Small improvements + rustfmt
This commit is contained in:
parent
dd99914fbe
commit
61c07b95fc
2 changed files with 590 additions and 631 deletions
juniper/src/executor
|
@ -34,21 +34,22 @@ impl<'a> LookAheadValue<'a> {
|
|||
InputValue::Boolean(b) => LookAheadValue::Boolean(b),
|
||||
InputValue::Enum(ref e) => LookAheadValue::Enum(e),
|
||||
InputValue::Variable(ref v) => Self::from_input_value(vars.get(v).unwrap(), vars),
|
||||
InputValue::List(ref l) => LookAheadValue::List(
|
||||
l.iter()
|
||||
.map(|i| LookAheadValue::from_input_value(&i.item, vars))
|
||||
.collect(),
|
||||
),
|
||||
InputValue::Object(ref o) => LookAheadValue::Object(
|
||||
o.iter()
|
||||
.map(|&(ref n, ref i)| {
|
||||
(
|
||||
&n.item as &str,
|
||||
LookAheadValue::from_input_value(&i.item, vars),
|
||||
)
|
||||
InputValue::List(ref l) => {
|
||||
LookAheadValue::List(l.iter()
|
||||
.map(|i| {
|
||||
LookAheadValue::from_input_value(&i.item, vars)
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
.collect())
|
||||
}
|
||||
InputValue::Object(ref o) => {
|
||||
LookAheadValue::Object(o.iter()
|
||||
.map(|&(ref n, ref i)| {
|
||||
(&n.item as &str,
|
||||
LookAheadValue::from_input_value(&i.item,
|
||||
vars))
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -60,15 +61,18 @@ pub struct LookAheadArgument<'a> {
|
|||
}
|
||||
|
||||
impl<'a> LookAheadArgument<'a> {
|
||||
fn new(
|
||||
&(ref name, ref value): &'a (Spanning<&'a str>, Spanning<InputValue>),
|
||||
vars: &'a Variables,
|
||||
) -> Self {
|
||||
pub(super) fn new(&(ref name, ref value): &'a (Spanning<&'a str>, Spanning<InputValue>),
|
||||
vars: &'a Variables)
|
||||
-> Self {
|
||||
LookAheadArgument {
|
||||
name: name.item,
|
||||
value: LookAheadValue::from_input_value(&value.item, vars),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn value(&'a self) -> &LookAheadValue<'a> {
|
||||
&self.value
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
|
@ -89,38 +93,37 @@ impl<'a> LookAheadSelection<'a> {
|
|||
fn should_include(directives: Option<&Vec<Spanning<Directive>>>, vars: &Variables) -> bool {
|
||||
directives
|
||||
.map(|d| {
|
||||
d.iter().all(|d| {
|
||||
d.iter()
|
||||
.all(|d| {
|
||||
let d = &d.item;
|
||||
let arguments = &d.arguments;
|
||||
match (d.name.item, arguments) {
|
||||
("include", &Some(ref a)) => a.item
|
||||
("include", &Some(ref a)) => {
|
||||
a.item
|
||||
.items
|
||||
.iter()
|
||||
.find(|item| item.0.item == "if")
|
||||
.map(|&(_, ref v)| {
|
||||
if let LookAheadValue::Boolean(b) =
|
||||
LookAheadValue::from_input_value(&v.item, vars)
|
||||
{
|
||||
.map(|&(_, ref v)| if let LookAheadValue::Boolean(b) =
|
||||
LookAheadValue::from_input_value(&v.item, vars) {
|
||||
b
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
.unwrap_or(false),
|
||||
("skip", &Some(ref a)) => a.item
|
||||
.unwrap_or(false)
|
||||
}
|
||||
("skip", &Some(ref a)) => {
|
||||
a.item
|
||||
.items
|
||||
.iter()
|
||||
.find(|item| item.0.item == "if")
|
||||
.map(|&(_, ref v)| {
|
||||
if let LookAheadValue::Boolean(b) =
|
||||
LookAheadValue::from_input_value(&v.item, vars)
|
||||
{
|
||||
.map(|&(_, ref v)| if let LookAheadValue::Boolean(b) =
|
||||
LookAheadValue::from_input_value(&v.item, vars) {
|
||||
!b
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
.unwrap_or(false),
|
||||
.unwrap_or(false)
|
||||
}
|
||||
("skip", &None) => false,
|
||||
("include", &None) => true,
|
||||
(_, _) => unreachable!(),
|
||||
|
@ -130,20 +133,18 @@ impl<'a> LookAheadSelection<'a> {
|
|||
.unwrap_or(true)
|
||||
}
|
||||
|
||||
pub(super) fn build_from_selection(
|
||||
s: &'a Selection<'a>,
|
||||
pub(super) fn build_from_selection(s: &'a Selection<'a>,
|
||||
vars: &'a Variables,
|
||||
fragments: &'a HashMap<&'a str, &'a Fragment<'a>>,
|
||||
) -> LookAheadSelection<'a> {
|
||||
fragments: &'a HashMap<&'a str, &'a Fragment<'a>>)
|
||||
-> LookAheadSelection<'a> {
|
||||
Self::build_from_selection_with_parent(s, None, vars, fragments).unwrap()
|
||||
}
|
||||
|
||||
fn build_from_selection_with_parent(
|
||||
s: &'a Selection<'a>,
|
||||
fn build_from_selection_with_parent(s: &'a Selection<'a>,
|
||||
parent: Option<&mut Self>,
|
||||
vars: &'a Variables,
|
||||
fragments: &'a HashMap<&'a str, &'a Fragment<'a>>,
|
||||
) -> Option<LookAheadSelection<'a>> {
|
||||
fragments: &'a HashMap<&'a str, &'a Fragment<'a>>)
|
||||
-> Option<LookAheadSelection<'a>> {
|
||||
let empty: &[Selection] = &[];
|
||||
match *s {
|
||||
Selection::Field(ref field) => {
|
||||
|
@ -176,18 +177,16 @@ impl<'a> LookAheadSelection<'a> {
|
|||
.as_ref()
|
||||
.map(|s| s as &[_])
|
||||
.unwrap_or_else(|| empty)
|
||||
.iter()
|
||||
{
|
||||
let s = LookAheadSelection::build_from_selection_with_parent(
|
||||
c,
|
||||
.iter() {
|
||||
let s = LookAheadSelection::build_from_selection_with_parent(c,
|
||||
Some(&mut ret),
|
||||
vars,
|
||||
fragments,
|
||||
);
|
||||
fragments);
|
||||
assert!(s.is_none());
|
||||
}
|
||||
if let Some(p) = parent {
|
||||
p.childs.push(ChildSelection {
|
||||
p.childs
|
||||
.push(ChildSelection {
|
||||
inner: ret,
|
||||
applies_for: Applies::All,
|
||||
});
|
||||
|
@ -204,12 +203,10 @@ impl<'a> LookAheadSelection<'a> {
|
|||
let parent = parent.unwrap();
|
||||
let f = fragments.get(&fragment.item.name.item).unwrap();
|
||||
for c in f.selection_set.iter() {
|
||||
let s = LookAheadSelection::build_from_selection_with_parent(
|
||||
c,
|
||||
let s = LookAheadSelection::build_from_selection_with_parent(c,
|
||||
Some(parent),
|
||||
vars,
|
||||
fragments,
|
||||
);
|
||||
fragments);
|
||||
assert!(s.is_none());
|
||||
}
|
||||
None
|
||||
|
@ -221,12 +218,10 @@ impl<'a> LookAheadSelection<'a> {
|
|||
}
|
||||
let parent = parent.unwrap();
|
||||
for c in inline.item.selection_set.iter() {
|
||||
let s = LookAheadSelection::build_from_selection_with_parent(
|
||||
c,
|
||||
let s = LookAheadSelection::build_from_selection_with_parent(c,
|
||||
Some(parent),
|
||||
vars,
|
||||
fragments,
|
||||
);
|
||||
fragments);
|
||||
assert!(s.is_none());
|
||||
if let Some(ref c) = inline.item.type_condition.as_ref().map(|t| t.item) {
|
||||
if let Some(p) = parent.childs.last_mut() {
|
||||
|
@ -257,6 +252,14 @@ impl<'a> LookAheadSelection<'a> {
|
|||
arguments: self.arguments.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn arguments(&self) -> &[LookAheadArgument] {
|
||||
&self.arguments
|
||||
}
|
||||
|
||||
pub fn argument(&self, name: &str) -> Option<&LookAheadArgument> {
|
||||
self.arguments.iter().find(|a| a.name == name)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
|
@ -293,7 +296,10 @@ impl<'a> LookAheadMethods for LookAheadSelection<'a> {
|
|||
}
|
||||
|
||||
fn select_child(&self, name: &str) -> Option<&Self> {
|
||||
self.childs.iter().find(|c| c.inner.name == name).map(|s| &s.inner)
|
||||
self.childs
|
||||
.iter()
|
||||
.find(|c| c.inner.name == name)
|
||||
.map(|s| &s.inner)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -316,31 +322,27 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn check_simple_query() {
|
||||
let docs = ::parse_document_source(
|
||||
"
|
||||
let docs = ::parse_document_source("
|
||||
query Hero {
|
||||
hero {
|
||||
id
|
||||
name
|
||||
}
|
||||
}
|
||||
",
|
||||
).unwrap();
|
||||
")
|
||||
.unwrap();
|
||||
let fragments = extract_fragments(&docs);
|
||||
|
||||
if let ::ast::Definition::Operation(ref op) = docs[0] {
|
||||
let vars = Variables::default();
|
||||
let look_ahead = LookAheadSelection::build_from_selection(
|
||||
&op.item.selection_set[0],
|
||||
let look_ahead = LookAheadSelection::build_from_selection(&op.item.selection_set[0],
|
||||
&vars,
|
||||
&fragments,
|
||||
);
|
||||
&fragments);
|
||||
let expected = LookAheadSelection {
|
||||
name: "hero",
|
||||
alias: None,
|
||||
arguments: Vec::new(),
|
||||
childs: vec![
|
||||
ChildSelection {
|
||||
childs: vec![ChildSelection {
|
||||
inner: LookAheadSelection {
|
||||
name: "id",
|
||||
alias: None,
|
||||
|
@ -357,8 +359,7 @@ query Hero {
|
|||
childs: Vec::new(),
|
||||
},
|
||||
applies_for: Applies::All,
|
||||
},
|
||||
],
|
||||
}],
|
||||
};
|
||||
assert_eq!(look_ahead, expected);
|
||||
} else {
|
||||
|
@ -368,31 +369,27 @@ query Hero {
|
|||
|
||||
#[test]
|
||||
fn check_query_with_alias() {
|
||||
let docs = ::parse_document_source(
|
||||
"
|
||||
let docs = ::parse_document_source("
|
||||
query Hero {
|
||||
custom_hero: hero {
|
||||
id
|
||||
my_name: name
|
||||
}
|
||||
}
|
||||
",
|
||||
).unwrap();
|
||||
")
|
||||
.unwrap();
|
||||
let fragments = extract_fragments(&docs);
|
||||
|
||||
if let ::ast::Definition::Operation(ref op) = docs[0] {
|
||||
let vars = Variables::default();
|
||||
let look_ahead = LookAheadSelection::build_from_selection(
|
||||
&op.item.selection_set[0],
|
||||
let look_ahead = LookAheadSelection::build_from_selection(&op.item.selection_set[0],
|
||||
&vars,
|
||||
&fragments,
|
||||
);
|
||||
&fragments);
|
||||
let expected = LookAheadSelection {
|
||||
name: "hero",
|
||||
alias: Some("custom_hero"),
|
||||
arguments: Vec::new(),
|
||||
childs: vec![
|
||||
ChildSelection {
|
||||
childs: vec![ChildSelection {
|
||||
inner: LookAheadSelection {
|
||||
name: "id",
|
||||
alias: None,
|
||||
|
@ -409,8 +406,7 @@ query Hero {
|
|||
childs: Vec::new(),
|
||||
},
|
||||
applies_for: Applies::All,
|
||||
},
|
||||
],
|
||||
}],
|
||||
};
|
||||
assert_eq!(look_ahead, expected);
|
||||
} else {
|
||||
|
@ -420,8 +416,7 @@ query Hero {
|
|||
|
||||
#[test]
|
||||
fn check_query_with_child() {
|
||||
let docs = ::parse_document_source(
|
||||
"
|
||||
let docs = ::parse_document_source("
|
||||
query Hero {
|
||||
hero {
|
||||
id
|
||||
|
@ -432,23 +427,20 @@ query Hero {
|
|||
}
|
||||
}
|
||||
}
|
||||
",
|
||||
).unwrap();
|
||||
")
|
||||
.unwrap();
|
||||
let fragments = extract_fragments(&docs);
|
||||
|
||||
if let ::ast::Definition::Operation(ref op) = docs[0] {
|
||||
let vars = Variables::default();
|
||||
let look_ahead = LookAheadSelection::build_from_selection(
|
||||
&op.item.selection_set[0],
|
||||
let look_ahead = LookAheadSelection::build_from_selection(&op.item.selection_set[0],
|
||||
&vars,
|
||||
&fragments,
|
||||
);
|
||||
&fragments);
|
||||
let expected = LookAheadSelection {
|
||||
name: "hero",
|
||||
alias: None,
|
||||
arguments: Vec::new(),
|
||||
childs: vec![
|
||||
ChildSelection {
|
||||
childs: vec![ChildSelection {
|
||||
inner: LookAheadSelection {
|
||||
name: "id",
|
||||
alias: None,
|
||||
|
@ -471,8 +463,7 @@ query Hero {
|
|||
name: "friends",
|
||||
alias: None,
|
||||
arguments: Vec::new(),
|
||||
childs: vec![
|
||||
ChildSelection {
|
||||
childs: vec![ChildSelection {
|
||||
inner: LookAheadSelection {
|
||||
name: "name",
|
||||
alias: None,
|
||||
|
@ -489,12 +480,10 @@ query Hero {
|
|||
childs: Vec::new(),
|
||||
},
|
||||
applies_for: Applies::All,
|
||||
},
|
||||
],
|
||||
}],
|
||||
},
|
||||
applies_for: Applies::All,
|
||||
},
|
||||
],
|
||||
}],
|
||||
};
|
||||
assert_eq!(look_ahead, expected);
|
||||
} else {
|
||||
|
@ -504,36 +493,30 @@ query Hero {
|
|||
|
||||
#[test]
|
||||
fn check_query_with_argument() {
|
||||
let docs = ::parse_document_source(
|
||||
"
|
||||
let docs = ::parse_document_source("
|
||||
query Hero {
|
||||
hero(episode: EMPIRE) {
|
||||
id
|
||||
name(uppercase: true)
|
||||
}
|
||||
}
|
||||
",
|
||||
).unwrap();
|
||||
")
|
||||
.unwrap();
|
||||
let fragments = extract_fragments(&docs);
|
||||
|
||||
if let ::ast::Definition::Operation(ref op) = docs[0] {
|
||||
let vars = Variables::default();
|
||||
let look_ahead = LookAheadSelection::build_from_selection(
|
||||
&op.item.selection_set[0],
|
||||
let look_ahead = LookAheadSelection::build_from_selection(&op.item.selection_set[0],
|
||||
&vars,
|
||||
&fragments,
|
||||
);
|
||||
&fragments);
|
||||
let expected = LookAheadSelection {
|
||||
name: "hero",
|
||||
alias: None,
|
||||
arguments: vec![
|
||||
LookAheadArgument {
|
||||
arguments: vec![LookAheadArgument {
|
||||
name: "episode",
|
||||
value: LookAheadValue::Enum("EMPIRE"),
|
||||
},
|
||||
],
|
||||
childs: vec![
|
||||
ChildSelection {
|
||||
}],
|
||||
childs: vec![ChildSelection {
|
||||
inner: LookAheadSelection {
|
||||
name: "id",
|
||||
alias: None,
|
||||
|
@ -546,17 +529,14 @@ query Hero {
|
|||
inner: LookAheadSelection {
|
||||
name: "name",
|
||||
alias: None,
|
||||
arguments: vec![
|
||||
LookAheadArgument {
|
||||
arguments: vec![LookAheadArgument {
|
||||
name: "uppercase",
|
||||
value: LookAheadValue::Boolean(true),
|
||||
},
|
||||
],
|
||||
}],
|
||||
childs: Vec::new(),
|
||||
},
|
||||
applies_for: Applies::All,
|
||||
},
|
||||
],
|
||||
}],
|
||||
};
|
||||
assert_eq!(look_ahead, expected);
|
||||
} else {
|
||||
|
@ -566,37 +546,31 @@ query Hero {
|
|||
|
||||
#[test]
|
||||
fn check_query_with_variable() {
|
||||
let docs = ::parse_document_source(
|
||||
"
|
||||
let docs = ::parse_document_source("
|
||||
query Hero($episode: Episode) {
|
||||
hero(episode: $episode) {
|
||||
id
|
||||
name
|
||||
}
|
||||
}
|
||||
",
|
||||
).unwrap();
|
||||
")
|
||||
.unwrap();
|
||||
let fragments = extract_fragments(&docs);
|
||||
|
||||
if let ::ast::Definition::Operation(ref op) = docs[0] {
|
||||
let mut vars = Variables::default();
|
||||
vars.insert("episode".into(), InputValue::Enum("JEDI".into()));
|
||||
let look_ahead = LookAheadSelection::build_from_selection(
|
||||
&op.item.selection_set[0],
|
||||
let look_ahead = LookAheadSelection::build_from_selection(&op.item.selection_set[0],
|
||||
&vars,
|
||||
&fragments,
|
||||
);
|
||||
&fragments);
|
||||
let expected = LookAheadSelection {
|
||||
name: "hero",
|
||||
alias: None,
|
||||
arguments: vec![
|
||||
LookAheadArgument {
|
||||
arguments: vec![LookAheadArgument {
|
||||
name: "episode",
|
||||
value: LookAheadValue::Enum("JEDI"),
|
||||
},
|
||||
],
|
||||
childs: vec![
|
||||
ChildSelection {
|
||||
}],
|
||||
childs: vec![ChildSelection {
|
||||
inner: LookAheadSelection {
|
||||
name: "id",
|
||||
alias: None,
|
||||
|
@ -613,8 +587,7 @@ query Hero($episode: Episode) {
|
|||
childs: Vec::new(),
|
||||
},
|
||||
applies_for: Applies::All,
|
||||
},
|
||||
],
|
||||
}],
|
||||
};
|
||||
assert_eq!(look_ahead, expected);
|
||||
} else {
|
||||
|
@ -624,8 +597,7 @@ query Hero($episode: Episode) {
|
|||
|
||||
#[test]
|
||||
fn check_query_with_fragment() {
|
||||
let docs = ::parse_document_source(
|
||||
"
|
||||
let docs = ::parse_document_source("
|
||||
query Hero {
|
||||
hero {
|
||||
id
|
||||
|
@ -637,23 +609,20 @@ fragment commonFields on Character {
|
|||
name
|
||||
appearsIn
|
||||
}
|
||||
",
|
||||
).unwrap();
|
||||
")
|
||||
.unwrap();
|
||||
let fragments = extract_fragments(&docs);
|
||||
|
||||
if let ::ast::Definition::Operation(ref op) = docs[0] {
|
||||
let vars = Variables::default();
|
||||
let look_ahead = LookAheadSelection::build_from_selection(
|
||||
&op.item.selection_set[0],
|
||||
let look_ahead = LookAheadSelection::build_from_selection(&op.item.selection_set[0],
|
||||
&vars,
|
||||
&fragments,
|
||||
);
|
||||
&fragments);
|
||||
let expected = LookAheadSelection {
|
||||
name: "hero",
|
||||
alias: None,
|
||||
arguments: Vec::new(),
|
||||
childs: vec![
|
||||
ChildSelection {
|
||||
childs: vec![ChildSelection {
|
||||
inner: LookAheadSelection {
|
||||
name: "id",
|
||||
alias: None,
|
||||
|
@ -679,8 +648,7 @@ fragment commonFields on Character {
|
|||
childs: Vec::new(),
|
||||
},
|
||||
applies_for: Applies::All,
|
||||
},
|
||||
],
|
||||
}],
|
||||
};
|
||||
assert_eq!(look_ahead, expected);
|
||||
} else {
|
||||
|
@ -690,8 +658,7 @@ fragment commonFields on Character {
|
|||
|
||||
#[test]
|
||||
fn check_query_with_directives() {
|
||||
let docs = ::parse_document_source(
|
||||
"
|
||||
let docs = ::parse_document_source("
|
||||
query Hero {
|
||||
hero {
|
||||
id @include(if: true)
|
||||
|
@ -699,23 +666,20 @@ query Hero {
|
|||
appearsIn @skip(if: true)
|
||||
height @skip(if: false)
|
||||
}
|
||||
}",
|
||||
).unwrap();
|
||||
}")
|
||||
.unwrap();
|
||||
let fragments = extract_fragments(&docs);
|
||||
|
||||
if let ::ast::Definition::Operation(ref op) = docs[0] {
|
||||
let vars = Variables::default();
|
||||
let look_ahead = LookAheadSelection::build_from_selection(
|
||||
&op.item.selection_set[0],
|
||||
let look_ahead = LookAheadSelection::build_from_selection(&op.item.selection_set[0],
|
||||
&vars,
|
||||
&fragments,
|
||||
);
|
||||
&fragments);
|
||||
let expected = LookAheadSelection {
|
||||
name: "hero",
|
||||
alias: None,
|
||||
arguments: Vec::new(),
|
||||
childs: vec![
|
||||
ChildSelection {
|
||||
childs: vec![ChildSelection {
|
||||
inner: LookAheadSelection {
|
||||
name: "id",
|
||||
alias: None,
|
||||
|
@ -732,8 +696,7 @@ query Hero {
|
|||
childs: Vec::new(),
|
||||
},
|
||||
applies_for: Applies::All,
|
||||
},
|
||||
],
|
||||
}],
|
||||
};
|
||||
assert_eq!(look_ahead, expected);
|
||||
} else {
|
||||
|
@ -743,8 +706,7 @@ query Hero {
|
|||
|
||||
#[test]
|
||||
fn check_query_with_inline_fragments() {
|
||||
let docs = ::parse_document_source(
|
||||
"
|
||||
let docs = ::parse_document_source("
|
||||
query Hero {
|
||||
hero {
|
||||
name
|
||||
|
@ -755,23 +717,20 @@ query Hero {
|
|||
height
|
||||
}
|
||||
}
|
||||
}",
|
||||
).unwrap();
|
||||
}")
|
||||
.unwrap();
|
||||
let fragments = extract_fragments(&docs);
|
||||
|
||||
if let ::ast::Definition::Operation(ref op) = docs[0] {
|
||||
let vars = Variables::default();
|
||||
let look_ahead = LookAheadSelection::build_from_selection(
|
||||
&op.item.selection_set[0],
|
||||
let look_ahead = LookAheadSelection::build_from_selection(&op.item.selection_set[0],
|
||||
&vars,
|
||||
&fragments,
|
||||
);
|
||||
&fragments);
|
||||
let expected = LookAheadSelection {
|
||||
name: "hero",
|
||||
alias: None,
|
||||
arguments: Vec::new(),
|
||||
childs: vec![
|
||||
ChildSelection {
|
||||
childs: vec![ChildSelection {
|
||||
inner: LookAheadSelection {
|
||||
name: "name",
|
||||
alias: None,
|
||||
|
@ -797,8 +756,7 @@ query Hero {
|
|||
childs: Vec::new(),
|
||||
},
|
||||
applies_for: Applies::OnlyType("Human"),
|
||||
},
|
||||
],
|
||||
}],
|
||||
};
|
||||
assert_eq!(look_ahead, expected);
|
||||
} else {
|
||||
|
@ -808,8 +766,7 @@ query Hero {
|
|||
|
||||
#[test]
|
||||
fn check_complex_query() {
|
||||
let docs = ::parse_document_source(
|
||||
"
|
||||
let docs = ::parse_document_source("
|
||||
query HeroNameAndFriends($id: Integer!, $withFriends: Boolean! = true) {
|
||||
hero(id: $id) {
|
||||
id
|
||||
|
@ -827,8 +784,8 @@ fragment comparisonFields on Character {
|
|||
appearsIn
|
||||
... on Droid { primaryFunction }
|
||||
... on Human { height }
|
||||
}",
|
||||
).unwrap();
|
||||
}")
|
||||
.unwrap();
|
||||
let fragments = extract_fragments(&docs);
|
||||
|
||||
if let ::ast::Definition::Operation(ref op) = docs[0] {
|
||||
|
@ -836,22 +793,17 @@ fragment comparisonFields on Character {
|
|||
vars.insert("id".into(), InputValue::Int(42));
|
||||
// This will normally be there
|
||||
vars.insert("withFriends".into(), InputValue::Boolean(true));
|
||||
let look_ahead = LookAheadSelection::build_from_selection(
|
||||
&op.item.selection_set[0],
|
||||
let look_ahead = LookAheadSelection::build_from_selection(&op.item.selection_set[0],
|
||||
&vars,
|
||||
&fragments,
|
||||
);
|
||||
&fragments);
|
||||
let expected = LookAheadSelection {
|
||||
name: "hero",
|
||||
alias: None,
|
||||
arguments: vec![
|
||||
LookAheadArgument {
|
||||
arguments: vec![LookAheadArgument {
|
||||
name: "id",
|
||||
value: LookAheadValue::Int(42),
|
||||
},
|
||||
],
|
||||
childs: vec![
|
||||
ChildSelection {
|
||||
}],
|
||||
childs: vec![ChildSelection {
|
||||
inner: LookAheadSelection {
|
||||
name: "id",
|
||||
alias: None,
|
||||
|
@ -910,8 +862,7 @@ fragment comparisonFields on Character {
|
|||
name: "friends",
|
||||
alias: None,
|
||||
arguments: Vec::new(),
|
||||
childs: vec![
|
||||
ChildSelection {
|
||||
childs: vec![ChildSelection {
|
||||
inner: LookAheadSelection {
|
||||
name: "__typename",
|
||||
alias: None,
|
||||
|
@ -955,12 +906,10 @@ fragment comparisonFields on Character {
|
|||
childs: Vec::new(),
|
||||
},
|
||||
applies_for: Applies::OnlyType("Human"),
|
||||
},
|
||||
],
|
||||
}],
|
||||
},
|
||||
applies_for: Applies::All,
|
||||
},
|
||||
],
|
||||
}],
|
||||
};
|
||||
assert_eq!(look_ahead, expected);
|
||||
} else {
|
||||
|
@ -970,8 +919,7 @@ fragment comparisonFields on Character {
|
|||
|
||||
#[test]
|
||||
fn check_resolve_concrete_type() {
|
||||
let docs = ::parse_document_source(
|
||||
"
|
||||
let docs = ::parse_document_source("
|
||||
query Hero {
|
||||
hero {
|
||||
name
|
||||
|
@ -982,23 +930,21 @@ query Hero {
|
|||
height
|
||||
}
|
||||
}
|
||||
}",
|
||||
).unwrap();
|
||||
}")
|
||||
.unwrap();
|
||||
let fragments = extract_fragments(&docs);
|
||||
|
||||
if let ::ast::Definition::Operation(ref op) = docs[0] {
|
||||
let vars = Variables::default();
|
||||
let look_ahead = LookAheadSelection::build_from_selection(
|
||||
&op.item.selection_set[0],
|
||||
let look_ahead = LookAheadSelection::build_from_selection(&op.item.selection_set[0],
|
||||
&vars,
|
||||
&fragments,
|
||||
).for_explicit_type("Human");
|
||||
&fragments)
|
||||
.for_explicit_type("Human");
|
||||
let expected = ConcreteLookAheadSelection {
|
||||
name: "hero",
|
||||
alias: None,
|
||||
arguments: Vec::new(),
|
||||
childs: vec![
|
||||
ConcreteLookAheadSelection {
|
||||
childs: vec![ConcreteLookAheadSelection {
|
||||
name: "name",
|
||||
alias: None,
|
||||
arguments: Vec::new(),
|
||||
|
@ -1009,8 +955,7 @@ query Hero {
|
|||
alias: None,
|
||||
arguments: Vec::new(),
|
||||
childs: Vec::new(),
|
||||
},
|
||||
],
|
||||
}],
|
||||
};
|
||||
assert_eq!(look_ahead, expected);
|
||||
} else {
|
||||
|
@ -1020,12 +965,11 @@ query Hero {
|
|||
|
||||
#[test]
|
||||
fn check_select_child() {
|
||||
let lookahead = LookAheadSelection{
|
||||
let lookahead = LookAheadSelection {
|
||||
name: "hero",
|
||||
alias: None,
|
||||
arguments: Vec::new(),
|
||||
childs: vec![
|
||||
ChildSelection{
|
||||
childs: vec![ChildSelection {
|
||||
inner: LookAheadSelection {
|
||||
name: "id",
|
||||
alias: None,
|
||||
|
@ -1039,15 +983,14 @@ query Hero {
|
|||
name: "friends",
|
||||
alias: None,
|
||||
arguments: Vec::new(),
|
||||
childs: vec![
|
||||
ChildSelection {
|
||||
childs: vec![ChildSelection {
|
||||
inner: LookAheadSelection {
|
||||
name: "id",
|
||||
alias: None,
|
||||
arguments: Vec::new(),
|
||||
childs: Vec::new(),
|
||||
},
|
||||
applies_for: Applies::All
|
||||
applies_for: Applies::All,
|
||||
},
|
||||
ChildSelection {
|
||||
inner: LookAheadSelection {
|
||||
|
@ -1056,21 +999,25 @@ query Hero {
|
|||
arguments: Vec::new(),
|
||||
childs: Vec::new(),
|
||||
},
|
||||
applies_for: Applies::All
|
||||
}
|
||||
],
|
||||
applies_for: Applies::All,
|
||||
}],
|
||||
},
|
||||
applies_for: Applies::All
|
||||
}
|
||||
]
|
||||
applies_for: Applies::All,
|
||||
}],
|
||||
};
|
||||
let concret_query = lookahead.for_explicit_type("does not matter");
|
||||
|
||||
let id = lookahead.select_child("id");
|
||||
let concrete_id = concret_query.select_child("id");
|
||||
let expected = LookAheadSelection{name: "id", alias: None, arguments: Vec::new(), childs: Vec::new()};
|
||||
let expected = LookAheadSelection {
|
||||
name: "id",
|
||||
alias: None,
|
||||
arguments: Vec::new(),
|
||||
childs: Vec::new(),
|
||||
};
|
||||
assert_eq!(id, Some(&expected));
|
||||
assert_eq!(concrete_id, Some(&expected.for_explicit_type("does not matter")));
|
||||
assert_eq!(concrete_id,
|
||||
Some(&expected.for_explicit_type("does not matter")));
|
||||
|
||||
let friends = lookahead.select_child("friends");
|
||||
let concrete_friends = concret_query.select_child("friends");
|
||||
|
@ -1078,15 +1025,14 @@ query Hero {
|
|||
name: "friends",
|
||||
alias: None,
|
||||
arguments: Vec::new(),
|
||||
childs: vec![
|
||||
ChildSelection {
|
||||
childs: vec![ChildSelection {
|
||||
inner: LookAheadSelection {
|
||||
name: "id",
|
||||
alias: None,
|
||||
arguments: Vec::new(),
|
||||
childs: Vec::new(),
|
||||
},
|
||||
applies_for: Applies::All
|
||||
applies_for: Applies::All,
|
||||
},
|
||||
ChildSelection {
|
||||
inner: LookAheadSelection {
|
||||
|
@ -1095,12 +1041,12 @@ query Hero {
|
|||
arguments: Vec::new(),
|
||||
childs: Vec::new(),
|
||||
},
|
||||
applies_for: Applies::All
|
||||
}
|
||||
],
|
||||
applies_for: Applies::All,
|
||||
}],
|
||||
};
|
||||
assert_eq!(friends, Some(&expected));
|
||||
assert_eq!(concrete_friends, Some(&expected.for_explicit_type("does not matter")));
|
||||
assert_eq!(concrete_friends,
|
||||
Some(&expected.for_explicit_type("does not matter")));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,6 +11,10 @@ use ast::{Definition, Document, Fragment, FromInputValue, InputValue, OperationT
|
|||
use parser::SourcePosition;
|
||||
use value::Value;
|
||||
use GraphQLError;
|
||||
use ast::{Definition, Document, Fragment, FromInputValue, InputValue, OperationType,
|
||||
Selection, ToInputValue, Type};
|
||||
use value::Value;
|
||||
use parser::SourcePosition;
|
||||
|
||||
use schema::meta::{Argument, EnumMeta, EnumValue, Field, InputObjectMeta, InterfaceMeta, ListMeta,
|
||||
MetaType, NullableMeta, ObjectMeta, PlaceholderMeta, ScalarMeta, UnionMeta};
|
||||
|
@ -50,6 +54,7 @@ where
|
|||
fragments: &'a HashMap<&'a str, &'a Fragment<'a>>,
|
||||
variables: &'a Variables,
|
||||
current_selection_set: Option<&'a [Selection<'a>]>,
|
||||
parent_selection_set: Option<&'a [Selection<'a>]>,
|
||||
current_type: TypeType<'a>,
|
||||
schema: &'a SchemaType<'a>,
|
||||
context: &'a CtxT,
|
||||
|
@ -319,6 +324,7 @@ impl<'a, CtxT> Executor<'a, CtxT> {
|
|||
fragments: self.fragments,
|
||||
variables: self.variables,
|
||||
current_selection_set: self.current_selection_set,
|
||||
parent_selection_set: self.parent_selection_set,
|
||||
current_type: self.current_type.clone(),
|
||||
schema: self.schema,
|
||||
context: ctx,
|
||||
|
@ -340,6 +346,7 @@ impl<'a, CtxT> Executor<'a, CtxT> {
|
|||
fragments: self.fragments,
|
||||
variables: self.variables,
|
||||
current_selection_set: selection_set,
|
||||
parent_selection_set: self.current_selection_set,
|
||||
current_type: self.schema.make_type(
|
||||
&self.current_type
|
||||
.innermost_concrete()
|
||||
|
@ -351,7 +358,7 @@ impl<'a, CtxT> Executor<'a, CtxT> {
|
|||
context: self.context,
|
||||
errors: self.errors,
|
||||
field_path: FieldPath::Field(field_alias, location, &self.field_path),
|
||||
type_name: self.type_name
|
||||
type_name: self.type_name,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -365,6 +372,7 @@ impl<'a, CtxT> Executor<'a, CtxT> {
|
|||
fragments: self.fragments,
|
||||
variables: self.variables,
|
||||
current_selection_set: selection_set,
|
||||
parent_selection_set: self.current_selection_set,
|
||||
current_type: match type_name {
|
||||
Some(type_name) => self.schema.type_by_name(type_name).expect("Type not found"),
|
||||
None => self.current_type.clone(),
|
||||
|
@ -430,9 +438,12 @@ impl<'a, CtxT> Executor<'a, CtxT> {
|
|||
});
|
||||
}
|
||||
|
||||
pub fn look_ahead(&self) -> LookAheadSelection {
|
||||
pub fn look_ahead(&'a self) -> LookAheadSelection<'a> {
|
||||
self.parent_selection_set.map(|p| {
|
||||
LookAheadSelection::build_from_selection(&p[0], self.variables, self.fragments)
|
||||
}).unwrap_or_else(||{
|
||||
LookAheadSelection{
|
||||
name: self.type_name,
|
||||
name: self.current_type.innermost_concrete().name().unwrap_or(""),
|
||||
alias: None,
|
||||
arguments: Vec::new(),
|
||||
childs: self.current_selection_set.map(|s| s.iter().map(|s| ChildSelection {
|
||||
|
@ -440,6 +451,7 @@ impl<'a, CtxT> Executor<'a, CtxT> {
|
|||
applies_for: Applies::All
|
||||
}).collect()).unwrap_or_else(Vec::new)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -568,6 +580,7 @@ where
|
|||
.collect(),
|
||||
variables: final_vars,
|
||||
current_selection_set: Some(&op.item.selection_set[..]),
|
||||
parent_selection_set: None,
|
||||
current_type: root_type,
|
||||
schema: &root_node.schema,
|
||||
context: context,
|
||||
|
|
Loading…
Add table
Reference in a new issue