Struct async_graphql_warp::GraphQLWebSocket
source · [−]pub struct GraphQLWebSocket<Sink, Stream, Query, Mutation, Subscription, OnInit> { /* private fields */ }
Expand description
A Websocket connection for GraphQL subscription.
Examples
use std::time::Duration;
use async_graphql::*;
use async_graphql_warp::*;
use futures_util::stream::{Stream, StreamExt};
use warp::{ws, Filter};
struct QueryRoot;
#[Object]
impl QueryRoot {
async fn value(&self) -> i32 {
// A GraphQL Object type must define one or more fields.
100
}
}
struct SubscriptionRoot;
#[Subscription]
impl SubscriptionRoot {
async fn tick(&self) -> impl Stream<Item = String> {
async_stream::stream! {
let mut interval = tokio::time::interval(Duration::from_secs(1));
loop {
let n = interval.tick().await;
yield format!("{}", n.elapsed().as_secs_f32());
}
}
}
}
let schema = Schema::new(QueryRoot, EmptyMutation, SubscriptionRoot);
let filter = warp::ws()
.and(graphql_protocol())
.map(move |ws: ws::Ws, protocol| {
let schema = schema.clone();
let reply = ws
.on_upgrade(move |socket| GraphQLWebSocket::new(socket, schema, protocol).serve());
warp::reply::with_header(
reply,
"Sec-WebSocket-Protocol",
protocol.sec_websocket_protocol(),
)
});
warp::serve(filter).run(([0, 0, 0, 0], 8000)).await;
Implementations
sourceimpl<S, Query, Mutation, Subscription> GraphQLWebSocket<SplitSink<S, Message>, SplitStream<S>, Query, Mutation, Subscription, fn(_: Value) -> Ready<Result<Data>>> where
S: Stream<Item = Result<Message, Error>> + Sink<Message>,
Query: ObjectType + 'static,
Mutation: ObjectType + 'static,
Subscription: SubscriptionType + 'static,
impl<S, Query, Mutation, Subscription> GraphQLWebSocket<SplitSink<S, Message>, SplitStream<S>, Query, Mutation, Subscription, fn(_: Value) -> Ready<Result<Data>>> where
S: Stream<Item = Result<Message, Error>> + Sink<Message>,
Query: ObjectType + 'static,
Mutation: ObjectType + 'static,
Subscription: SubscriptionType + 'static,
sourcepub fn new(
socket: S,
schema: Schema<Query, Mutation, Subscription>,
protocol: WebSocketProtocols
) -> Self
pub fn new(
socket: S,
schema: Schema<Query, Mutation, Subscription>,
protocol: WebSocketProtocols
) -> Self
Create a GraphQLWebSocket
object.
sourceimpl<Sink, Stream, Query, Mutation, Subscription> GraphQLWebSocket<Sink, Stream, Query, Mutation, Subscription, fn(_: Value) -> Ready<Result<Data>>> where
Sink: Sink<Message>,
Stream: Stream<Item = Result<Message, Error>>,
Query: ObjectType + 'static,
Mutation: ObjectType + 'static,
Subscription: SubscriptionType + 'static,
impl<Sink, Stream, Query, Mutation, Subscription> GraphQLWebSocket<Sink, Stream, Query, Mutation, Subscription, fn(_: Value) -> Ready<Result<Data>>> where
Sink: Sink<Message>,
Stream: Stream<Item = Result<Message, Error>>,
Query: ObjectType + 'static,
Mutation: ObjectType + 'static,
Subscription: SubscriptionType + 'static,
sourcepub fn new_with_pair(
sink: Sink,
stream: Stream,
schema: Schema<Query, Mutation, Subscription>,
protocol: WebSocketProtocols
) -> Self
pub fn new_with_pair(
sink: Sink,
stream: Stream,
schema: Schema<Query, Mutation, Subscription>,
protocol: WebSocketProtocols
) -> Self
Create a GraphQLWebSocket
object with sink and stream objects.
sourceimpl<Sink, Stream, Query, Mutation, Subscription, OnConnInit, OnConnInitFut> GraphQLWebSocket<Sink, Stream, Query, Mutation, Subscription, OnConnInit> where
Sink: Sink<Message>,
Stream: Stream<Item = Result<Message, Error>>,
Query: ObjectType + 'static,
Mutation: ObjectType + 'static,
Subscription: SubscriptionType + 'static,
OnConnInit: FnOnce(Value) -> OnConnInitFut + Send + 'static,
OnConnInitFut: Future<Output = Result<Data>> + Send + 'static,
impl<Sink, Stream, Query, Mutation, Subscription, OnConnInit, OnConnInitFut> GraphQLWebSocket<Sink, Stream, Query, Mutation, Subscription, OnConnInit> where
Sink: Sink<Message>,
Stream: Stream<Item = Result<Message, Error>>,
Query: ObjectType + 'static,
Mutation: ObjectType + 'static,
Subscription: SubscriptionType + 'static,
OnConnInit: FnOnce(Value) -> OnConnInitFut + Send + 'static,
OnConnInitFut: Future<Output = Result<Data>> + Send + 'static,
sourcepub fn with_data(self, data: Data) -> Self
pub fn with_data(self, data: Data) -> Self
Specify the initial subscription context data, usually you can get something from the incoming request to create it.
sourcepub fn on_connection_init<OnConnInit2, Fut>(
self,
callback: OnConnInit2
) -> GraphQLWebSocket<Sink, Stream, Query, Mutation, Subscription, OnConnInit2> where
OnConnInit2: FnOnce(Value) -> Fut + Send + 'static,
Fut: Future<Output = Result<Data>> + Send + 'static,
pub fn on_connection_init<OnConnInit2, Fut>(
self,
callback: OnConnInit2
) -> GraphQLWebSocket<Sink, Stream, Query, Mutation, Subscription, OnConnInit2> where
OnConnInit2: FnOnce(Value) -> Fut + Send + 'static,
Fut: Future<Output = Result<Data>> + Send + 'static,
Specify a callback function to be called when the connection is initialized.
You can get something from the payload of GQL_CONNECTION_INIT
message to create Data
.
The data returned by this callback function will be merged with the data
specified by [with_data
].
Auto Trait Implementations
impl<Sink, Stream, Query, Mutation, Subscription, OnInit> !RefUnwindSafe for GraphQLWebSocket<Sink, Stream, Query, Mutation, Subscription, OnInit>
impl<Sink, Stream, Query, Mutation, Subscription, OnInit> Send for GraphQLWebSocket<Sink, Stream, Query, Mutation, Subscription, OnInit> where
Mutation: Send + Sync,
OnInit: Send,
Query: Send + Sync,
Sink: Send,
Stream: Send,
Subscription: Send + Sync,
impl<Sink, Stream, Query, Mutation, Subscription, OnInit> Sync for GraphQLWebSocket<Sink, Stream, Query, Mutation, Subscription, OnInit> where
Mutation: Send + Sync,
OnInit: Sync,
Query: Send + Sync,
Sink: Sync,
Stream: Sync,
Subscription: Send + Sync,
impl<Sink, Stream, Query, Mutation, Subscription, OnInit> Unpin for GraphQLWebSocket<Sink, Stream, Query, Mutation, Subscription, OnInit> where
OnInit: Unpin,
Sink: Unpin,
Stream: Unpin,
impl<Sink, Stream, Query, Mutation, Subscription, OnInit> !UnwindSafe for GraphQLWebSocket<Sink, Stream, Query, Mutation, Subscription, OnInit>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more