|
| 1 | +use futures::{Stream, StreamExt, TryFutureExt}; |
| 2 | +use juniper::{ |
| 3 | + executor::{execute_validated_query_async, get_operation, resolve_validated_subscription}, |
| 4 | + graphql_object, graphql_subscription, |
| 5 | + parser::parse_document_source, |
| 6 | + validation::{validate_input_values, visit_all_rules, ValidatorContext}, |
| 7 | + EmptyMutation, FieldError, OperationType, RootNode, Variables, |
| 8 | +}; |
| 9 | +use std::pin::Pin; |
| 10 | + |
| 11 | +pub struct Context; |
| 12 | + |
| 13 | +impl juniper::Context for Context {} |
| 14 | + |
| 15 | +pub type UserStream = Pin<Box<dyn Stream<Item = Result<User, FieldError>> + Send>>; |
| 16 | + |
| 17 | +pub struct Query; |
| 18 | + |
| 19 | +#[graphql_object(context = Context)] |
| 20 | +impl Query { |
| 21 | + fn users() -> Vec<User> { |
| 22 | + vec![User] |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +pub struct Subscription; |
| 27 | + |
| 28 | +#[graphql_subscription(context = Context)] |
| 29 | +impl Subscription { |
| 30 | + async fn users() -> UserStream { |
| 31 | + Box::pin(futures::stream::iter(vec![Ok(User)])) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Clone)] |
| 36 | +pub struct User; |
| 37 | + |
| 38 | +#[graphql_object(context = Context)] |
| 39 | +impl User { |
| 40 | + fn id() -> i32 { |
| 41 | + 1 |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +type Schema = RootNode<'static, Query, EmptyMutation<Context>, Subscription>; |
| 46 | + |
| 47 | +#[tokio::test] |
| 48 | +async fn query_document_can_be_pre_parsed() { |
| 49 | + let root_node = &Schema::new(Query, EmptyMutation::<Context>::new(), Subscription); |
| 50 | + |
| 51 | + let document_source = r#"query { users { id } }"#; |
| 52 | + let document = parse_document_source(document_source, &root_node.schema).unwrap(); |
| 53 | + |
| 54 | + { |
| 55 | + let mut ctx = ValidatorContext::new(&root_node.schema, &document); |
| 56 | + visit_all_rules(&mut ctx, &document); |
| 57 | + let errors = ctx.into_errors(); |
| 58 | + assert!(errors.is_empty()); |
| 59 | + } |
| 60 | + |
| 61 | + let operation = get_operation(&document, None).unwrap(); |
| 62 | + assert!(operation.item.operation_type == OperationType::Query); |
| 63 | + |
| 64 | + let errors = validate_input_values(&juniper::Variables::new(), operation, &root_node.schema); |
| 65 | + assert!(errors.is_empty()); |
| 66 | + |
| 67 | + let (_, errors) = execute_validated_query_async( |
| 68 | + &document, |
| 69 | + operation, |
| 70 | + root_node, |
| 71 | + &Variables::new(), |
| 72 | + &Context {}, |
| 73 | + ) |
| 74 | + .await |
| 75 | + .unwrap(); |
| 76 | + |
| 77 | + assert!(errors.len() == 0); |
| 78 | +} |
| 79 | + |
| 80 | +#[tokio::test] |
| 81 | +async fn subscription_document_can_be_pre_parsed() { |
| 82 | + let root_node = &Schema::new(Query, EmptyMutation::<Context>::new(), Subscription); |
| 83 | + |
| 84 | + let document_source = r#"subscription { users { id } }"#; |
| 85 | + let document = parse_document_source(document_source, &root_node.schema).unwrap(); |
| 86 | + |
| 87 | + let operation = get_operation(&document, None).unwrap(); |
| 88 | + assert!(operation.item.operation_type == OperationType::Subscription); |
| 89 | + |
| 90 | + let mut stream = resolve_validated_subscription( |
| 91 | + &document, |
| 92 | + &operation, |
| 93 | + &root_node, |
| 94 | + &Variables::new(), |
| 95 | + &Context {}, |
| 96 | + ) |
| 97 | + .map_ok(|(stream, errors)| juniper_subscriptions::Connection::from_stream(stream, errors)) |
| 98 | + .await |
| 99 | + .unwrap(); |
| 100 | + |
| 101 | + let _ = stream.next().await.unwrap(); |
| 102 | +} |
0 commit comments