Struct diana::Publisher[][src]

pub struct Publisher { /* fields omitted */ }
Expand description

The system that publishes data from the queries/mutations system to the subscriptions server. These communications are secured by a JWT specified in Options. This is automatically created from the Options and passed to all resolvers. You should never need to manually create it.

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

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.