Make all tests pass.

This commit is contained in:
Sunli 2020-09-06 18:38:06 +08:00
parent e50f1c9200
commit ca84859f46
12 changed files with 38 additions and 59 deletions

View File

@ -12,10 +12,6 @@ jobs:
name: Deploy book on gh-pages name: Deploy book on gh-pages
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.46
override: true
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Install mdBook - name: Install mdBook

View File

@ -15,8 +15,9 @@ jobs:
- uses: actions/checkout@v1 - uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1 - uses: actions-rs/toolchain@v1
with: with:
toolchain: 1.46 toolchain: 1.46.0
override: true override: true
components: clippy, rustfmt
- name: Check format - name: Check format
run: cargo fmt --all -- --check run: cargo fmt --all -- --check
- name: Check with clippy - name: Check with clippy

View File

@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v1 - uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1 - uses: actions-rs/toolchain@v1
with: with:
toolchain: 1.46 toolchain: 1.46.0
override: true override: true
- name: Install libsqlite3-dev - name: Install libsqlite3-dev
run: | run: |

View File

@ -39,7 +39,7 @@ jobs:
steps: steps:
- uses: actions-rs/toolchain@v1 - uses: actions-rs/toolchain@v1
with: with:
toolchain: 1.46 toolchain: 1.46.0
override: true override: true
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2

View File

@ -196,9 +196,7 @@ impl<'q> FromQuery<'q> for GQLRequest {
let decoded = value.url_decode().map_err(|e| e.to_string())?; let decoded = value.url_decode().map_err(|e| e.to_string())?;
let json_value = serde_json::from_str::<serde_json::Value>(&decoded) let json_value = serde_json::from_str::<serde_json::Value>(&decoded)
.map_err(|e| e.to_string())?; .map_err(|e| e.to_string())?;
variables = Variables::parse_from_json(json_value) variables = Variables::parse_from_json(json_value).into();
.map_err(|e| e.to_string())?
.into();
} }
} }
_ => { _ => {

View File

@ -1,6 +1,6 @@
pub use async_graphql::http::GQLResponse; pub use async_graphql::http::GQLResponse;
use async_graphql::{ObjectType, QueryResponse, Schema, SubscriptionType}; 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; use async_std::task;
#[cfg(feature = "jemalloc")] #[cfg(feature = "jemalloc")]

View File

@ -30,13 +30,13 @@ impl Display for Variables {
impl Variables { impl Variables {
/// Parse variables from JSON object. /// Parse variables from JSON object.
// TODO: Is this supposed to be able to return an error? /// If this `value` is not a `JsonObject`, then an empty `Variables` instance will be returned.
pub fn parse_from_json(value: serde_json::Value) -> Result<Self> { pub fn parse_from_json(value: serde_json::Value) -> Self {
Ok(if let Value::Object(obj) = value.into() { if let Value::Object(obj) = value.into() {
Self(obj) Self(obj)
} else { } else {
Default::default() Default::default()
}) }
} }
pub(crate) fn variable_path(&mut self, path: &str) -> Option<&mut Value> { pub(crate) fn variable_path(&mut self, path: &str) -> Option<&mut Value> {

View File

@ -45,9 +45,7 @@ impl IntoQueryBuilder for GQLRequest {
builder = builder.operation_name(operation_name); builder = builder.operation_name(operation_name);
} }
if let Some(variables) = self.variables { if let Some(variables) = self.variables {
if let Ok(variables) = Variables::parse_from_json(variables) { builder = builder.variables(Variables::parse_from_json(variables));
builder = builder.variables(variables);
}
} }
Ok(builder) Ok(builder)
} }

View File

@ -12,7 +12,6 @@ use crate::{
CacheControl, Error, ObjectType, Pos, QueryEnv, QueryError, QueryResponse, Result, CacheControl, Error, ObjectType, Pos, QueryEnv, QueryError, QueryResponse, Result,
SubscriptionType, Type, Variables, ID, SubscriptionType, Type, Variables, ID,
}; };
use bytes::Bytes;
use futures::channel::mpsc; use futures::channel::mpsc;
use futures::Stream; use futures::Stream;
use indexmap::map::IndexMap; use indexmap::map::IndexMap;

View File

@ -86,8 +86,7 @@ impl ConnectionTransport for WebSocketTransport {
if let Ok(request) = serde_json::from_value::<GQLRequest>(payload) { if let Ok(request) = serde_json::from_value::<GQLRequest>(payload) {
let variables = request let variables = request
.variables .variables
.map(|value| Variables::parse_from_json(value).ok()) .map(|value| Variables::parse_from_json(value))
.flatten()
.unwrap_or_default(); .unwrap_or_default();
match schema match schema
.create_subscription_stream( .create_subscription_stream(

View File

@ -1,4 +1,4 @@
use crate::parser::types::OperationDefinition; use crate::parser::types::{OperationDefinition, OperationType};
use crate::validation::visitor::{Visitor, VisitorContext}; use crate::validation::visitor::{Visitor, VisitorContext};
use crate::Positioned; use crate::Positioned;
@ -16,11 +16,14 @@ impl<'a> Visitor<'a> for UploadFile {
.registry .registry
.concrete_type_by_parsed_type(&var.node.var_type.node) .concrete_type_by_parsed_type(&var.node.var_type.node)
{ {
if ty.name() == "Upload" { if operation_definition.node.ty != OperationType::Mutation && ty.name() == "Upload"
ctx.report_error( {
vec![var.pos], if ty.name() == "Upload" {
"The Upload type is only allowed to be defined on a mutation", ctx.report_error(
); vec![var.pos],
"The Upload type is only allowed to be defined on a mutation",
);
}
} }
} }
} }

View File

@ -24,13 +24,10 @@ pub async fn test_variables() {
} }
"#, "#,
) )
.variables( .variables(Variables::parse_from_json(serde_json::json!({
Variables::parse_from_json(serde_json::json!({ "intVal": 10,
"intVal": 10, "intListVal": [1, 2, 3, 4, 5],
"intListVal": [1, 2, 3, 4, 5], })));
}))
.unwrap(),
);
let resp = query.execute(&schema).await.unwrap(); let resp = query.execute(&schema).await.unwrap();
assert_eq!( assert_eq!(
resp.data, 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(); let resp = query.execute(&schema).await.unwrap();
assert_eq!( assert_eq!(
resp.data, resp.data,
@ -119,12 +116,9 @@ pub async fn test_variable_null() {
} }
"#, "#,
) )
.variables( .variables(Variables::parse_from_json(serde_json::json!({
Variables::parse_from_json(serde_json::json!({ "intVal": null,
"intVal": null, })));
}))
.unwrap(),
);
let resp = query.execute(&schema).await.unwrap(); let resp = query.execute(&schema).await.unwrap();
assert_eq!( assert_eq!(
resp.data, resp.data,
@ -172,12 +166,9 @@ pub async fn test_variable_in_input_object() {
test(input: {value: $value }) test(input: {value: $value })
}"#; }"#;
let resp = QueryBuilder::new(query) let resp = QueryBuilder::new(query)
.variables( .variables(Variables::parse_from_json(serde_json::json!({
Variables::parse_from_json(serde_json::json!({ "value": 10,
"value": 10, })))
}))
.unwrap(),
)
.execute(&schema) .execute(&schema)
.await .await
.unwrap(); .unwrap();
@ -196,12 +187,9 @@ pub async fn test_variable_in_input_object() {
test2(input: [{value: $value }, {value: $value }]) test2(input: [{value: $value }, {value: $value }])
}"#; }"#;
let resp = QueryBuilder::new(query) let resp = QueryBuilder::new(query)
.variables( .variables(Variables::parse_from_json(serde_json::json!({
Variables::parse_from_json(serde_json::json!({ "value": 3,
"value": 3, })))
}))
.unwrap(),
)
.execute(&schema) .execute(&schema)
.await .await
.unwrap(); .unwrap();
@ -220,12 +208,9 @@ pub async fn test_variable_in_input_object() {
test(input: {value: $value }) test(input: {value: $value })
}"#; }"#;
let resp = QueryBuilder::new(query) let resp = QueryBuilder::new(query)
.variables( .variables(Variables::parse_from_json(serde_json::json!({
Variables::parse_from_json(serde_json::json!({ "value": 10,
"value": 10, })))
}))
.unwrap(),
)
.execute(&schema) .execute(&schema)
.await .await
.unwrap(); .unwrap();