Coder Social home page Coder Social logo

Comments (3)

sunli829 avatar sunli829 commented on April 28, 2024

I haven't supported validation of inputobject yet, and I'm adding this functionality.

from async-graphql.

sunli829 avatar sunli829 commented on April 28, 2024

Please upgrade to 1.6.7😁

from async-graphql.

dvdmgl avatar dvdmgl commented on April 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.