Struct diana::Publisher [−][src]
pub struct Publisher { /* fields omitted */ }
Expand description
Implementations
Creates a new publisher. This is done for you when you create the queries/mutations system, so you should never need to call this.
Sends the given data to the subscriptions server on the given channel. In-depth information about this process is available in the book. You should use serde to serialize anything sent here as a string (this won’t be done for you). It should then be deserialized in the appropriate subscription (which will listen for messages from here indirectly). This function will return an error if the subscriptions server was unavailable or didn’t correctly acknowledge the request.
Example
use diana::{ async_graphql::{Object as GQLObject, InputObject as GQLInputObject, SimpleObject as GQLSimpleObject}, errors::GQLResult, Publisher, }; use serde::Serialize; #[derive(Serialize, GQLSimpleObject)] struct User { username: String, } #[derive(Serialize, GQLInputObject)] struct UserInput { username: String, } #[derive(Default, Clone)] pub struct Mutation {} #[GQLObject] impl Mutation { async fn add_user( &self, ctx: &async_graphql::Context<'_>, new_user: UserInput, ) -> GQLResult<User> { // Your code to add the new user // Notify the subscriptions server that a new user has been added let publisher = ctx.data::<Publisher>()?; let user_json = serde_json::to_string(&new_user).unwrap(); // GraphQL has already checked for ill-formation publisher.publish("new_user", user_json.to_string()).await?; Ok(User { username: new_user.username }) // In reality, you'd probably return the user that's just been created } }
Auto Trait Implementations
impl !RefUnwindSafe for Publisher
impl !UnwindSafe for Publisher