2020-10-15 06:38:10 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2020-10-11 12:24:31 +00:00
|
|
|
use crate::parser::types::{Field, FragmentDefinition, Selection, SelectionSet};
|
2021-07-01 02:36:25 +00:00
|
|
|
use crate::{Name, Positioned, SelectionField};
|
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-22 18:59:48 +00:00
|
|
|
fragments: &'a HashMap<Name, Positioned<FragmentDefinition>>,
|
2021-07-15 01:16:55 +00:00
|
|
|
fields: Vec<&'a Field>,
|
2020-05-14 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Lookahead<'a> {
|
2020-09-22 18:59:48 +00:00
|
|
|
pub(crate) fn new(
|
|
|
|
fragments: &'a HashMap<Name, Positioned<FragmentDefinition>>,
|
|
|
|
field: &'a Field,
|
|
|
|
) -> Self {
|
2020-09-13 07:02:14 +00:00
|
|
|
Self {
|
2020-09-22 18:59:48 +00:00
|
|
|
fragments,
|
2021-07-15 01:16:55 +00:00
|
|
|
fields: vec![field],
|
2020-09-13 07:02:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 01:16:55 +00:00
|
|
|
/// Get the field of the selection set with the specified name. This will ignore
|
2020-09-13 07:02:14 +00:00
|
|
|
/// 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 {
|
2021-07-15 01:16:55 +00:00
|
|
|
let mut fields = Vec::new();
|
|
|
|
for field in &self.fields {
|
|
|
|
filter(&mut fields, self.fragments, &field.selection_set.node, name)
|
|
|
|
}
|
|
|
|
|
2020-09-06 05:38:31 +00:00
|
|
|
Self {
|
2020-09-22 18:59:48 +00:00
|
|
|
fragments: self.fragments,
|
2021-07-15 01:16:55 +00:00
|
|
|
fields,
|
2020-05-14 14:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if field exists otherwise return false.
|
|
|
|
#[inline]
|
|
|
|
pub fn exists(&self) -> bool {
|
2021-07-15 01:16:55 +00:00
|
|
|
!self.fields.is_empty()
|
2020-05-14 14:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-01 02:36:25 +00:00
|
|
|
impl<'a> From<SelectionField<'a>> for Lookahead<'a> {
|
|
|
|
fn from(selection_field: SelectionField<'a>) -> Self {
|
|
|
|
Lookahead {
|
|
|
|
fragments: selection_field.fragments,
|
2021-07-15 01:16:55 +00:00
|
|
|
fields: vec![selection_field.field],
|
2021-07-01 02:36:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 03:51:44 +00:00
|
|
|
/// Convert a slice of `SelectionField`s to a `Lookahead`.
|
|
|
|
///
|
|
|
|
/// This assumes that the `SelectionField`s are all from the same query.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
/// Panics if 0 `SelectionField`s are provided.
|
|
|
|
impl<'a> From<&[SelectionField<'a>]> for Lookahead<'a> {
|
|
|
|
fn from(selection_fields: &[SelectionField<'a>]) -> Self {
|
|
|
|
Lookahead {
|
|
|
|
fragments: selection_fields[0].fragments,
|
|
|
|
fields: selection_fields
|
|
|
|
.iter()
|
|
|
|
.map(|selection_field| selection_field.field)
|
|
|
|
.collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 01:16:55 +00:00
|
|
|
fn filter<'a>(
|
|
|
|
fields: &mut Vec<&'a Field>,
|
2020-09-22 18:59:48 +00:00
|
|
|
fragments: &'a HashMap<Name, Positioned<FragmentDefinition>>,
|
2020-05-14 14:13:28 +00:00
|
|
|
selection_set: &'a SelectionSet,
|
|
|
|
name: &str,
|
2021-07-15 01:16:55 +00:00
|
|
|
) {
|
|
|
|
for item in &selection_set.items {
|
|
|
|
// doing this imperatively is a bit nasty, but using iterators would
|
|
|
|
// require a boxed return type (I believe) as its recusive
|
|
|
|
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 {
|
2021-07-15 01:16:55 +00:00
|
|
|
fields.push(&field.node)
|
2020-05-14 14:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-13 07:02:14 +00:00
|
|
|
Selection::InlineFragment(fragment) => {
|
2021-07-15 01:16:55 +00:00
|
|
|
filter(fields, fragments, &fragment.node.selection_set.node, name)
|
|
|
|
}
|
|
|
|
Selection::FragmentSpread(spread) => {
|
|
|
|
if let Some(fragment) = fragments.get(&spread.node.fragment_name.node) {
|
|
|
|
filter(fields, fragments, &fragment.node.selection_set.node, name)
|
|
|
|
}
|
2020-05-14 14:13:28 +00:00
|
|
|
}
|
2021-07-15 01:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-14 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::*;
|
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-05-14 14:13:28 +00:00
|
|
|
async fn test_look_ahead() {
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(SimpleObject)]
|
2020-09-13 03:41:15 +00:00
|
|
|
#[graphql(internal)]
|
2020-05-14 14:13:28 +00:00
|
|
|
struct Detail {
|
|
|
|
c: i32,
|
|
|
|
d: i32,
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(SimpleObject)]
|
2020-09-13 03:41:15 +00:00
|
|
|
#[graphql(internal)]
|
2020-05-14 14:13:28 +00:00
|
|
|
struct MyObj {
|
|
|
|
a: i32,
|
|
|
|
b: i32,
|
|
|
|
detail: Detail,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Query;
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object(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);
|
2021-07-15 01:16:55 +00:00
|
|
|
} else if ctx.look_ahead().field("detail").field("c").exists()
|
|
|
|
&& ctx.look_ahead().field("detail").field("d").exists()
|
|
|
|
{
|
2020-05-14 14:13:28 +00:00
|
|
|
// This is a query like `obj { detail { c } }`
|
|
|
|
assert_eq!(n, 2);
|
2021-07-15 01:16:55 +00:00
|
|
|
} else if ctx.look_ahead().field("detail").field("c").exists() {
|
|
|
|
// This is a query like `obj { detail { c } }`
|
|
|
|
assert_eq!(n, 3);
|
2020-05-14 14:13:28 +00:00
|
|
|
} else {
|
|
|
|
// This query doesn't have `a`
|
2021-07-15 01:16:55 +00:00
|
|
|
assert_eq!(n, 4);
|
2020-05-14 14:13:28 +00:00
|
|
|
}
|
|
|
|
MyObj {
|
|
|
|
a: 0,
|
|
|
|
b: 0,
|
|
|
|
detail: Detail { c: 0, d: 0 },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
|
|
|
|
|
2020-11-30 08:17:32 +00:00
|
|
|
assert!(schema
|
2020-05-14 14:13:28 +00:00
|
|
|
.execute(
|
|
|
|
r#"{
|
|
|
|
obj(n: 1) {
|
|
|
|
a
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
.await
|
2020-11-30 08:17:32 +00:00
|
|
|
.is_ok());
|
2020-05-14 14:13:28 +00:00
|
|
|
|
2020-11-30 08:17:32 +00:00
|
|
|
assert!(schema
|
2020-05-14 14:13:28 +00:00
|
|
|
.execute(
|
|
|
|
r#"{
|
|
|
|
obj(n: 1) {
|
|
|
|
k:a
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
.await
|
2020-11-30 08:17:32 +00:00
|
|
|
.is_ok());
|
2020-05-14 14:13:28 +00:00
|
|
|
|
2021-07-15 01:16:55 +00:00
|
|
|
assert!(schema
|
|
|
|
.execute(
|
|
|
|
r#"{
|
|
|
|
obj(n: 3) {
|
|
|
|
detail {
|
|
|
|
c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.is_ok());
|
|
|
|
|
2020-11-30 08:17:32 +00:00
|
|
|
assert!(schema
|
2020-05-14 14:13:28 +00:00
|
|
|
.execute(
|
|
|
|
r#"{
|
|
|
|
obj(n: 2) {
|
2021-07-15 01:16:55 +00:00
|
|
|
detail {
|
|
|
|
d
|
|
|
|
}
|
|
|
|
|
2020-05-14 14:13:28 +00:00
|
|
|
detail {
|
|
|
|
c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
.await
|
2020-11-30 08:17:32 +00:00
|
|
|
.is_ok());
|
2020-05-14 14:13:28 +00:00
|
|
|
|
2020-11-30 08:17:32 +00:00
|
|
|
assert!(schema
|
2020-05-14 14:13:28 +00:00
|
|
|
.execute(
|
|
|
|
r#"{
|
2021-07-15 01:16:55 +00:00
|
|
|
obj(n: 4) {
|
2020-05-14 14:13:28 +00:00
|
|
|
b
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
.await
|
2020-11-30 08:17:32 +00:00
|
|
|
.is_ok());
|
2020-05-14 14:13:28 +00:00
|
|
|
|
2020-11-30 08:17:32 +00:00
|
|
|
assert!(schema
|
2020-05-14 14:13:28 +00:00
|
|
|
.execute(
|
|
|
|
r#"{
|
|
|
|
obj(n: 1) {
|
|
|
|
... {
|
|
|
|
a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
.await
|
2020-11-30 08:17:32 +00:00
|
|
|
.is_ok());
|
2020-05-14 14:13:28 +00:00
|
|
|
|
2021-07-15 01:16:55 +00:00
|
|
|
assert!(schema
|
|
|
|
.execute(
|
|
|
|
r#"{
|
|
|
|
obj(n: 3) {
|
|
|
|
... {
|
|
|
|
detail {
|
|
|
|
c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.is_ok());
|
|
|
|
|
2020-11-30 08:17:32 +00:00
|
|
|
assert!(schema
|
2020-05-14 14:13:28 +00:00
|
|
|
.execute(
|
|
|
|
r#"{
|
|
|
|
obj(n: 2) {
|
|
|
|
... {
|
2021-07-15 01:16:55 +00:00
|
|
|
detail {
|
|
|
|
d
|
|
|
|
}
|
|
|
|
|
2020-05-14 14:13:28 +00:00
|
|
|
detail {
|
|
|
|
c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
.await
|
2020-11-30 08:17:32 +00:00
|
|
|
.is_ok());
|
2020-05-14 14:13:28 +00:00
|
|
|
|
2020-11-30 08:17:32 +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-11-30 08:17:32 +00:00
|
|
|
.is_ok());
|
2020-05-14 14:13:28 +00:00
|
|
|
|
2021-07-15 01:16:55 +00:00
|
|
|
assert!(schema
|
|
|
|
.execute(
|
|
|
|
r#"{
|
|
|
|
obj(n: 3) {
|
|
|
|
... A
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fragment A on MyObj {
|
|
|
|
detail {
|
|
|
|
c
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.is_ok());
|
|
|
|
|
2020-11-30 08:17:32 +00:00
|
|
|
assert!(schema
|
2020-05-14 14:13:28 +00:00
|
|
|
.execute(
|
|
|
|
r#"{
|
|
|
|
obj(n: 2) {
|
|
|
|
... A
|
2021-07-15 01:16:55 +00:00
|
|
|
... B
|
2020-05-14 14:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fragment A on MyObj {
|
2021-07-15 01:16:55 +00:00
|
|
|
detail {
|
|
|
|
d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fragment B on MyObj {
|
2020-05-14 14:13:28 +00:00
|
|
|
detail {
|
|
|
|
c
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
.await
|
2020-11-30 08:17:32 +00:00
|
|
|
.is_ok());
|
2020-05-14 14:13:28 +00:00
|
|
|
}
|
|
|
|
}
|