#[graphql_subscription]
Expand description
#[graphql_subscription]
macro for generating a GraphQL subscription
implementation for structs with computable field resolvers (declared via
a regular Rust impl
block).
It enables you to write GraphQL field resolvers for a type by declaring a
regular Rust impl
block. Under the hood, the macro implements
GraphQLType
/GraphQLSubscriptionValue
traits.
Specifying multiple #[graphql_subscription]
attributes on the same
definition is totally okay. They all will be treated as a single attribute.
This macro is similar to #[graphql_object]
macro
and has all its properties, but requires methods to be async
and return
Stream
of values instead of a value itself.
use juniper::graphql_subscription;
// We can declare the type as a plain struct without any members.
struct Subscription;
#[graphql_subscription]
impl Subscription {
// WARNING: Only GraphQL fields can be specified in this `impl` block.
// If normal methods are required on the struct, they can be
// defined either in a separate "normal" `impl` block, or
// marked with `#[graphql(ignore)]` attribute.
// This defines a simple, static field which does not require any
// context.
// Such field can return a `Stream` of any value implementing
// `GraphQLType` and `GraphQLValue` traits.
//
// NOTICE: Method must be `async`.
async fn api_version() -> BoxStream<'static, &'static str> {
Box::pin(stream::once(async { "0.1" }))
}
}