2020-09-08 08:21:27 +00:00
|
|
|
use crate::parser::types::{ExecutableDocumentData, Field, Selection, SelectionSet};
|
2020-05-14 14:13:28 +00:00
|
|
|
|
2020-09-13 07:02:14 +00:00
|
|
|
/// A selection performed by a query.
|
2020-05-14 14:13:28 +00:00
|
|
|
pub struct Lookahead<'a> {
|
2020-09-13 07:02:14 +00:00
|
|
|
document: &'a ExecutableDocumentData,
|
|
|
|
field: Option<&'a Field>,
|
2020-05-14 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Lookahead<'a> {
|
2020-09-13 07:02:14 +00:00
|
|
|
pub(crate) fn new(document: &'a ExecutableDocumentData, field: &'a Field) -> Self {
|
|
|
|
Self {
|
|
|
|
document,
|
|
|
|
field: Some(field),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the first subfield of the selection set with the specified name. This will ignore
|
|
|
|
/// aliases.
|
|
|
|
///
|
|
|
|
/// For example, calling `.field("a")` on `{ a { b } }` will return a lookahead that
|
|
|
|
/// represents `{ b }`.
|
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> {
|
2020-09-13 07:02:14 +00:00
|
|
|
selection_set
|
|
|
|
.items
|
|
|
|
.iter()
|
|
|
|
.find_map(|item| match &item.node {
|
2020-05-14 14:13:28 +00:00
|
|
|
Selection::Field(field) => {
|
2020-09-06 05:38:31 +00:00
|
|
|
if field.node.name.node == name {
|
2020-09-13 07:02:14 +00:00
|
|
|
Some(&field.node)
|
|
|
|
} else {
|
|
|
|
None
|
2020-05-14 14:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-13 07:02:14 +00:00
|
|
|
Selection::InlineFragment(fragment) => {
|
|
|
|
find(document, &fragment.node.selection_set.node, name)
|
2020-05-14 14:13:28 +00:00
|
|
|
}
|
2020-09-13 07:02:14 +00:00
|
|
|
Selection::FragmentSpread(spread) => document
|
|
|
|
.fragments
|
|
|
|
.get(&spread.node.fragment_name.node)
|
|
|
|
.and_then(|fragment| find(document, &fragment.node.selection_set.node, name)),
|
|
|
|
})
|
2020-05-14 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::*;
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
async fn test_look_ahead() {
|
2020-09-13 03:41:15 +00:00
|
|
|
#[derive(GQLSimpleObject)]
|
|
|
|
#[graphql(internal)]
|
2020-05-14 14:13:28 +00:00
|
|
|
struct Detail {
|
|
|
|
c: i32,
|
|
|
|
d: i32,
|
|
|
|
}
|
|
|
|
|
2020-09-13 03:41:15 +00:00
|
|
|
#[derive(GQLSimpleObject)]
|
|
|
|
#[graphql(internal)]
|
2020-05-14 14:13:28 +00:00
|
|
|
struct MyObj {
|
|
|
|
a: i32,
|
|
|
|
b: i32,
|
|
|
|
detail: Detail,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Query;
|
|
|
|
|
2020-09-13 03:41:15 +00:00
|
|
|
#[GQLObject(internal)]
|
2020-05-14 14:13:28 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|