Comments (3)
I haven't supported validation of inputobject yet, and I'm adding this functionality.
from async-graphql.
Please upgrade to 1.6.7😁
from async-graphql.
Super 😎 💯
I have updated the tests, to notice with arguments returns 2 errors, but with InputObject
it returns only the 1st error.
#[cfg(test)]
mod tests {
use super::*;
use actix_web::dev::Service;
use actix_web::{
http::{header, StatusCode},
test, web, App,
};
use serde::Deserialize;
use serde_json::from_slice;
#[derive(Deserialize, Clone)]
struct ErrorLocation {
line: u32,
column: u32,
}
#[derive(Deserialize, Clone)]
struct ErrorMessage {
message: Option<String>,
locations: Option<Vec<ErrorLocation>>,
}
#[derive(Deserialize, Clone)]
struct ErrorResp {
errors: Option<Vec<ErrorMessage>>,
}
#[actix_rt::test]
async fn test_validates_input_with_errors() {
let schema = Schema::build(QueryRoot, MutationRoot, SubscriptionRoot)
.data(Storage::default())
.finish();
let handler = async_graphql_actix_web::HandlerBuilder::new(schema)
.enable_subscription()
.build();
let mut app = test::init_service(
App::new().service(web::resource("/").to(handler)),
)
.await;
let req = test::TestRequest::post()
.uri("/")
.header(header::CONTENT_TYPE, "application/json")
.set_payload(r#"{"operationName":null,"variables":{},"query":"mutation { createBookInput(input: {name: \"foo\", author: \"bar\"})}"}"#)
.to_request();
let res = app.call(req).await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
let response_body: ErrorResp = match res.response().body().as_ref() {
Some(actix_web::body::Body::Bytes(bytes)) => {
from_slice(bytes).unwrap()
}
_ => panic!("Response error"),
};
assert!(response_body.errors.is_some());
let errs = response_body.errors.unwrap();
assert_eq!(errs.len(), 1);
assert!(&errs[0].message.is_some());
assert!(&errs[0].locations.is_some());
assert_eq!(
errs[0].message,
Some(
r#"Invalid value for argument "input.name", the value length is 3, but the length must be greater than or equal to 5"#.into()
)
);
}
#[actix_rt::test]
async fn test_validates_arguments_with_errors() {
let schema = Schema::build(QueryRoot, MutationRoot, SubscriptionRoot)
.data(Storage::default())
.finish();
let handler = async_graphql_actix_web::HandlerBuilder::new(schema)
.enable_subscription()
.build();
let mut app = test::init_service(
App::new().service(web::resource("/").to(handler)),
)
.await;
let req = test::TestRequest::post()
.uri("/")
.header(header::CONTENT_TYPE, "application/json")
.set_payload(r#"{"operationName":null,"variables":{},"query":"mutation { createBookArgs(author: \"foo\", name: \"bar\")}"}"#)
.to_request();
let res = app.call(req).await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
let response_body: ErrorResp = match res.response().body().as_ref() {
Some(actix_web::body::Body::Bytes(bytes)) => {
from_slice(bytes).unwrap()
}
_ => panic!("Response error"),
};
assert!(response_body.errors.is_some());
let errs = response_body.errors.unwrap();
assert_eq!(errs.len(), 2);
assert!(&errs[0].message.is_some());
assert!(&errs[0].locations.is_some());
assert!(&errs[1].message.is_some());
assert!(&errs[1].locations.is_some());
assert_eq!(
errs[0].message,
Some(
r#"Invalid value for argument "author", the value length is 3, but the length must be greater than or equal to 5"#.into()
)
);
assert_eq!(
errs[1].message,
Some(
r#"Invalid value for argument "name", the value length is 3, but the length must be greater than or equal to 5"#.into()
)
);
}
}
Thank you for your awesome work.
from async-graphql.
Related Issues (20)
- Get mutable referenece to the global data defined in the `Context` or `Schema` HOT 1
- Confusing `unused_mut` warning in `#[Object]` HOT 8
- How to handle both directions of one-to-many relation in federated graph
- Question: How to get server to send ping messages on subscriptions? HOT 2
- Parsing multiple operations in a file HOT 1
- Non nullable variables should allow default values HOT 3
- Object with single skipped field but with ComplexObject HOT 2
- As using proxy type
- Using flatten inside an impl with no other fields causes a compile error
- Subscription with MPSC receiver in context data
- Reduce clippy noise from #[Object] macro HOT 2
- Guard trait lifetime HOT 1
- Using generics with both SimpleObject and InputObject as field in output type fails HOT 1
- Subscription Authentication
- Stack overflow after upgrade to 7.0.2 HOT 5
- Does async-graphql validate responses?
- Error reading data from ExtensionContext after upgrade to 7.0.3 HOT 1
- Create a general error formateer
- Allow flatten on arguments
- External fields not getting generated in SDL
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from async-graphql.