async-graphql/tests/raw_ident.rs
Aaron Hill d8780bd720 Strip r# prefix when constructing getter ident
When calling `to_string()` on an identifier, the returned string will
have an `r#` prefix if the original identifer was a raw identifier. This
prefix needs to be removed if the identifier is interpolated into
another identifier (e.g. `__some_prefix__r#other_ident`) in order for
the new identifier to be valid.

This issue was previously masked due to the fact that the relevant test
uses `r#i32`. It's possible to use `i32` as a normal (non-raw) ident -
due to a bug in rustc, this means that the user-supplied `r#` prefix
will be lost when calling `to_string()`. This bug will eventually be
fixed, causing `to_string()` to start returning `r#i32` instead of
`i32`.

This commit strips the `r#` prefix (if present) from an identifier
before using it to construct a new identifier. The relevant test is
updated to use `r#match`, which actually requires the `r#` prefix to be
a valid identifier. This causes the test to fail without this patch on
current versions of Rust.
2020-12-20 19:08:22 -05:00

76 lines
1.7 KiB
Rust

use async_graphql::*;
use futures_util::stream::{Stream, StreamExt, TryStreamExt};
#[async_std::test]
pub async fn test_input_value_custom_error() {
#[derive(Enum, Copy, Clone, Eq, PartialEq)]
#[allow(non_camel_case_types)]
enum MyEnum {
r#type,
}
#[derive(SimpleObject)]
struct MyObject {
r#match: i32,
}
#[derive(InputObject)]
struct MyInputObject {
r#match: i32,
}
struct Query;
#[Object]
impl Query {
async fn r#type(&self, r#match: i32) -> i32 {
r#match
}
async fn obj(&self, obj: MyInputObject) -> MyObject {
MyObject {
r#match: obj.r#match,
}
}
async fn enum_value(&self, value: MyEnum) -> MyEnum {
value
}
}
struct SubscriptionRoot;
#[Subscription]
impl SubscriptionRoot {
async fn r#type(&self) -> impl Stream<Item = i32> {
futures_util::stream::iter(0..10)
}
}
let schema = Schema::new(Query, EmptyMutation, SubscriptionRoot);
let query = r#"
{
type(match: 99)
obj(obj: { match: 88} ) { match }
enumValue(value: TYPE)
}"#;
assert_eq!(
schema.execute(query).await.into_result().unwrap().data,
value!({
"type": 99,
"obj": { "match": 88 },
"enumValue": "TYPE",
})
);
let mut stream = schema
.execute_stream("subscription { type }")
.map(|resp| resp.into_result())
.map_ok(|resp| resp.data)
.boxed();
for i in 0..10 {
assert_eq!(value!({ "type": i }), stream.next().await.unwrap().unwrap());
}
assert!(stream.next().await.is_none());
}