Add Schema::execute
This commit is contained in:
parent
b3525356c9
commit
fda683c931
|
@ -243,7 +243,7 @@ where
|
||||||
.map_err(actix_web::error::ErrorBadRequest)?
|
.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,
|
Ok(builder) => builder,
|
||||||
Err(err) => return Ok(web::Json(GQLResponse(Err(err))).respond_to(&req).await?),
|
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)
|
let gql_request = web::Json::<GQLRequest>::from_request(&req, &mut payload.0)
|
||||||
.await?
|
.await?
|
||||||
.into_inner();
|
.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,
|
Ok(builder) => builder,
|
||||||
Err(err) => return Ok(web::Json(GQLResponse(Err(err))).respond_to(&req).await?),
|
Err(err) => return Ok(web::Json(GQLResponse(Err(err))).respond_to(&req).await?),
|
||||||
};
|
};
|
||||||
|
|
|
@ -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> {
|
async fn index(s: web::Data<StarWarsSchema>, req: web::Json<GQLRequest>) -> web::Json<GQLResponse> {
|
||||||
web::Json(GQLResponse(
|
web::Json(GQLResponse(
|
||||||
req.into_inner()
|
futures::future::ready(req.into_inner().into_query_builder(&s))
|
||||||
.into_query_builder(&s)
|
|
||||||
.and_then(|builder| builder.execute())
|
.and_then(|builder| builder.execute())
|
||||||
.await,
|
.await,
|
||||||
))
|
))
|
||||||
|
|
|
@ -76,8 +76,7 @@ async fn index(
|
||||||
req: web::Json<GQLRequest>,
|
req: web::Json<GQLRequest>,
|
||||||
) -> web::Json<GQLResponse> {
|
) -> web::Json<GQLResponse> {
|
||||||
web::Json(GQLResponse(
|
web::Json(GQLResponse(
|
||||||
req.into_inner()
|
futures::future::ready(req.into_inner().into_query_builder(&s))
|
||||||
.into_query_builder(&s)
|
|
||||||
.and_then(|builder| builder.execute())
|
.and_then(|builder| builder.execute())
|
||||||
.await,
|
.await,
|
||||||
))
|
))
|
||||||
|
|
|
@ -11,8 +11,7 @@ type StarWarsSchema = Schema<starwars::QueryRoot, EmptyMutation, EmptySubscripti
|
||||||
async fn index(mut request: Request<StarWarsSchema>) -> Response {
|
async fn index(mut request: Request<StarWarsSchema>) -> Response {
|
||||||
let gql_request: GQLRequest = request.body_json().await.unwrap();
|
let gql_request: GQLRequest = request.body_json().await.unwrap();
|
||||||
let schema = request.state();
|
let schema = request.state();
|
||||||
let gql_response = gql_request
|
let gql_response = futures::future::ready(gql_request.into_query_builder(schema))
|
||||||
.into_query_builder(schema)
|
|
||||||
.and_then(|builder| builder.execute())
|
.and_then(|builder| builder.execute())
|
||||||
.await;
|
.await;
|
||||||
Response::new(200)
|
Response::new(200)
|
||||||
|
|
|
@ -30,7 +30,7 @@ pub struct GQLRequest {
|
||||||
|
|
||||||
impl GQLRequest {
|
impl GQLRequest {
|
||||||
/// Into query builder, you can set other parameters or execute queries immediately.
|
/// 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,
|
self,
|
||||||
schema: &Schema<Query, Mutation, Subscription>,
|
schema: &Schema<Query, Mutation, Subscription>,
|
||||||
) -> Result<QueryBuilder<Query, Mutation, Subscription>>
|
) -> Result<QueryBuilder<Query, Mutation, Subscription>>
|
||||||
|
|
16
src/lib.rs
16
src/lib.rs
|
@ -220,13 +220,13 @@ pub use types::{EnumItem, EnumType};
|
||||||
/// #[async_std::main]
|
/// #[async_std::main]
|
||||||
/// async fn main() {
|
/// async fn main() {
|
||||||
/// let schema = Schema::new(QueryRoot{ value: 10 }, EmptyMutation, EmptySubscription);
|
/// let schema = Schema::new(QueryRoot{ value: 10 }, EmptyMutation, EmptySubscription);
|
||||||
/// let res = schema.query(r#"{
|
/// let res = schema.execute(r#"{
|
||||||
/// value
|
/// value
|
||||||
/// valueRef
|
/// valueRef
|
||||||
/// valueWithError
|
/// valueWithError
|
||||||
/// valueWithArg1: valueWithArg
|
/// valueWithArg1: valueWithArg
|
||||||
/// valueWithArg2: valueWithArg(a: 99)
|
/// valueWithArg2: valueWithArg(a: 99)
|
||||||
/// }"#).unwrap().execute().await.unwrap().data;
|
/// }"#).await.unwrap().data;
|
||||||
/// assert_eq!(res, serde_json::json!({
|
/// assert_eq!(res, serde_json::json!({
|
||||||
/// "value": 10,
|
/// "value": 10,
|
||||||
/// "valueRef": 10,
|
/// "valueRef": 10,
|
||||||
|
@ -273,7 +273,7 @@ pub use async_graphql_derive::Object;
|
||||||
/// #[async_std::main]
|
/// #[async_std::main]
|
||||||
/// async fn main() {
|
/// async fn main() {
|
||||||
/// let schema = Schema::new(QueryRoot{ value: 10 }, EmptyMutation, EmptySubscription);
|
/// 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!({
|
/// assert_eq!(res, serde_json::json!({
|
||||||
/// "value": 10,
|
/// "value": 10,
|
||||||
/// }));
|
/// }));
|
||||||
|
@ -330,7 +330,7 @@ pub use async_graphql_derive::SimpleObject;
|
||||||
/// #[async_std::main]
|
/// #[async_std::main]
|
||||||
/// async fn main() {
|
/// async fn main() {
|
||||||
/// let schema = Schema::new(QueryRoot{ value1: MyEnum::A, value2: MyEnum::B }, EmptyMutation, EmptySubscription);
|
/// 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" }));
|
/// assert_eq!(res, serde_json::json!({ "value1": "A", "value2": "b" }));
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -379,11 +379,11 @@ pub use async_graphql_derive::Enum;
|
||||||
/// #[async_std::main]
|
/// #[async_std::main]
|
||||||
/// async fn main() {
|
/// async fn main() {
|
||||||
/// let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
|
/// let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
|
||||||
/// let res = schema.query(r#"
|
/// let res = schema.execute(r#"
|
||||||
/// {
|
/// {
|
||||||
/// value1: value(input:{a:9, b:3})
|
/// value1: value(input:{a:9, b:3})
|
||||||
/// value2: value(input:{a:9})
|
/// value2: value(input:{a:9})
|
||||||
/// }"#).unwrap().execute().await.unwrap().data;
|
/// }"#).await.unwrap().data;
|
||||||
/// assert_eq!(res, serde_json::json!({ "value1": 27, "value2": 90 }));
|
/// assert_eq!(res, serde_json::json!({ "value1": 27, "value2": 90 }));
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -484,14 +484,14 @@ pub use async_graphql_derive::InputObject;
|
||||||
/// #[async_std::main]
|
/// #[async_std::main]
|
||||||
/// async fn main() {
|
/// async fn main() {
|
||||||
/// let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription).data("hello".to_string()).finish();
|
/// let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription).data("hello".to_string()).finish();
|
||||||
/// let res = schema.query(r#"
|
/// let res = schema.execute(r#"
|
||||||
/// {
|
/// {
|
||||||
/// typeA {
|
/// typeA {
|
||||||
/// valueA
|
/// valueA
|
||||||
/// valueB
|
/// valueB
|
||||||
/// valueC(a: 3, b: 2)
|
/// valueC(a: 3, b: 2)
|
||||||
/// }
|
/// }
|
||||||
/// }"#).unwrap().execute().await.unwrap().data;
|
/// }"#).await.unwrap().data;
|
||||||
/// assert_eq!(res, serde_json::json!({
|
/// assert_eq!(res, serde_json::json!({
|
||||||
/// "typeA": {
|
/// "typeA": {
|
||||||
/// "valueA": "hello",
|
/// "valueA": "hello",
|
||||||
|
|
|
@ -7,12 +7,12 @@ use crate::subscription::{SubscriptionConnectionBuilder, SubscriptionStub, Subsc
|
||||||
use crate::types::QueryRoot;
|
use crate::types::QueryRoot;
|
||||||
use crate::validation::{check_rules, CheckResult};
|
use crate::validation::{check_rules, CheckResult};
|
||||||
use crate::{
|
use crate::{
|
||||||
ContextSelectionSet, Error, ObjectType, Pos, QueryError, Result, SubscriptionType, Type,
|
ContextSelectionSet, Error, ObjectType, Pos, QueryError, QueryResponse, Result,
|
||||||
Variables,
|
SubscriptionType, Type, Variables,
|
||||||
};
|
};
|
||||||
use futures::channel::mpsc;
|
use futures::channel::mpsc;
|
||||||
use futures::lock::Mutex;
|
use futures::lock::Mutex;
|
||||||
use futures::SinkExt;
|
use futures::{SinkExt, TryFutureExt};
|
||||||
use graphql_parser::parse_query;
|
use graphql_parser::parse_query;
|
||||||
use graphql_parser::query::{
|
use graphql_parser::query::{
|
||||||
Definition, Field, FragmentDefinition, OperationDefinition, Selection,
|
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/
|
/// Create subscription stub, typically called inside the `SubscriptionTransport::handle_request` method/
|
||||||
pub fn create_subscription_stub(
|
pub fn create_subscription_stub(
|
||||||
&self,
|
&self,
|
||||||
|
|
|
@ -106,7 +106,7 @@ impl EmptyEdgeFields {}
|
||||||
/// async fn main() {
|
/// async fn main() {
|
||||||
/// let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
|
/// 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": {
|
/// "numbers": {
|
||||||
/// "edges": [
|
/// "edges": [
|
||||||
/// {"node": 0},
|
/// {"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": {
|
/// "numbers": {
|
||||||
/// "edges": [
|
/// "edges": [
|
||||||
/// {"node": -2, "diff": -1002},
|
/// {"node": -2, "diff": -1002},
|
||||||
|
|
|
@ -44,7 +44,7 @@ pub async fn test_enum_type() {
|
||||||
"#
|
"#
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
schema.query(&query).unwrap().execute().await.unwrap().data,
|
schema.execute(&query).await.unwrap().data,
|
||||||
serde_json::json!({
|
serde_json::json!({
|
||||||
"value": "A",
|
"value": "A",
|
||||||
"testArg": "A",
|
"testArg": "A",
|
||||||
|
|
|
@ -81,7 +81,7 @@ pub async fn test_input_object_default_value() {
|
||||||
}}"#
|
}}"#
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
schema.query(&query).unwrap().execute().await.unwrap().data,
|
schema.execute(&query).await.unwrap().data,
|
||||||
serde_json::json!({
|
serde_json::json!({
|
||||||
"a": {
|
"a": {
|
||||||
"a": 999,
|
"a": 999,
|
||||||
|
|
|
@ -52,7 +52,7 @@ pub async fn test_list_type() {
|
||||||
json_value
|
json_value
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
schema.query(&query).unwrap().execute().await.unwrap().data,
|
schema.execute(&query).await.unwrap().data,
|
||||||
serde_json::json!({
|
serde_json::json!({
|
||||||
"valueVec": vec![1, 2, 3, 4, 5],
|
"valueVec": vec![1, 2, 3, 4, 5],
|
||||||
"valueSlice": vec![1, 2, 3, 4, 5],
|
"valueSlice": vec![1, 2, 3, 4, 5],
|
||||||
|
|
|
@ -66,7 +66,7 @@ pub async fn test_optional_type() {
|
||||||
}}"#
|
}}"#
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
schema.query(&query).unwrap().execute().await.unwrap().data,
|
schema.execute(&query).await.unwrap().data,
|
||||||
serde_json::json!({
|
serde_json::json!({
|
||||||
"value1": 10,
|
"value1": 10,
|
||||||
"value1Ref": 10,
|
"value1Ref": 10,
|
||||||
|
|
|
@ -35,7 +35,7 @@ macro_rules! test_scalars {
|
||||||
let json_value: serde_json::Value = $value.into();
|
let json_value: serde_json::Value = $value.into();
|
||||||
let query = format!("{{ value testArg(input: {0}) testInput(input: {{value: {0}}}) }}", json_value);
|
let query = format!("{{ value testArg(input: {0}) testInput(input: {{value: {0}}}) }}", json_value);
|
||||||
assert_eq!(
|
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 })
|
serde_json::json!({ "value": $value, "testArg": $value, "testInput": $value })
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user