2020-04-06 10:30:38 +00:00
|
|
|
use async_graphql::*;
|
2020-10-16 06:49:22 +00:00
|
|
|
use futures_util::stream::{Stream, StreamExt, TryStreamExt};
|
2020-04-06 10:30:38 +00:00
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Query;
|
2020-10-20 03:49:31 +00:00
|
|
|
|
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Query {
|
2020-10-20 03:49:31 +00:00
|
|
|
async fn value(&self) -> i32 {
|
|
|
|
10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-04-06 10:30:38 +00:00
|
|
|
pub async fn test_subscription() {
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(SimpleObject)]
|
2020-04-06 10:30:38 +00:00
|
|
|
struct Event {
|
|
|
|
a: i32,
|
|
|
|
b: i32,
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Subscription;
|
2020-04-06 10:30:38 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Subscription]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Subscription {
|
2020-04-07 06:30:46 +00:00
|
|
|
async fn values(&self, start: i32, end: i32) -> impl Stream<Item = i32> {
|
2020-10-16 06:49:22 +00:00
|
|
|
futures_util::stream::iter(start..end)
|
2020-04-06 10:30:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 06:30:46 +00:00
|
|
|
async fn events(&self, start: i32, end: i32) -> impl Stream<Item = Event> {
|
2020-10-16 06:49:22 +00:00
|
|
|
futures_util::stream::iter((start..end).map(|n| Event { a: n, b: n * 10 }))
|
2020-04-06 10:30:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
let schema = Schema::new(Query, EmptyMutation, Subscription);
|
2020-04-06 10:30:38 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut stream = schema
|
2020-09-10 11:35:48 +00:00
|
|
|
.execute_stream("subscription { values(start: 10, end: 20) }")
|
2021-04-04 04:05:54 +00:00
|
|
|
.map(|resp| resp.into_result().unwrap().data);
|
2020-04-06 10:30:38 +00:00
|
|
|
for i in 10..20 {
|
2020-10-12 02:17:05 +00:00
|
|
|
assert_eq!(value!({ "values": i }), stream.next().await.unwrap());
|
2020-04-06 10:30:38 +00:00
|
|
|
}
|
|
|
|
assert!(stream.next().await.is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut stream = schema
|
2020-09-10 11:35:48 +00:00
|
|
|
.execute_stream("subscription { events(start: 10, end: 20) { a b } }")
|
2021-04-04 04:05:54 +00:00
|
|
|
.map(|resp| resp.into_result().unwrap().data);
|
2020-04-06 10:30:38 +00:00
|
|
|
for i in 10..20 {
|
|
|
|
assert_eq!(
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({ "events": {"a": i, "b": i * 10} }),
|
2020-10-10 02:32:43 +00:00
|
|
|
stream.next().await.unwrap()
|
2020-04-06 10:30:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
assert!(stream.next().await.is_none());
|
|
|
|
}
|
|
|
|
}
|
2020-04-08 01:05:54 +00:00
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-04-23 02:26:16 +00:00
|
|
|
pub async fn test_subscription_with_ctx_data() {
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Query;
|
2020-04-23 02:26:16 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Query {
|
2020-10-20 03:49:31 +00:00
|
|
|
async fn value(&self) -> i32 {
|
|
|
|
10
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 02:26:16 +00:00
|
|
|
|
2020-04-23 14:29:38 +00:00
|
|
|
struct MyObject;
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2020-04-23 14:29:38 +00:00
|
|
|
impl MyObject {
|
|
|
|
async fn value(&self, ctx: &Context<'_>) -> i32 {
|
2020-07-06 23:54:57 +00:00
|
|
|
*ctx.data_unchecked::<i32>()
|
2020-04-23 14:29:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Subscription;
|
2020-04-23 02:26:16 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Subscription]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Subscription {
|
2020-04-23 02:26:16 +00:00
|
|
|
async fn values(&self, ctx: &Context<'_>) -> impl Stream<Item = i32> {
|
2020-07-06 23:54:57 +00:00
|
|
|
let value = *ctx.data_unchecked::<i32>();
|
2020-10-16 06:49:22 +00:00
|
|
|
futures_util::stream::once(async move { value })
|
2020-04-23 02:26:16 +00:00
|
|
|
}
|
2020-04-23 14:29:38 +00:00
|
|
|
|
|
|
|
async fn objects(&self) -> impl Stream<Item = MyObject> {
|
2020-10-16 06:49:22 +00:00
|
|
|
futures_util::stream::once(async move { MyObject })
|
2020-04-23 14:29:38 +00:00
|
|
|
}
|
2020-04-23 02:26:16 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
let schema = Schema::new(Query, EmptyMutation, Subscription);
|
2020-04-23 02:26:16 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut stream = schema
|
2020-09-10 11:35:48 +00:00
|
|
|
.execute_stream(Request::new("subscription { values objects { value } }").data(100i32))
|
2021-04-04 04:05:54 +00:00
|
|
|
.map(|resp| resp.data);
|
2020-10-12 02:17:05 +00:00
|
|
|
assert_eq!(value!({ "values": 100 }), stream.next().await.unwrap());
|
2020-04-23 02:26:16 +00:00
|
|
|
assert_eq!(
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({ "objects": { "value": 100 } }),
|
2020-10-10 02:32:43 +00:00
|
|
|
stream.next().await.unwrap()
|
2020-04-23 14:29:38 +00:00
|
|
|
);
|
2020-04-23 02:26:16 +00:00
|
|
|
assert!(stream.next().await.is_none());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-04-23 03:26:36 +00:00
|
|
|
pub async fn test_subscription_with_token() {
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Query;
|
2020-04-23 03:26:36 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Query {
|
2020-10-20 03:49:31 +00:00
|
|
|
async fn value(&self) -> i32 {
|
|
|
|
10
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 03:26:36 +00:00
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Subscription;
|
2020-04-23 03:26:36 +00:00
|
|
|
|
|
|
|
struct Token(String);
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Subscription]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Subscription {
|
Rework errors
This completely overhauls the error system used in async-graphql.
- `Error` has been renamed to `ServerError` and `FieldError` has been
renamed to just `Error`. This is because `FieldError` is by far the most
common error that users will have to use so it makes sense to use the
most obvious error name. Also, the current name didn't make sense as it
was used for things other than field errors, such as the data callback
for websockets.
- `ServerError` has been made completely opaque. Before it was an enum
of all the possible errors, but now it just contains an error message,
the locations, the path and extensions. It is a shame that we lose
information, it makes more sense as _conceptually_ GraphQL does not
provide that information. It also frees us to change the internals of
async-graphql a lot more.
- The path of errors is no longer an opaque JSON value but a regular
type, `Vec<PathSegment>`. The type duplication of `PathSegment` and
`QueryPathSegment` is unfortunate, I plan to work on this in the future.
- Now that `ServerError` is opaque, `RuleError` has been removed from
the public API, making it simpler.
- Additionally `QueryError` has been completely removed. Instead the
error messages are constructed ad-hoc; I took care to never repeat an
error message.
- Instead of constructing field-not-found errors inside the
implementations of field resolvers they now return `Option`s, where a
`None` value is representative of the field not being found.
- As an unfortunate consequence of the last change, self-referential
types based on the output of a subscription resolver can no longer be
created. This does not mean anything for users, but causes lifetime
issues in the implementation of merged objects. I fixed it with a bit of
a hack, but this'll have to be looked into further.
- `InputValueError` now has a generic parameter - it's kind of weird but
it's necessary for ergonomics. It also improves error messages.
- The `ErrorExtensions` trait has been removed. I didn't think the
`extend` method was necessary since `From` impls exist. But the
ergonomics are still there with a new trait `ExtendError`, which
is implemented for both errors and results.
- `Response` now supports serializing multiple errors. This allows for
nice things like having multiple validation errors not be awkwardly
shoved into a single error.
- When an error occurs in execution, data is sent as `null`. This is
slightly more compliant with the spec but the algorithm described in
<https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has
yet to be implemented.
2020-09-29 19:06:44 +00:00
|
|
|
async fn values(&self, ctx: &Context<'_>) -> Result<impl Stream<Item = i32>> {
|
2020-07-06 23:54:57 +00:00
|
|
|
if ctx.data_unchecked::<Token>().0 != "123456" {
|
2020-04-23 03:26:36 +00:00
|
|
|
return Err("forbidden".into());
|
|
|
|
}
|
2020-10-16 06:49:22 +00:00
|
|
|
Ok(futures_util::stream::once(async move { 100 }))
|
2020-04-23 03:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
let schema = Schema::new(Query, EmptyMutation, Subscription);
|
2020-04-23 03:26:36 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut stream = schema
|
2020-09-10 11:35:48 +00:00
|
|
|
.execute_stream(
|
|
|
|
Request::new("subscription { values }").data(Token("123456".to_string())),
|
2020-04-23 03:26:36 +00:00
|
|
|
)
|
2021-04-04 04:05:54 +00:00
|
|
|
.map(|resp| resp.into_result().unwrap().data);
|
2020-10-12 02:17:05 +00:00
|
|
|
assert_eq!(value!({ "values": 100 }), stream.next().await.unwrap());
|
2020-04-23 03:26:36 +00:00
|
|
|
assert!(stream.next().await.is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
assert!(schema
|
2020-09-10 11:35:48 +00:00
|
|
|
.execute_stream(
|
|
|
|
Request::new("subscription { values }").data(Token("654321".to_string()))
|
2020-04-23 03:26:36 +00:00
|
|
|
)
|
2020-09-10 23:58:02 +00:00
|
|
|
.next()
|
2020-04-23 03:26:36 +00:00
|
|
|
.await
|
2020-09-10 23:58:02 +00:00
|
|
|
.unwrap()
|
2020-04-23 03:26:36 +00:00
|
|
|
.is_err());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-05-03 01:12:14 +00:00
|
|
|
pub async fn test_subscription_inline_fragment() {
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(SimpleObject)]
|
2020-05-03 01:12:14 +00:00
|
|
|
struct Event {
|
|
|
|
a: i32,
|
|
|
|
b: i32,
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Query;
|
2020-10-20 03:49:31 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Query {
|
2020-10-20 03:49:31 +00:00
|
|
|
async fn value(&self) -> i32 {
|
|
|
|
10
|
|
|
|
}
|
|
|
|
}
|
2020-05-03 01:12:14 +00:00
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Subscription;
|
2020-05-03 01:12:14 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Subscription]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Subscription {
|
2020-05-03 01:12:14 +00:00
|
|
|
async fn events(&self, start: i32, end: i32) -> impl Stream<Item = Event> {
|
2020-10-16 06:49:22 +00:00
|
|
|
futures_util::stream::iter((start..end).map(|n| Event { a: n, b: n * 10 }))
|
2020-05-03 01:12:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
let schema = Schema::new(Query, EmptyMutation, Subscription);
|
2020-05-03 01:12:14 +00:00
|
|
|
let mut stream = schema
|
2020-09-10 11:35:48 +00:00
|
|
|
.execute_stream(
|
2020-05-03 01:12:14 +00:00
|
|
|
r#"
|
|
|
|
subscription {
|
|
|
|
events(start: 10, end: 20) {
|
|
|
|
a
|
|
|
|
... {
|
|
|
|
b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
2021-04-04 04:05:54 +00:00
|
|
|
.map(|resp| resp.data);
|
2020-05-03 01:12:14 +00:00
|
|
|
for i in 10..20 {
|
|
|
|
assert_eq!(
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({ "events": {"a": i, "b": i * 10} }),
|
2020-10-10 02:32:43 +00:00
|
|
|
stream.next().await.unwrap()
|
2020-05-03 01:12:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
assert!(stream.next().await.is_none());
|
|
|
|
}
|
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-05-03 01:12:14 +00:00
|
|
|
pub async fn test_subscription_fragment() {
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(SimpleObject)]
|
2020-05-03 01:12:14 +00:00
|
|
|
struct Event {
|
|
|
|
a: i32,
|
|
|
|
b: i32,
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(Interface)]
|
2020-09-13 03:41:15 +00:00
|
|
|
#[graphql(field(name = "a", type = "&i32"))]
|
2020-05-11 03:25:49 +00:00
|
|
|
enum MyInterface {
|
|
|
|
Event(Event),
|
|
|
|
}
|
2020-05-03 01:12:14 +00:00
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Subscription;
|
2020-05-03 01:12:14 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Subscription]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Subscription {
|
2020-05-03 01:12:14 +00:00
|
|
|
async fn events(&self, start: i32, end: i32) -> impl Stream<Item = Event> {
|
2020-10-16 06:49:22 +00:00
|
|
|
futures_util::stream::iter((start..end).map(|n| Event { a: n, b: n * 10 }))
|
2020-05-03 01:12:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
let schema = Schema::build(Query, EmptyMutation, Subscription)
|
2021-11-09 10:52:10 +00:00
|
|
|
.register_output_type::<MyInterface>()
|
2020-05-03 01:12:14 +00:00
|
|
|
.finish();
|
|
|
|
let mut stream = schema
|
2020-09-10 11:35:48 +00:00
|
|
|
.execute_stream(
|
2020-05-03 01:12:14 +00:00
|
|
|
r#"
|
|
|
|
subscription s {
|
|
|
|
events(start: 10, end: 20) {
|
|
|
|
... on MyInterface {
|
|
|
|
a
|
|
|
|
}
|
|
|
|
b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
2021-04-04 04:05:54 +00:00
|
|
|
.map(|resp| resp.data);
|
2020-09-11 07:54:56 +00:00
|
|
|
for i in 10i32..20 {
|
2020-05-03 01:12:14 +00:00
|
|
|
assert_eq!(
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({ "events": {"a": i, "b": i * 10} }),
|
2020-10-10 02:32:43 +00:00
|
|
|
stream.next().await.unwrap()
|
2020-05-03 01:12:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
assert!(stream.next().await.is_none());
|
|
|
|
}
|
2020-05-03 02:06:17 +00:00
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-05-03 02:06:17 +00:00
|
|
|
pub async fn test_subscription_fragment2() {
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(SimpleObject)]
|
2020-05-03 02:06:17 +00:00
|
|
|
struct Event {
|
|
|
|
a: i32,
|
|
|
|
b: i32,
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(Interface)]
|
2020-09-13 03:41:15 +00:00
|
|
|
#[graphql(field(name = "a", type = "&i32"))]
|
2020-05-11 03:25:49 +00:00
|
|
|
enum MyInterface {
|
|
|
|
Event(Event),
|
|
|
|
}
|
2020-05-03 02:06:17 +00:00
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Subscription;
|
2020-05-03 02:06:17 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Subscription]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Subscription {
|
2020-05-03 02:06:17 +00:00
|
|
|
async fn events(&self, start: i32, end: i32) -> impl Stream<Item = Event> {
|
2020-10-16 06:49:22 +00:00
|
|
|
futures_util::stream::iter((start..end).map(|n| Event { a: n, b: n * 10 }))
|
2020-05-03 02:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
let schema = Schema::build(Query, EmptyMutation, Subscription)
|
2021-11-09 10:52:10 +00:00
|
|
|
.register_output_type::<MyInterface>()
|
2020-05-03 02:06:17 +00:00
|
|
|
.finish();
|
|
|
|
let mut stream = schema
|
2020-09-10 11:35:48 +00:00
|
|
|
.execute_stream(
|
2020-05-03 02:06:17 +00:00
|
|
|
r#"
|
|
|
|
subscription s {
|
|
|
|
events(start: 10, end: 20) {
|
|
|
|
... Frag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fragment Frag on Event {
|
|
|
|
a b
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
2021-04-04 04:05:54 +00:00
|
|
|
.map(|resp| resp.data);
|
2020-05-03 02:06:17 +00:00
|
|
|
for i in 10..20 {
|
|
|
|
assert_eq!(
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({ "events": {"a": i, "b": i * 10} }),
|
2020-10-10 02:32:43 +00:00
|
|
|
stream.next().await.unwrap()
|
2020-05-03 02:06:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
assert!(stream.next().await.is_none());
|
|
|
|
}
|
2020-05-03 08:02:46 +00:00
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-05-03 08:02:46 +00:00
|
|
|
pub async fn test_subscription_error() {
|
|
|
|
struct Event {
|
|
|
|
value: i32,
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2020-05-03 08:02:46 +00:00
|
|
|
impl Event {
|
Rework errors
This completely overhauls the error system used in async-graphql.
- `Error` has been renamed to `ServerError` and `FieldError` has been
renamed to just `Error`. This is because `FieldError` is by far the most
common error that users will have to use so it makes sense to use the
most obvious error name. Also, the current name didn't make sense as it
was used for things other than field errors, such as the data callback
for websockets.
- `ServerError` has been made completely opaque. Before it was an enum
of all the possible errors, but now it just contains an error message,
the locations, the path and extensions. It is a shame that we lose
information, it makes more sense as _conceptually_ GraphQL does not
provide that information. It also frees us to change the internals of
async-graphql a lot more.
- The path of errors is no longer an opaque JSON value but a regular
type, `Vec<PathSegment>`. The type duplication of `PathSegment` and
`QueryPathSegment` is unfortunate, I plan to work on this in the future.
- Now that `ServerError` is opaque, `RuleError` has been removed from
the public API, making it simpler.
- Additionally `QueryError` has been completely removed. Instead the
error messages are constructed ad-hoc; I took care to never repeat an
error message.
- Instead of constructing field-not-found errors inside the
implementations of field resolvers they now return `Option`s, where a
`None` value is representative of the field not being found.
- As an unfortunate consequence of the last change, self-referential
types based on the output of a subscription resolver can no longer be
created. This does not mean anything for users, but causes lifetime
issues in the implementation of merged objects. I fixed it with a bit of
a hack, but this'll have to be looked into further.
- `InputValueError` now has a generic parameter - it's kind of weird but
it's necessary for ergonomics. It also improves error messages.
- The `ErrorExtensions` trait has been removed. I didn't think the
`extend` method was necessary since `From` impls exist. But the
ergonomics are still there with a new trait `ExtendError`, which
is implemented for both errors and results.
- `Response` now supports serializing multiple errors. This allows for
nice things like having multiple validation errors not be awkwardly
shoved into a single error.
- When an error occurs in execution, data is sent as `null`. This is
slightly more compliant with the spec but the algorithm described in
<https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has
yet to be implemented.
2020-09-29 19:06:44 +00:00
|
|
|
async fn value(&self) -> Result<i32> {
|
2021-12-07 03:00:53 +00:00
|
|
|
if self.value != 5 {
|
2020-05-03 08:02:46 +00:00
|
|
|
Ok(self.value)
|
|
|
|
} else {
|
|
|
|
Err("TestError".into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Subscription;
|
2020-05-03 08:02:46 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Subscription]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Subscription {
|
2020-05-03 08:02:46 +00:00
|
|
|
async fn events(&self) -> impl Stream<Item = Event> {
|
2020-10-16 06:49:22 +00:00
|
|
|
futures_util::stream::iter((0..10).map(|n| Event { value: n }))
|
2020-05-03 08:02:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
let schema = Schema::new(Query, EmptyMutation, Subscription);
|
2020-05-03 08:02:46 +00:00
|
|
|
let mut stream = schema
|
2020-09-10 11:35:48 +00:00
|
|
|
.execute_stream("subscription { events { value } }")
|
|
|
|
.map(|resp| resp.into_result())
|
2021-04-04 04:05:54 +00:00
|
|
|
.map_ok(|resp| resp.data);
|
2021-12-07 03:00:53 +00:00
|
|
|
|
2020-05-03 08:02:46 +00:00
|
|
|
for i in 0i32..5 {
|
|
|
|
assert_eq!(
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({ "events": { "value": i } }),
|
2020-10-10 02:32:43 +00:00
|
|
|
stream.next().await.unwrap().unwrap()
|
2020-05-03 08:02:46 +00:00
|
|
|
);
|
|
|
|
}
|
2021-12-07 03:00:53 +00:00
|
|
|
|
2020-05-03 08:02:46 +00:00
|
|
|
assert_eq!(
|
|
|
|
stream.next().await,
|
Rework errors
This completely overhauls the error system used in async-graphql.
- `Error` has been renamed to `ServerError` and `FieldError` has been
renamed to just `Error`. This is because `FieldError` is by far the most
common error that users will have to use so it makes sense to use the
most obvious error name. Also, the current name didn't make sense as it
was used for things other than field errors, such as the data callback
for websockets.
- `ServerError` has been made completely opaque. Before it was an enum
of all the possible errors, but now it just contains an error message,
the locations, the path and extensions. It is a shame that we lose
information, it makes more sense as _conceptually_ GraphQL does not
provide that information. It also frees us to change the internals of
async-graphql a lot more.
- The path of errors is no longer an opaque JSON value but a regular
type, `Vec<PathSegment>`. The type duplication of `PathSegment` and
`QueryPathSegment` is unfortunate, I plan to work on this in the future.
- Now that `ServerError` is opaque, `RuleError` has been removed from
the public API, making it simpler.
- Additionally `QueryError` has been completely removed. Instead the
error messages are constructed ad-hoc; I took care to never repeat an
error message.
- Instead of constructing field-not-found errors inside the
implementations of field resolvers they now return `Option`s, where a
`None` value is representative of the field not being found.
- As an unfortunate consequence of the last change, self-referential
types based on the output of a subscription resolver can no longer be
created. This does not mean anything for users, but causes lifetime
issues in the implementation of merged objects. I fixed it with a bit of
a hack, but this'll have to be looked into further.
- `InputValueError` now has a generic parameter - it's kind of weird but
it's necessary for ergonomics. It also improves error messages.
- The `ErrorExtensions` trait has been removed. I didn't think the
`extend` method was necessary since `From` impls exist. But the
ergonomics are still there with a new trait `ExtendError`, which
is implemented for both errors and results.
- `Response` now supports serializing multiple errors. This allows for
nice things like having multiple validation errors not be awkwardly
shoved into a single error.
- When an error occurs in execution, data is sent as `null`. This is
slightly more compliant with the spec but the algorithm described in
<https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has
yet to be implemented.
2020-09-29 19:06:44 +00:00
|
|
|
Some(Err(vec![ServerError {
|
|
|
|
message: "TestError".to_string(),
|
2021-11-07 11:11:17 +00:00
|
|
|
source: None,
|
Rework errors
This completely overhauls the error system used in async-graphql.
- `Error` has been renamed to `ServerError` and `FieldError` has been
renamed to just `Error`. This is because `FieldError` is by far the most
common error that users will have to use so it makes sense to use the
most obvious error name. Also, the current name didn't make sense as it
was used for things other than field errors, such as the data callback
for websockets.
- `ServerError` has been made completely opaque. Before it was an enum
of all the possible errors, but now it just contains an error message,
the locations, the path and extensions. It is a shame that we lose
information, it makes more sense as _conceptually_ GraphQL does not
provide that information. It also frees us to change the internals of
async-graphql a lot more.
- The path of errors is no longer an opaque JSON value but a regular
type, `Vec<PathSegment>`. The type duplication of `PathSegment` and
`QueryPathSegment` is unfortunate, I plan to work on this in the future.
- Now that `ServerError` is opaque, `RuleError` has been removed from
the public API, making it simpler.
- Additionally `QueryError` has been completely removed. Instead the
error messages are constructed ad-hoc; I took care to never repeat an
error message.
- Instead of constructing field-not-found errors inside the
implementations of field resolvers they now return `Option`s, where a
`None` value is representative of the field not being found.
- As an unfortunate consequence of the last change, self-referential
types based on the output of a subscription resolver can no longer be
created. This does not mean anything for users, but causes lifetime
issues in the implementation of merged objects. I fixed it with a bit of
a hack, but this'll have to be looked into further.
- `InputValueError` now has a generic parameter - it's kind of weird but
it's necessary for ergonomics. It also improves error messages.
- The `ErrorExtensions` trait has been removed. I didn't think the
`extend` method was necessary since `From` impls exist. But the
ergonomics are still there with a new trait `ExtendError`, which
is implemented for both errors and results.
- `Response` now supports serializing multiple errors. This allows for
nice things like having multiple validation errors not be awkwardly
shoved into a single error.
- When an error occurs in execution, data is sent as `null`. This is
slightly more compliant with the spec but the algorithm described in
<https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has
yet to be implemented.
2020-09-29 19:06:44 +00:00
|
|
|
locations: vec![Pos {
|
2020-05-03 08:02:46 +00:00
|
|
|
line: 1,
|
|
|
|
column: 25
|
Rework errors
This completely overhauls the error system used in async-graphql.
- `Error` has been renamed to `ServerError` and `FieldError` has been
renamed to just `Error`. This is because `FieldError` is by far the most
common error that users will have to use so it makes sense to use the
most obvious error name. Also, the current name didn't make sense as it
was used for things other than field errors, such as the data callback
for websockets.
- `ServerError` has been made completely opaque. Before it was an enum
of all the possible errors, but now it just contains an error message,
the locations, the path and extensions. It is a shame that we lose
information, it makes more sense as _conceptually_ GraphQL does not
provide that information. It also frees us to change the internals of
async-graphql a lot more.
- The path of errors is no longer an opaque JSON value but a regular
type, `Vec<PathSegment>`. The type duplication of `PathSegment` and
`QueryPathSegment` is unfortunate, I plan to work on this in the future.
- Now that `ServerError` is opaque, `RuleError` has been removed from
the public API, making it simpler.
- Additionally `QueryError` has been completely removed. Instead the
error messages are constructed ad-hoc; I took care to never repeat an
error message.
- Instead of constructing field-not-found errors inside the
implementations of field resolvers they now return `Option`s, where a
`None` value is representative of the field not being found.
- As an unfortunate consequence of the last change, self-referential
types based on the output of a subscription resolver can no longer be
created. This does not mean anything for users, but causes lifetime
issues in the implementation of merged objects. I fixed it with a bit of
a hack, but this'll have to be looked into further.
- `InputValueError` now has a generic parameter - it's kind of weird but
it's necessary for ergonomics. It also improves error messages.
- The `ErrorExtensions` trait has been removed. I didn't think the
`extend` method was necessary since `From` impls exist. But the
ergonomics are still there with a new trait `ExtendError`, which
is implemented for both errors and results.
- `Response` now supports serializing multiple errors. This allows for
nice things like having multiple validation errors not be awkwardly
shoved into a single error.
- When an error occurs in execution, data is sent as `null`. This is
slightly more compliant with the spec but the algorithm described in
<https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has
yet to be implemented.
2020-09-29 19:06:44 +00:00
|
|
|
}],
|
|
|
|
path: vec![
|
|
|
|
PathSegment::Field("events".to_owned()),
|
|
|
|
PathSegment::Field("value".to_owned())
|
|
|
|
],
|
|
|
|
extensions: None,
|
|
|
|
}]))
|
2020-05-03 08:02:46 +00:00
|
|
|
);
|
2020-05-03 13:21:54 +00:00
|
|
|
|
2021-12-07 03:00:53 +00:00
|
|
|
for i in 6i32..10 {
|
|
|
|
assert_eq!(
|
|
|
|
value!({ "events": { "value": i } }),
|
|
|
|
stream.next().await.unwrap().unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-03 13:21:54 +00:00
|
|
|
assert!(stream.next().await.is_none());
|
2020-05-03 08:02:46 +00:00
|
|
|
}
|
2020-05-03 14:32:37 +00:00
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-05-03 14:32:37 +00:00
|
|
|
pub async fn test_subscription_fieldresult() {
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Subscription;
|
2020-05-03 14:32:37 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Subscription]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Subscription {
|
Rework errors
This completely overhauls the error system used in async-graphql.
- `Error` has been renamed to `ServerError` and `FieldError` has been
renamed to just `Error`. This is because `FieldError` is by far the most
common error that users will have to use so it makes sense to use the
most obvious error name. Also, the current name didn't make sense as it
was used for things other than field errors, such as the data callback
for websockets.
- `ServerError` has been made completely opaque. Before it was an enum
of all the possible errors, but now it just contains an error message,
the locations, the path and extensions. It is a shame that we lose
information, it makes more sense as _conceptually_ GraphQL does not
provide that information. It also frees us to change the internals of
async-graphql a lot more.
- The path of errors is no longer an opaque JSON value but a regular
type, `Vec<PathSegment>`. The type duplication of `PathSegment` and
`QueryPathSegment` is unfortunate, I plan to work on this in the future.
- Now that `ServerError` is opaque, `RuleError` has been removed from
the public API, making it simpler.
- Additionally `QueryError` has been completely removed. Instead the
error messages are constructed ad-hoc; I took care to never repeat an
error message.
- Instead of constructing field-not-found errors inside the
implementations of field resolvers they now return `Option`s, where a
`None` value is representative of the field not being found.
- As an unfortunate consequence of the last change, self-referential
types based on the output of a subscription resolver can no longer be
created. This does not mean anything for users, but causes lifetime
issues in the implementation of merged objects. I fixed it with a bit of
a hack, but this'll have to be looked into further.
- `InputValueError` now has a generic parameter - it's kind of weird but
it's necessary for ergonomics. It also improves error messages.
- The `ErrorExtensions` trait has been removed. I didn't think the
`extend` method was necessary since `From` impls exist. But the
ergonomics are still there with a new trait `ExtendError`, which
is implemented for both errors and results.
- `Response` now supports serializing multiple errors. This allows for
nice things like having multiple validation errors not be awkwardly
shoved into a single error.
- When an error occurs in execution, data is sent as `null`. This is
slightly more compliant with the spec but the algorithm described in
<https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has
yet to be implemented.
2020-09-29 19:06:44 +00:00
|
|
|
async fn values(&self) -> impl Stream<Item = Result<i32>> {
|
2020-10-16 06:49:22 +00:00
|
|
|
futures_util::stream::iter(0..5)
|
Rework errors
This completely overhauls the error system used in async-graphql.
- `Error` has been renamed to `ServerError` and `FieldError` has been
renamed to just `Error`. This is because `FieldError` is by far the most
common error that users will have to use so it makes sense to use the
most obvious error name. Also, the current name didn't make sense as it
was used for things other than field errors, such as the data callback
for websockets.
- `ServerError` has been made completely opaque. Before it was an enum
of all the possible errors, but now it just contains an error message,
the locations, the path and extensions. It is a shame that we lose
information, it makes more sense as _conceptually_ GraphQL does not
provide that information. It also frees us to change the internals of
async-graphql a lot more.
- The path of errors is no longer an opaque JSON value but a regular
type, `Vec<PathSegment>`. The type duplication of `PathSegment` and
`QueryPathSegment` is unfortunate, I plan to work on this in the future.
- Now that `ServerError` is opaque, `RuleError` has been removed from
the public API, making it simpler.
- Additionally `QueryError` has been completely removed. Instead the
error messages are constructed ad-hoc; I took care to never repeat an
error message.
- Instead of constructing field-not-found errors inside the
implementations of field resolvers they now return `Option`s, where a
`None` value is representative of the field not being found.
- As an unfortunate consequence of the last change, self-referential
types based on the output of a subscription resolver can no longer be
created. This does not mean anything for users, but causes lifetime
issues in the implementation of merged objects. I fixed it with a bit of
a hack, but this'll have to be looked into further.
- `InputValueError` now has a generic parameter - it's kind of weird but
it's necessary for ergonomics. It also improves error messages.
- The `ErrorExtensions` trait has been removed. I didn't think the
`extend` method was necessary since `From` impls exist. But the
ergonomics are still there with a new trait `ExtendError`, which
is implemented for both errors and results.
- `Response` now supports serializing multiple errors. This allows for
nice things like having multiple validation errors not be awkwardly
shoved into a single error.
- When an error occurs in execution, data is sent as `null`. This is
slightly more compliant with the spec but the algorithm described in
<https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has
yet to be implemented.
2020-09-29 19:06:44 +00:00
|
|
|
.map(Result::Ok)
|
2020-10-16 10:37:59 +00:00
|
|
|
.chain(futures_util::stream::once(async move {
|
|
|
|
Err("StreamErr".into())
|
|
|
|
}))
|
2021-12-07 03:00:53 +00:00
|
|
|
.chain(futures_util::stream::iter(5..10).map(Result::Ok))
|
2020-05-03 14:32:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
let schema = Schema::new(Query, EmptyMutation, Subscription);
|
2021-06-07 06:03:36 +00:00
|
|
|
let mut stream = schema.execute_stream("subscription { values }");
|
2021-12-07 03:00:53 +00:00
|
|
|
|
2020-05-03 14:32:37 +00:00
|
|
|
for i in 0i32..5 {
|
|
|
|
assert_eq!(
|
2021-06-07 06:03:36 +00:00
|
|
|
Response::new(value!({ "values": i })),
|
|
|
|
stream.next().await.unwrap()
|
2020-05-03 14:32:37 +00:00
|
|
|
);
|
|
|
|
}
|
2021-12-07 03:00:53 +00:00
|
|
|
|
2020-05-03 14:32:37 +00:00
|
|
|
assert_eq!(
|
2021-06-07 06:03:36 +00:00
|
|
|
Response {
|
2021-06-07 12:51:20 +00:00
|
|
|
data: Value::Null,
|
2021-06-07 06:03:36 +00:00
|
|
|
extensions: Default::default(),
|
|
|
|
cache_control: Default::default(),
|
|
|
|
errors: vec![ServerError {
|
|
|
|
message: "StreamErr".to_string(),
|
2021-11-07 11:11:17 +00:00
|
|
|
source: None,
|
2021-06-07 06:03:36 +00:00
|
|
|
locations: vec![Pos {
|
|
|
|
line: 1,
|
|
|
|
column: 16
|
|
|
|
}],
|
|
|
|
path: vec![PathSegment::Field("values".to_owned())],
|
|
|
|
extensions: None,
|
Rework errors
This completely overhauls the error system used in async-graphql.
- `Error` has been renamed to `ServerError` and `FieldError` has been
renamed to just `Error`. This is because `FieldError` is by far the most
common error that users will have to use so it makes sense to use the
most obvious error name. Also, the current name didn't make sense as it
was used for things other than field errors, such as the data callback
for websockets.
- `ServerError` has been made completely opaque. Before it was an enum
of all the possible errors, but now it just contains an error message,
the locations, the path and extensions. It is a shame that we lose
information, it makes more sense as _conceptually_ GraphQL does not
provide that information. It also frees us to change the internals of
async-graphql a lot more.
- The path of errors is no longer an opaque JSON value but a regular
type, `Vec<PathSegment>`. The type duplication of `PathSegment` and
`QueryPathSegment` is unfortunate, I plan to work on this in the future.
- Now that `ServerError` is opaque, `RuleError` has been removed from
the public API, making it simpler.
- Additionally `QueryError` has been completely removed. Instead the
error messages are constructed ad-hoc; I took care to never repeat an
error message.
- Instead of constructing field-not-found errors inside the
implementations of field resolvers they now return `Option`s, where a
`None` value is representative of the field not being found.
- As an unfortunate consequence of the last change, self-referential
types based on the output of a subscription resolver can no longer be
created. This does not mean anything for users, but causes lifetime
issues in the implementation of merged objects. I fixed it with a bit of
a hack, but this'll have to be looked into further.
- `InputValueError` now has a generic parameter - it's kind of weird but
it's necessary for ergonomics. It also improves error messages.
- The `ErrorExtensions` trait has been removed. I didn't think the
`extend` method was necessary since `From` impls exist. But the
ergonomics are still there with a new trait `ExtendError`, which
is implemented for both errors and results.
- `Response` now supports serializing multiple errors. This allows for
nice things like having multiple validation errors not be awkwardly
shoved into a single error.
- When an error occurs in execution, data is sent as `null`. This is
slightly more compliant with the spec but the algorithm described in
<https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has
yet to be implemented.
2020-09-29 19:06:44 +00:00
|
|
|
}],
|
2021-06-07 06:03:36 +00:00
|
|
|
http_headers: Default::default()
|
|
|
|
},
|
|
|
|
stream.next().await.unwrap(),
|
2020-05-03 14:32:37 +00:00
|
|
|
);
|
|
|
|
|
2021-12-07 03:00:53 +00:00
|
|
|
for i in 5i32..10 {
|
|
|
|
assert_eq!(
|
|
|
|
Response::new(value!({ "values": i })),
|
|
|
|
stream.next().await.unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-03 14:32:37 +00:00
|
|
|
assert!(stream.next().await.is_none());
|
|
|
|
}
|