Fix the problem that async-graphql cannot be compiled and passed in rust nightly-2020-05-25. #122

This commit is contained in:
Sunli 2020-05-28 10:26:07 +08:00
parent 0dfbbc0261
commit dfe381b2e0
4 changed files with 214 additions and 135 deletions

View File

@ -63,5 +63,5 @@ members = [
"async-graphql-actix-web",
"async-graphql-warp",
"async-graphql-tide",
"async-graphql-lambda",
# "async-graphql-lambda",
]

View File

@ -1,33 +1,48 @@
use crate::{InputValueError, InputValueResult, ScalarType, Value};
use async_graphql_derive::Scalar;
macro_rules! impl_float_scalars {
($($ty:ty),*) => {
$(
/// The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).
#[Scalar(internal, name = "Float")]
impl ScalarType for $ty {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::Int(n) => Ok(n as Self),
Value::Float(n) => Ok(n as Self),
_ => Err(InputValueError::ExpectedType(value))
}
}
fn is_valid(value: &Value) -> bool {
match value {
Value::Int(_) | Value::Float(_) => true,
_ => false
}
}
fn to_value(&self) -> Value {
Value::Float(*self as f64)
}
/// The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).
#[Scalar(internal, name = "Float")]
impl ScalarType for f32 {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::Int(n) => Ok(n as Self),
Value::Float(n) => Ok(n as Self),
_ => Err(InputValueError::ExpectedType(value)),
}
)*
};
}
fn is_valid(value: &Value) -> bool {
match value {
Value::Int(_) | Value::Float(_) => true,
_ => false,
}
}
fn to_value(&self) -> Value {
Value::Float(*self as f64)
}
}
impl_float_scalars!(f32, f64);
/// The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).
#[Scalar(internal, name = "Float")]
impl ScalarType for f64 {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::Int(n) => Ok(n as Self),
Value::Float(n) => Ok(n as Self),
_ => Err(InputValueError::ExpectedType(value)),
}
}
fn is_valid(value: &Value) -> bool {
match value {
Value::Int(_) | Value::Float(_) => true,
_ => false,
}
}
fn to_value(&self) -> Value {
Value::Float(*self as f64)
}
}

View File

@ -1,63 +1,180 @@
use crate::{InputValueError, InputValueResult, ScalarType, Value};
use async_graphql_derive::Scalar;
macro_rules! impl_integer_scalars {
($($ty:ty),*) => {
$(
/// The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.
#[Scalar(internal, name = "Int")]
impl ScalarType for $ty {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::Int(n) => Ok(n as Self),
_ => Err(InputValueError::ExpectedType(value))
}
}
fn is_valid(value: &Value) -> bool {
match value {
Value::Int(_) => true,
_ => false
}
}
fn to_value(&self) -> Value {
Value::Int(*self as i32)
}
/// The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.
#[Scalar(internal, name = "Int")]
impl ScalarType for i8 {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::Int(n) => Ok(n as Self),
_ => Err(InputValueError::ExpectedType(value)),
}
)*
};
}
fn is_valid(value: &Value) -> bool {
match value {
Value::Int(_) => true,
_ => false,
}
}
fn to_value(&self) -> Value {
Value::Int(*self as i32)
}
}
impl_integer_scalars!(i8, i16, i32, u8, u16, u32);
macro_rules! impl_int64_scalars {
($($ty:ty),*) => {
$(
/// The `Int64` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^64) and 2^64 - 1.
#[Scalar(internal, name = "Int64")]
impl ScalarType for $ty {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::Int(n) => Ok(n as Self),
Value::String(s) => Ok(s.parse()?),
_ => Err(InputValueError::ExpectedType(value))
}
}
fn is_valid(value: &Value) -> bool {
match value {
Value::Int(_) | Value::String(_) => true,
_ => false
}
}
fn to_value(&self) -> Value {
Value::String(self.to_string())
}
/// The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.
#[Scalar(internal, name = "Int")]
impl ScalarType for i16 {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::Int(n) => Ok(n as Self),
_ => Err(InputValueError::ExpectedType(value)),
}
)*
};
}
fn is_valid(value: &Value) -> bool {
match value {
Value::Int(_) => true,
_ => false,
}
}
fn to_value(&self) -> Value {
Value::Int(*self as i32)
}
}
impl_int64_scalars!(i64, u64);
/// The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.
#[Scalar(internal, name = "Int")]
impl ScalarType for i32 {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::Int(n) => Ok(n as Self),
_ => Err(InputValueError::ExpectedType(value)),
}
}
fn is_valid(value: &Value) -> bool {
match value {
Value::Int(_) => true,
_ => false,
}
}
fn to_value(&self) -> Value {
Value::Int(*self as i32)
}
}
/// The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.
#[Scalar(internal, name = "Int")]
impl ScalarType for u8 {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::Int(n) => Ok(n as Self),
_ => Err(InputValueError::ExpectedType(value)),
}
}
fn is_valid(value: &Value) -> bool {
match value {
Value::Int(_) => true,
_ => false,
}
}
fn to_value(&self) -> Value {
Value::Int(*self as i32)
}
}
/// The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.
#[Scalar(internal, name = "Int")]
impl ScalarType for u16 {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::Int(n) => Ok(n as Self),
_ => Err(InputValueError::ExpectedType(value)),
}
}
fn is_valid(value: &Value) -> bool {
match value {
Value::Int(_) => true,
_ => false,
}
}
fn to_value(&self) -> Value {
Value::Int(*self as i32)
}
}
/// The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.
#[Scalar(internal, name = "Int")]
impl ScalarType for u32 {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::Int(n) => Ok(n as Self),
_ => Err(InputValueError::ExpectedType(value)),
}
}
fn is_valid(value: &Value) -> bool {
match value {
Value::Int(_) => true,
_ => false,
}
}
fn to_value(&self) -> Value {
Value::Int(*self as i32)
}
}
/// The `Int64` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^64) and 2^64 - 1.
#[Scalar(internal, name = "Int64")]
impl ScalarType for i64 {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::Int(n) => Ok(n as Self),
Value::String(s) => Ok(s.parse()?),
_ => Err(InputValueError::ExpectedType(value)),
}
}
fn is_valid(value: &Value) -> bool {
match value {
Value::Int(_) | Value::String(_) => true,
_ => false,
}
}
fn to_value(&self) -> Value {
Value::String(self.to_string())
}
}
/// The `Int64` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^64) and 2^64 - 1.
#[Scalar(internal, name = "Int64")]
impl ScalarType for u64 {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::Int(n) => Ok(n as Self),
Value::String(s) => Ok(s.parse()?),
_ => Err(InputValueError::ExpectedType(value)),
}
}
fn is_valid(value: &Value) -> bool {
match value {
Value::Int(_) | Value::String(_) => true,
_ => false,
}
}
fn to_value(&self) -> Value {
Value::String(self.to_string())
}
}

View File

@ -1,53 +0,0 @@
use async_graphql::*;
macro_rules! test_scalars {
($test_name:ident, $ty:ty, $value:expr, $res_value:expr) => {
#[async_std::test]
pub async fn $test_name() {
#[InputObject]
struct MyInput {
value: $ty,
}
struct Root {
value: $ty,
}
#[Object]
impl Root {
async fn value(&self) -> $ty {
self.value
}
async fn test_arg(&self, input: $ty) -> $ty {
input
}
async fn test_input(&self, input: MyInput) -> $ty {
input.value
}
}
let schema = Schema::new(Root { value: $value }, EmptyMutation, EmptySubscription);
let json_value: serde_json::Value = $value.into();
let query = format!("{{ value testArg(input: {0}) testInput(input: {{value: {0}}}) }}", json_value);
assert_eq!(
schema.execute(&query).await.unwrap().data,
serde_json::json!({ "value": $res_value, "testArg": $res_value, "testInput": $res_value })
);
}
};
}
test_scalars!(test_i8_scalar, i8, 10, 10);
test_scalars!(test_i16_scalar, i16, 10, 10);
test_scalars!(test_i32_scalar, i32, 10, 10);
test_scalars!(test_u8_scalar, u8, 10, 10);
test_scalars!(test_u16_scalar, u16, 10, 10);
test_scalars!(test_u32_scalar, u32, 10, 10);
test_scalars!(test_bool_scalar, bool, true, true);
test_scalars!(test_f32_scalar, f32, 10.5, 10.5);
test_scalars!(test_f64_scalar, f32, 10.5, 10.5);
test_scalars!(test_i64_scalar, i64, 10, "10");
test_scalars!(test_u64_scalar, u64, 10, "10");