From ca84859f46159c2871bbd5718f08eaa9ddb20117 Mon Sep 17 00:00:00 2001 From: Sunli Date: Sun, 6 Sep 2020 18:38:06 +0800 Subject: [PATCH] Make all tests pass. --- .github/workflows/book.yml | 4 --- .github/workflows/ci.yml | 3 +- .github/workflows/code-coverage.yml | 2 +- .github/workflows/release.yml | 2 +- async-graphql-rocket/src/lib.rs | 4 +-- benchmark/src/lib.rs | 2 +- src/context.rs | 8 ++--- src/http/mod.rs | 4 +-- src/schema.rs | 1 - src/subscription/ws_transport.rs | 3 +- src/validation/rules/upload_file.rs | 15 +++++---- tests/variables.rs | 49 ++++++++++------------------- 12 files changed, 38 insertions(+), 59 deletions(-) diff --git a/.github/workflows/book.yml b/.github/workflows/book.yml index 4976bdbf..970bcec1 100644 --- a/.github/workflows/book.yml +++ b/.github/workflows/book.yml @@ -12,10 +12,6 @@ jobs: name: Deploy book on gh-pages runs-on: ubuntu-latest steps: - - uses: actions-rs/toolchain@v1 - with: - toolchain: 1.46 - override: true - name: Checkout uses: actions/checkout@v2 - name: Install mdBook diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9ecf1ae0..f7fdbcd0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,8 +15,9 @@ jobs: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: - toolchain: 1.46 + toolchain: 1.46.0 override: true + components: clippy, rustfmt - name: Check format run: cargo fmt --all -- --check - name: Check with clippy diff --git a/.github/workflows/code-coverage.yml b/.github/workflows/code-coverage.yml index 40e39310..e9d37cd1 100644 --- a/.github/workflows/code-coverage.yml +++ b/.github/workflows/code-coverage.yml @@ -12,7 +12,7 @@ jobs: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: - toolchain: 1.46 + toolchain: 1.46.0 override: true - name: Install libsqlite3-dev run: | diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index f0ea6426..30daf433 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -39,7 +39,7 @@ jobs: steps: - uses: actions-rs/toolchain@v1 with: - toolchain: 1.46 + toolchain: 1.46.0 override: true - name: Checkout uses: actions/checkout@v2 diff --git a/async-graphql-rocket/src/lib.rs b/async-graphql-rocket/src/lib.rs index 8d36e9e4..7fa93442 100644 --- a/async-graphql-rocket/src/lib.rs +++ b/async-graphql-rocket/src/lib.rs @@ -196,9 +196,7 @@ impl<'q> FromQuery<'q> for GQLRequest { let decoded = value.url_decode().map_err(|e| e.to_string())?; let json_value = serde_json::from_str::(&decoded) .map_err(|e| e.to_string())?; - variables = Variables::parse_from_json(json_value) - .map_err(|e| e.to_string())? - .into(); + variables = Variables::parse_from_json(json_value).into(); } } _ => { diff --git a/benchmark/src/lib.rs b/benchmark/src/lib.rs index d6462c9b..92949747 100644 --- a/benchmark/src/lib.rs +++ b/benchmark/src/lib.rs @@ -1,6 +1,6 @@ pub use async_graphql::http::GQLResponse; use async_graphql::{ObjectType, QueryResponse, Schema, SubscriptionType}; -use async_graphql_parser::{parse_query, query::Document}; +use async_graphql_parser::{parse_query, types::Document}; use async_std::task; #[cfg(feature = "jemalloc")] diff --git a/src/context.rs b/src/context.rs index b0fde8a3..b91d12d2 100644 --- a/src/context.rs +++ b/src/context.rs @@ -30,13 +30,13 @@ impl Display for Variables { impl Variables { /// Parse variables from JSON object. - // TODO: Is this supposed to be able to return an error? - pub fn parse_from_json(value: serde_json::Value) -> Result { - Ok(if let Value::Object(obj) = value.into() { + /// If this `value` is not a `JsonObject`, then an empty `Variables` instance will be returned. + pub fn parse_from_json(value: serde_json::Value) -> Self { + if let Value::Object(obj) = value.into() { Self(obj) } else { Default::default() - }) + } } pub(crate) fn variable_path(&mut self, path: &str) -> Option<&mut Value> { diff --git a/src/http/mod.rs b/src/http/mod.rs index 6e521e23..2d1e8275 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -45,9 +45,7 @@ impl IntoQueryBuilder for GQLRequest { builder = builder.operation_name(operation_name); } if let Some(variables) = self.variables { - if let Ok(variables) = Variables::parse_from_json(variables) { - builder = builder.variables(variables); - } + builder = builder.variables(Variables::parse_from_json(variables)); } Ok(builder) } diff --git a/src/schema.rs b/src/schema.rs index 3198c4c2..c347180f 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -12,7 +12,6 @@ use crate::{ CacheControl, Error, ObjectType, Pos, QueryEnv, QueryError, QueryResponse, Result, SubscriptionType, Type, Variables, ID, }; -use bytes::Bytes; use futures::channel::mpsc; use futures::Stream; use indexmap::map::IndexMap; diff --git a/src/subscription/ws_transport.rs b/src/subscription/ws_transport.rs index 3164768d..2cc09c5b 100644 --- a/src/subscription/ws_transport.rs +++ b/src/subscription/ws_transport.rs @@ -86,8 +86,7 @@ impl ConnectionTransport for WebSocketTransport { if let Ok(request) = serde_json::from_value::(payload) { let variables = request .variables - .map(|value| Variables::parse_from_json(value).ok()) - .flatten() + .map(|value| Variables::parse_from_json(value)) .unwrap_or_default(); match schema .create_subscription_stream( diff --git a/src/validation/rules/upload_file.rs b/src/validation/rules/upload_file.rs index 75c79ae1..4aac5b80 100644 --- a/src/validation/rules/upload_file.rs +++ b/src/validation/rules/upload_file.rs @@ -1,4 +1,4 @@ -use crate::parser::types::OperationDefinition; +use crate::parser::types::{OperationDefinition, OperationType}; use crate::validation::visitor::{Visitor, VisitorContext}; use crate::Positioned; @@ -16,11 +16,14 @@ impl<'a> Visitor<'a> for UploadFile { .registry .concrete_type_by_parsed_type(&var.node.var_type.node) { - if ty.name() == "Upload" { - ctx.report_error( - vec![var.pos], - "The Upload type is only allowed to be defined on a mutation", - ); + if operation_definition.node.ty != OperationType::Mutation && ty.name() == "Upload" + { + if ty.name() == "Upload" { + ctx.report_error( + vec![var.pos], + "The Upload type is only allowed to be defined on a mutation", + ); + } } } } diff --git a/tests/variables.rs b/tests/variables.rs index 600076cb..bb7dccd6 100644 --- a/tests/variables.rs +++ b/tests/variables.rs @@ -24,13 +24,10 @@ pub async fn test_variables() { } "#, ) - .variables( - Variables::parse_from_json(serde_json::json!({ - "intVal": 10, - "intListVal": [1, 2, 3, 4, 5], - })) - .unwrap(), - ); + .variables(Variables::parse_from_json(serde_json::json!({ + "intVal": 10, + "intListVal": [1, 2, 3, 4, 5], + }))); let resp = query.execute(&schema).await.unwrap(); assert_eq!( resp.data, @@ -90,7 +87,7 @@ pub async fn test_variable_no_value() { } "#, ) - .variables(Variables::parse_from_json(serde_json::json!({})).unwrap()); + .variables(Variables::parse_from_json(serde_json::json!({}))); let resp = query.execute(&schema).await.unwrap(); assert_eq!( resp.data, @@ -119,12 +116,9 @@ pub async fn test_variable_null() { } "#, ) - .variables( - Variables::parse_from_json(serde_json::json!({ - "intVal": null, - })) - .unwrap(), - ); + .variables(Variables::parse_from_json(serde_json::json!({ + "intVal": null, + }))); let resp = query.execute(&schema).await.unwrap(); assert_eq!( resp.data, @@ -172,12 +166,9 @@ pub async fn test_variable_in_input_object() { test(input: {value: $value }) }"#; let resp = QueryBuilder::new(query) - .variables( - Variables::parse_from_json(serde_json::json!({ - "value": 10, - })) - .unwrap(), - ) + .variables(Variables::parse_from_json(serde_json::json!({ + "value": 10, + }))) .execute(&schema) .await .unwrap(); @@ -196,12 +187,9 @@ pub async fn test_variable_in_input_object() { test2(input: [{value: $value }, {value: $value }]) }"#; let resp = QueryBuilder::new(query) - .variables( - Variables::parse_from_json(serde_json::json!({ - "value": 3, - })) - .unwrap(), - ) + .variables(Variables::parse_from_json(serde_json::json!({ + "value": 3, + }))) .execute(&schema) .await .unwrap(); @@ -220,12 +208,9 @@ pub async fn test_variable_in_input_object() { test(input: {value: $value }) }"#; let resp = QueryBuilder::new(query) - .variables( - Variables::parse_from_json(serde_json::json!({ - "value": 10, - })) - .unwrap(), - ) + .variables(Variables::parse_from_json(serde_json::json!({ + "value": 10, + }))) .execute(&schema) .await .unwrap();