2020-04-17 01:16:00 -05:00
# Subscriptions
### How to achieve realtime data with GraphQL subscriptions
GraphQL subscriptions are a way to push data from the server to clients requesting real-time messages
from the server. Subscriptions are similar to queries in that they specify a set of fields to be delivered to the client,
2020-07-15 02:02:30 -05:00
but instead of immediately returning a single answer a result is sent every time a particular event happens on the
2020-04-17 01:16:00 -05:00
server.
In order to execute subscriptions you need a coordinator (that spawns connections)
and a GraphQL object that can be resolved into a stream--elements of which will then
2020-07-15 02:02:30 -05:00
be returned to the end user. The [`juniper_subscriptions`][juniper_subscriptions] crate
2020-04-17 01:16:00 -05:00
provides a default connection implementation. Currently subscriptions are only supported on the `master` branch. Add the following to your `Cargo.toml` :
```toml
[dependencies]
juniper = { git = "https://github.com/graphql-rust/juniper", branch = "master" }
juniper_subscriptions = { git = "https://github.com/graphql-rust/juniper", branch = "master" }
```
### Schema Definition
2020-07-15 02:02:30 -05:00
The `Subscription` is just a GraphQL object, similar to the query root and mutations object that you defined for the
operations in your [Schema][Schema]. For subscriptions all fields/operations should be async and should return a [Stream][Stream].
2020-04-17 01:16:00 -05:00
This example shows a subscription operation that returns two events, the strings `Hello` and `World!`
sequentially:
```rust
2020-07-18 20:24:33 -05:00
# extern crate futures;
# extern crate juniper;
# extern crate juniper_subscriptions;
# extern crate tokio;
2020-11-06 20:15:18 -06:00
# use juniper::{graphql_object, graphql_subscription, FieldError};
2020-07-18 20:24:33 -05:00
# use futures::Stream;
2020-04-17 01:16:00 -05:00
# use std::pin::Pin;
2020-07-18 20:24:33 -05:00
#
2020-04-17 01:16:00 -05:00
# #[derive(Clone)]
# pub struct Database;
# impl juniper::Context for Database {}
2020-07-18 20:24:33 -05:00
2020-04-17 01:16:00 -05:00
# pub struct Query;
2020-11-06 20:15:18 -06:00
# #[graphql_object(context = Database)]
2020-04-17 01:16:00 -05:00
# impl Query {
# fn hello_world() -> &str {
# "Hello World!"
# }
# }
pub struct Subscription;
type StringStream = Pin< Box < dyn Stream < Item = Result<String, FieldError > > + Send>>;
2020-11-06 20:15:18 -06:00
#[graphql_subscription(context = Database)]
2020-04-17 01:16:00 -05:00
impl Subscription {
async fn hello_world() -> StringStream {
let stream = tokio::stream::iter(vec![
Ok(String::from("Hello")),
Ok(String::from("World!"))
]);
Box::pin(stream)
}
}
2020-11-06 20:15:18 -06:00
#
2020-04-17 01:16:00 -05:00
# fn main () {}
2020-11-06 20:15:18 -06:00
```
2020-04-17 01:16:00 -05:00
### Coordinator
2020-07-15 02:02:30 -05:00
Subscriptions require a bit more resources than regular queries and provide a great vector for DOS attacks. This can can bring down a server easily if not handled correctly. The [`SubscriptionCoordinator`][SubscriptionCoordinator] trait provides coordination logic to enable functionality like DOS attack mitigation and resource limits.
The [`SubscriptionCoordinator`][SubscriptionCoordinator] contains the schema and can keep track of opened connections, handle subscription
start and end, and maintain a global subscription id for each subscription. Each time a connection is established,
the [`SubscriptionCoordinator`][SubscriptionCoordinator] spawns a [`SubscriptionConnection`][SubscriptionConnection]. The [`SubscriptionConnection`][SubscriptionConnection] handles a single connection, providing resolver logic for a client stream as well as reconnection
2020-04-17 01:16:00 -05:00
and shutdown logic.
2020-07-15 02:02:30 -05:00
While you can implement [`SubscriptionCoordinator`][SubscriptionCoordinator] yourself, Juniper contains a simple and generic implementation called [`Coordinator`][Coordinator]. The `subscribe`
operation returns a [`Future`][Future] with an `Item` value of a `Result<Connection, GraphQLError>` ,
where [`Connection`][Connection] is a `Stream` of values returned by the operation and [`GraphQLError`][GraphQLError] is the error when the subscription fails.
2020-04-17 01:16:00 -05:00
```rust
Make interfaces great again! (#682)
* Bootstrap
* Upd
* Bootstrap macro
* Revert stuff
* Correct PoC to compile
* Bootstrap #[graphql_interface] expansion
* Bootstrap #[graphql_interface] meta parsing
* Bootstrap #[graphql_interface] very basic code generation [skip ci]
* Upd trait code generation and fix keywords usage [skip ci]
* Expand trait impls [skip ci]
* Tune up objects [skip ci]
* Finally! Complies at least... [skip ci]
* Parse meta for fields and its arguments [skip ci]
- also, refactor and bikeshed new macros code
* Impl filling fields meta and bootstrap field resolution [skip ci]
* Poking with fields resolution [skip ci]
* Solve Rust's teen async HRTB problems [skip ci]
* Start parsing trait methods [skip ci]
* Finish parsing fields from trait methods [skip ci]
* Autodetect trait asyncness and allow to specify it [skip ci]
* Allow to autogenerate trait object alias via attribute
* Support generics in trait definition and asyncify them correctly
* Temporary disable explicit async
* Cover arguments and custom names/descriptions in tests
* Re-enable tests with explicit async and fix the codegen to satisfy it
* Check implementers are registered in schema and vice versa
* Check argument camelCases
* Test argument defaults, and allow Into coercions for them
* Re-enable markers
* Re-enable markers and relax Sized requirement on IsInputType/IsOutputType marker traits
* Revert 'juniper_actix' fmt
* Fix missing marks for object
* Fix subscriptions marks
* Deduce result type correctly via traits
* Final fixes
* Fmt
* Restore marks checking
* Support custom ScalarValue
* Cover deprecations with tests
* Impl dowcasting via methods
* Impl dowcasting via external functions
* Support custom context, vol. 1
* Support custom context, vol. 2
* Cover fallible field with test
* Impl explicit generic ScalarValue, vol.1
* Impl explicit generic ScalarValue, vol.2
* Allow passing executor into methods
* Generating enum, vol.1
* Generating enum, vol.2
* Generating enum, vol.3
* Generating enum, vol.3
* Generating enum, vol.4
* Generating enum, vol.5
* Generating enum, vol.6
* Generating enum, vol.7
* Generating enum, vol.8
* Refactor juniper stuff
* Fix juniper tests, vol.1
* Fix juniper tests, vol.2
* Polish 'juniper' crate changes, vol.1
* Polish 'juniper' crate changes, vol.2
* Remove redundant stuf
* Polishing 'juniper_codegen', vol.1
* Polishing 'juniper_codegen', vol.2
* Polishing 'juniper_codegen', vol.3
* Polishing 'juniper_codegen', vol.4
* Polishing 'juniper_codegen', vol.5
* Polishing 'juniper_codegen', vol.6
* Polishing 'juniper_codegen', vol.7
* Polishing 'juniper_codegen', vol.8
* Polishing 'juniper_codegen', vol.9
* Fix other crates tests and make Clippy happier
* Fix examples
* Add codegen failure tests, vol. 1
* Add codegen failure tests, vol. 2
* Add codegen failure tests, vol.3
* Fix codegen failure tests accordingly to latest nightly Rust
* Fix codegen when interface has no implementers
* Fix warnings in book tests
* Describing new interfaces in Book, vol.1
Co-authored-by: Christian Legnitto <LegNeato@users.noreply.github.com>
2020-10-06 02:21:01 -05:00
# #![allow(dead_code)]
2020-07-18 20:24:33 -05:00
# extern crate futures;
# extern crate juniper;
# extern crate juniper_subscriptions;
# extern crate serde_json;
# extern crate tokio;
2020-11-06 20:15:18 -06:00
# use juniper::{
# http::GraphQLRequest,
# graphql_object, graphql_subscription,
# DefaultScalarValue, EmptyMutation, FieldError,
# RootNode, SubscriptionCoordinator,
# };
2020-04-17 01:16:00 -05:00
# use juniper_subscriptions::Coordinator;
# use futures::{Stream, StreamExt};
# use std::pin::Pin;
#
# #[derive(Clone)]
# pub struct Database;
#
# impl juniper::Context for Database {}
#
# impl Database {
# fn new() -> Self {
# Self {}
# }
# }
#
# pub struct Query;
#
2020-11-06 20:15:18 -06:00
# #[graphql_object(context = Database)]
2020-04-17 01:16:00 -05:00
# impl Query {
# fn hello_world() -> &str {
# "Hello World!"
# }
# }
#
# pub struct Subscription;
#
# type StringStream = Pin<Box<dyn Stream<Item = Result<String, FieldError>> + Send>>;
#
2020-11-06 20:15:18 -06:00
# #[graphql_subscription(context = Database)]
2020-04-17 01:16:00 -05:00
# impl Subscription {
# async fn hello_world() -> StringStream {
# let stream =
# tokio::stream::iter(vec![Ok(String::from("Hello")), Ok(String::from("World!"))]);
# Box::pin(stream)
# }
# }
type Schema = RootNode< 'static, Query, EmptyMutation< Database > , Subscription>;
fn schema() -> Schema {
Schema::new(Query {}, EmptyMutation::new(), Subscription {})
}
async fn run_subscription() {
let schema = schema();
let coordinator = Coordinator::new(schema);
let req: GraphQLRequest< DefaultScalarValue > = serde_json::from_str(
2020-11-06 20:15:18 -06:00
r#"{
2020-04-17 01:16:00 -05:00
"query": "subscription { helloWorld }"
2020-11-06 20:15:18 -06:00
}"#,
2020-04-17 01:16:00 -05:00
)
.unwrap();
let ctx = Database::new();
let mut conn = coordinator.subscribe(& req, &ctx).await.unwrap();
while let Some(result) = conn.next().await {
println!("{}", serde_json::to_string(&result).unwrap());
}
}
2020-11-06 20:15:18 -06:00
#
2020-04-17 01:16:00 -05:00
# fn main() { }
```
### Web Integration and Examples
Currently there is an example of subscriptions with [warp][warp], but it still in an alpha state.
GraphQL over [WS][WS] is not fully supported yet and is non-standard.
- [Warp Subscription Example ](https://github.com/graphql-rust/juniper/tree/master/examples/warp_subscriptions )
- [Small Example ](https://github.com/graphql-rust/juniper/tree/master/examples/basic_subscriptions )
[juniper_subscriptions]: https://github.com/graphql-rust/juniper/tree/master/juniper_subscriptions
[Stream]: https://docs.rs/futures/0.3.4/futures/stream/trait.Stream.html
<!-- TODO: Fix these links when the documentation for the `juniper_subscriptions` are defined in the docs. --->
[Coordinator]: https://docs.rs/juniper_subscriptions/0.15.0/struct.Coordinator.html
[SubscriptionCoordinator]: https://docs.rs/juniper_subscriptions/0.15.0/trait.SubscriptionCoordinator.html
[Connection]: https://docs.rs/juniper_subscriptions/0.15.0/struct.Connection.html
[SubscriptionConnection]: https://docs.rs/juniper_subscriptions/0.15.0/trait.SubscriptionConnection.html
<!-- - - -->
[Future]: https://docs.rs/futures/0.3.4/futures/future/trait.Future.html
[warp]: https://github.com/graphql-rust/juniper/tree/master/juniper_warp
[WS]: https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md
[GraphQLError]: https://docs.rs/juniper/0.14.2/juniper/enum.GraphQLError.html
[Schema]: ../schema/schemas_and_mutations.md