Add Schema::execute

This commit is contained in:
sunli 2020-04-02 12:53:53 +08:00
parent cb0ca21a3d
commit 506e82895f
13 changed files with 31 additions and 27 deletions

View File

@ -243,7 +243,7 @@ where
.map_err(actix_web::error::ErrorBadRequest)?
};
let mut builder = match gql_request.into_query_builder(schema).await {
let mut builder = match gql_request.into_query_builder(schema) {
Ok(builder) => builder,
Err(err) => return Ok(web::Json(GQLResponse(Err(err))).respond_to(&req).await?),
};
@ -319,7 +319,7 @@ where
let gql_request = web::Json::<GQLRequest>::from_request(&req, &mut payload.0)
.await?
.into_inner();
let mut builder = match gql_request.into_query_builder(schema).await {
let mut builder = match gql_request.into_query_builder(schema) {
Ok(builder) => builder,
Err(err) => return Ok(web::Json(GQLResponse(Err(err))).respond_to(&req).await?),
};

View File

@ -9,8 +9,7 @@ type StarWarsSchema = Schema<starwars::QueryRoot, EmptyMutation, EmptySubscripti
async fn index(s: web::Data<StarWarsSchema>, req: web::Json<GQLRequest>) -> web::Json<GQLResponse> {
web::Json(GQLResponse(
req.into_inner()
.into_query_builder(&s)
futures::future::ready(req.into_inner().into_query_builder(&s))
.and_then(|builder| builder.execute())
.await,
))

View File

@ -76,8 +76,7 @@ async fn index(
req: web::Json<GQLRequest>,
) -> web::Json<GQLResponse> {
web::Json(GQLResponse(
req.into_inner()
.into_query_builder(&s)
futures::future::ready(req.into_inner().into_query_builder(&s))
.and_then(|builder| builder.execute())
.await,
))

View File

@ -11,8 +11,7 @@ type StarWarsSchema = Schema<starwars::QueryRoot, EmptyMutation, EmptySubscripti
async fn index(mut request: Request<StarWarsSchema>) -> Response {
let gql_request: GQLRequest = request.body_json().await.unwrap();
let schema = request.state();
let gql_response = gql_request
.into_query_builder(schema)
let gql_response = futures::future::ready(gql_request.into_query_builder(schema))
.and_then(|builder| builder.execute())
.await;
Response::new(200)

View File

@ -30,7 +30,7 @@ pub struct GQLRequest {
impl GQLRequest {
/// Into query builder, you can set other parameters or execute queries immediately.
pub async fn into_query_builder<Query, Mutation, Subscription>(
pub fn into_query_builder<Query, Mutation, Subscription>(
self,
schema: &Schema<Query, Mutation, Subscription>,
) -> Result<QueryBuilder<Query, Mutation, Subscription>>

View File

@ -220,13 +220,13 @@ pub use types::{EnumItem, EnumType};
/// #[async_std::main]
/// async fn main() {
/// let schema = Schema::new(QueryRoot{ value: 10 }, EmptyMutation, EmptySubscription);
/// let res = schema.query(r#"{
/// let res = schema.execute(r#"{
/// value
/// valueRef
/// valueWithError
/// valueWithArg1: valueWithArg
/// valueWithArg2: valueWithArg(a: 99)
/// }"#).unwrap().execute().await.unwrap().data;
/// }"#).await.unwrap().data;
/// assert_eq!(res, serde_json::json!({
/// "value": 10,
/// "valueRef": 10,
@ -273,7 +273,7 @@ pub use async_graphql_derive::Object;
/// #[async_std::main]
/// async fn main() {
/// let schema = Schema::new(QueryRoot{ value: 10 }, EmptyMutation, EmptySubscription);
/// let res = schema.query("{ value }").unwrap().execute().await.unwrap().data;
/// let res = schema.execute("{ value }").await.unwrap().data;
/// assert_eq!(res, serde_json::json!({
/// "value": 10,
/// }));
@ -330,7 +330,7 @@ pub use async_graphql_derive::SimpleObject;
/// #[async_std::main]
/// async fn main() {
/// let schema = Schema::new(QueryRoot{ value1: MyEnum::A, value2: MyEnum::B }, EmptyMutation, EmptySubscription);
/// let res = schema.query("{ value1 value2 }").unwrap().execute().await.unwrap().data;
/// let res = schema.execute("{ value1 value2 }").await.unwrap().data;
/// assert_eq!(res, serde_json::json!({ "value1": "A", "value2": "b" }));
/// }
/// ```
@ -379,11 +379,11 @@ pub use async_graphql_derive::Enum;
/// #[async_std::main]
/// async fn main() {
/// let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
/// let res = schema.query(r#"
/// let res = schema.execute(r#"
/// {
/// value1: value(input:{a:9, b:3})
/// value2: value(input:{a:9})
/// }"#).unwrap().execute().await.unwrap().data;
/// }"#).await.unwrap().data;
/// assert_eq!(res, serde_json::json!({ "value1": 27, "value2": 90 }));
/// }
/// ```
@ -484,14 +484,14 @@ pub use async_graphql_derive::InputObject;
/// #[async_std::main]
/// async fn main() {
/// let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription).data("hello".to_string()).finish();
/// let res = schema.query(r#"
/// let res = schema.execute(r#"
/// {
/// typeA {
/// valueA
/// valueB
/// valueC(a: 3, b: 2)
/// }
/// }"#).unwrap().execute().await.unwrap().data;
/// }"#).await.unwrap().data;
/// assert_eq!(res, serde_json::json!({
/// "typeA": {
/// "valueA": "hello",

View File

@ -7,12 +7,12 @@ use crate::subscription::{SubscriptionConnectionBuilder, SubscriptionStub, Subsc
use crate::types::QueryRoot;
use crate::validation::{check_rules, CheckResult};
use crate::{
ContextSelectionSet, Error, ObjectType, Pos, QueryError, Result, SubscriptionType, Type,
Variables,
ContextSelectionSet, Error, ObjectType, Pos, QueryError, QueryResponse, Result,
SubscriptionType, Type, Variables,
};
use futures::channel::mpsc;
use futures::lock::Mutex;
use futures::SinkExt;
use futures::{SinkExt, TryFutureExt};
use graphql_parser::parse_query;
use graphql_parser::query::{
Definition, Field, FragmentDefinition, OperationDefinition, Selection,
@ -253,6 +253,13 @@ where
})
}
/// Execute query without create the `QueryBuilder`.
pub async fn execute(&self, source: &str) -> Result<QueryResponse> {
futures::future::ready(self.query(source))
.and_then(|builder| builder.execute())
.await
}
/// Create subscription stub, typically called inside the `SubscriptionTransport::handle_request` method/
pub fn create_subscription_stub(
&self,

View File

@ -106,7 +106,7 @@ impl EmptyEdgeFields {}
/// async fn main() {
/// let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
///
/// assert_eq!(schema.query("{ numbers(first: 2) { edges { node } } }").unwrap().execute().await.unwrap().data, serde_json::json!({
/// assert_eq!(schema.execute("{ numbers(first: 2) { edges { node } } }").await.unwrap().data, serde_json::json!({
/// "numbers": {
/// "edges": [
/// {"node": 0},
@ -115,7 +115,7 @@ impl EmptyEdgeFields {}
/// },
/// }));
///
/// assert_eq!(schema.query("{ numbers(last: 2) { edges { node diff } } }").unwrap().execute().await.unwrap().data, serde_json::json!({
/// assert_eq!(schema.execute("{ numbers(last: 2) { edges { node diff } } }").await.unwrap().data, serde_json::json!({
/// "numbers": {
/// "edges": [
/// {"node": -2, "diff": -1002},

View File

@ -44,7 +44,7 @@ pub async fn test_enum_type() {
"#
);
assert_eq!(
schema.query(&query).unwrap().execute().await.unwrap().data,
schema.execute(&query).await.unwrap().data,
serde_json::json!({
"value": "A",
"testArg": "A",

View File

@ -81,7 +81,7 @@ pub async fn test_input_object_default_value() {
}}"#
);
assert_eq!(
schema.query(&query).unwrap().execute().await.unwrap().data,
schema.execute(&query).await.unwrap().data,
serde_json::json!({
"a": {
"a": 999,

View File

@ -52,7 +52,7 @@ pub async fn test_list_type() {
json_value
);
assert_eq!(
schema.query(&query).unwrap().execute().await.unwrap().data,
schema.execute(&query).await.unwrap().data,
serde_json::json!({
"valueVec": vec![1, 2, 3, 4, 5],
"valueSlice": vec![1, 2, 3, 4, 5],

View File

@ -66,7 +66,7 @@ pub async fn test_optional_type() {
}}"#
);
assert_eq!(
schema.query(&query).unwrap().execute().await.unwrap().data,
schema.execute(&query).await.unwrap().data,
serde_json::json!({
"value1": 10,
"value1Ref": 10,

View File

@ -35,7 +35,7 @@ macro_rules! test_scalars {
let json_value: serde_json::Value = $value.into();
let query = format!("{{ value testArg(input: {0}) testInput(input: {{value: {0}}}) }}", json_value);
assert_eq!(
schema.query(&query).unwrap().execute().await.unwrap().data,
schema.execute(&query).await.unwrap().data,
serde_json::json!({ "value": $value, "testArg": $value, "testInput": $value })
);
}