2020-09-08 08:21:27 +00:00
|
|
|
use crate::parser::types::{ExecutableDocumentData, Field, Selection, SelectionSet};
|
2020-05-14 14:13:28 +00:00
|
|
|
|
|
|
|
/// A selection performed by a query
|
|
|
|
pub struct Lookahead<'a> {
|
2020-09-08 08:21:27 +00:00
|
|
|
pub(crate) document: &'a ExecutableDocumentData,
|
2020-05-14 14:13:28 +00:00
|
|
|
pub(crate) field: Option<&'a Field>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Lookahead<'a> {
|
|
|
|
/// Check if the specified field exists in the current selection.
|
2020-09-06 05:38:31 +00:00
|
|
|
pub fn field(&self, name: &str) -> Self {
|
|
|
|
Self {
|
2020-05-14 14:13:28 +00:00
|
|
|
document: self.document,
|
|
|
|
field: self
|
|
|
|
.field
|
|
|
|
.and_then(|field| find(self.document, &field.selection_set.node, name)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if field exists otherwise return false.
|
|
|
|
#[inline]
|
|
|
|
pub fn exists(&self) -> bool {
|
|
|
|
self.field.is_some()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find<'a>(
|
2020-09-08 08:21:27 +00:00
|
|
|
document: &'a ExecutableDocumentData,
|
2020-05-14 14:13:28 +00:00
|
|
|
selection_set: &'a SelectionSet,
|
|
|
|
name: &str,
|
|
|
|
) -> Option<&'a Field> {
|
|
|
|
for item in &selection_set.items {
|
|
|
|
match &item.node {
|
|
|
|
Selection::Field(field) => {
|
2020-09-06 05:38:31 +00:00
|
|
|
if field.node.name.node == name {
|
2020-05-14 14:13:28 +00:00
|
|
|
return Some(&field.node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Selection::InlineFragment(inline_fragment) => {
|
2020-09-06 06:16:36 +00:00
|
|
|
if let Some(field) = find(document, &inline_fragment.node.selection_set.node, name)
|
|
|
|
{
|
2020-05-14 14:13:28 +00:00
|
|
|
return Some(field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Selection::FragmentSpread(fragment_spread) => {
|
2020-05-16 13:14:26 +00:00
|
|
|
if let Some(fragment) = document
|
2020-09-06 05:38:31 +00:00
|
|
|
.fragments
|
|
|
|
.get(&fragment_spread.node.fragment_name.node)
|
2020-05-14 14:13:28 +00:00
|
|
|
{
|
2020-09-06 05:38:31 +00:00
|
|
|
if let Some(field) = find(document, &fragment.node.selection_set.node, name) {
|
2020-05-14 14:13:28 +00:00
|
|
|
return Some(field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::*;
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
async fn test_look_ahead() {
|
|
|
|
#[SimpleObject(internal)]
|
|
|
|
struct Detail {
|
|
|
|
c: i32,
|
|
|
|
d: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[SimpleObject(internal)]
|
|
|
|
struct MyObj {
|
|
|
|
a: i32,
|
|
|
|
b: i32,
|
|
|
|
detail: Detail,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Query;
|
|
|
|
|
|
|
|
#[Object(internal)]
|
|
|
|
impl Query {
|
|
|
|
async fn obj(&self, ctx: &Context<'_>, n: i32) -> MyObj {
|
|
|
|
if ctx.look_ahead().field("a").exists() {
|
|
|
|
// This is a query like `obj { a }`
|
|
|
|
assert_eq!(n, 1);
|
|
|
|
} else if ctx.look_ahead().field("detail").field("c").exists() {
|
|
|
|
// This is a query like `obj { detail { c } }`
|
|
|
|
assert_eq!(n, 2);
|
|
|
|
} else {
|
|
|
|
// This query doesn't have `a`
|
|
|
|
assert_eq!(n, 3);
|
|
|
|
}
|
|
|
|
MyObj {
|
|
|
|
a: 0,
|
|
|
|
b: 0,
|
|
|
|
detail: Detail { c: 0, d: 0 },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
|
|
|
|
|
2020-09-10 11:35:48 +00:00
|
|
|
assert!(!schema
|
2020-05-14 14:13:28 +00:00
|
|
|
.execute(
|
|
|
|
r#"{
|
|
|
|
obj(n: 1) {
|
|
|
|
a
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
.await
|
2020-09-10 11:35:48 +00:00
|
|
|
.is_err());
|
2020-05-14 14:13:28 +00:00
|
|
|
|
2020-09-10 11:35:48 +00:00
|
|
|
assert!(!schema
|
2020-05-14 14:13:28 +00:00
|
|
|
.execute(
|
|
|
|
r#"{
|
|
|
|
obj(n: 1) {
|
|
|
|
k:a
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
.await
|
2020-09-10 11:35:48 +00:00
|
|
|
.is_err());
|
2020-05-14 14:13:28 +00:00
|
|
|
|
2020-09-10 11:35:48 +00:00
|
|
|
assert!(!schema
|
2020-05-14 14:13:28 +00:00
|
|
|
.execute(
|
|
|
|
r#"{
|
|
|
|
obj(n: 2) {
|
|
|
|
detail {
|
|
|
|
c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
.await
|
2020-09-10 11:35:48 +00:00
|
|
|
.is_err());
|
2020-05-14 14:13:28 +00:00
|
|
|
|
2020-09-10 11:35:48 +00:00
|
|
|
assert!(!schema
|
2020-05-14 14:13:28 +00:00
|
|
|
.execute(
|
|
|
|
r#"{
|
|
|
|
obj(n: 3) {
|
|
|
|
b
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
.await
|
2020-09-10 11:35:48 +00:00
|
|
|
.is_err());
|
2020-05-14 14:13:28 +00:00
|
|
|
|
2020-09-10 11:35:48 +00:00
|
|
|
assert!(!schema
|
2020-05-14 14:13:28 +00:00
|
|
|
.execute(
|
|
|
|
r#"{
|
|
|
|
obj(n: 1) {
|
|
|
|
... {
|
|
|
|
a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
.await
|
2020-09-10 11:35:48 +00:00
|
|
|
.is_err());
|
2020-05-14 14:13:28 +00:00
|
|
|
|
2020-09-10 11:35:48 +00:00
|
|
|
assert!(!schema
|
2020-05-14 14:13:28 +00:00
|
|
|
.execute(
|
|
|
|
r#"{
|
|
|
|
obj(n: 2) {
|
|
|
|
... {
|
|
|
|
detail {
|
|
|
|
c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
.await
|
2020-09-10 11:35:48 +00:00
|
|
|
.is_err());
|
2020-05-14 14:13:28 +00:00
|
|
|
|
2020-09-10 11:35:48 +00:00
|
|
|
assert!(!schema
|
2020-05-14 14:13:28 +00:00
|
|
|
.execute(
|
|
|
|
r#"{
|
|
|
|
obj(n: 1) {
|
|
|
|
... A
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fragment A on MyObj {
|
|
|
|
a
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
.await
|
2020-09-10 11:35:48 +00:00
|
|
|
.is_err());
|
2020-05-14 14:13:28 +00:00
|
|
|
|
2020-09-10 11:35:48 +00:00
|
|
|
assert!(!schema
|
2020-05-14 14:13:28 +00:00
|
|
|
.execute(
|
|
|
|
r#"{
|
|
|
|
obj(n: 2) {
|
|
|
|
... A
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fragment A on MyObj {
|
|
|
|
detail {
|
|
|
|
c
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
.await
|
2020-09-10 11:35:48 +00:00
|
|
|
.is_err());
|
2020-05-14 14:13:28 +00:00
|
|
|
}
|
|
|
|
}
|