Skip to main content

GraphqlPlugin

Struct GraphqlPlugin 

Source
pub struct GraphqlPlugin<E> { /* private fields */ }
Expand description

Mounts an async-graphql schema (or any Executor) on an Autumn app.

ⓘ
use autumn_plugin_graphql::GraphqlPlugin;

autumn_web::app()
    .plugin(GraphqlPlugin::new(schema))   // POST/GET /graphql, /graphql/sdl, /graphql/ws
    .run()
    .await;

Configuration comes from the [graphql] section of autumn.toml (with profile layering and AUTUMN_GRAPHQL__* overrides — see GraphqlConfig); the fluent setters here are applied on top of it, so code can pin a value that operations must not change. Call config to supply the whole configuration in code and read no files.

Implementations§

Source§

impl<Q, M, S> GraphqlPlugin<Schema<Q, M, S>>
where Q: ObjectType + 'static, M: ObjectType + 'static, S: SubscriptionType + 'static,

Source

pub fn new(schema: Schema<Q, M, S>) -> Self

Mount a built schema. Static limits are enforced by the plugin’s own analysis; for async-graphql’s complexity model as well, use from_builder.

Source

pub fn from_builder(builder: SchemaBuilder<Q, M, S>) -> Self
where SchemaBuilder<Q, M, S>: Send,

Mount a schema the plugin finishes itself, so the configured limits.max_depth, limits.max_complexity and limits.max_directives are also installed as async-graphql’s own schema-level limits (defence in depth, and the only way to use its #[graphql(complexity = …)] cost model).

Source§

impl<E: Executor> GraphqlPlugin<E>

Source

pub fn from_executor(executor: E) -> Self

Mount any Executor — for example an async_graphql::dynamic::Schema built at runtime. Subscriptions are assumed supported (turn the transports off in [graphql.subscriptions] if they are not); provide the SDL with sdl_text to serve GET {path}/sdl.

Source

pub fn drain_handle(&self) -> DrainHandle

A handle that closes every open WebSocket and SSE stream of this endpoint on demand (see DrainHandle).

Source

pub fn sdl_text(self, sdl: impl Into<String>) -> Self

Supply the SDL served at GET {path}/sdl (for from_executor mounts).

Source

pub fn configure( self, apply: impl Fn(&mut GraphqlConfig) + Send + Sync + 'static, ) -> Self

Adjust the configuration in code, on top of whatever the files say.

ⓘ
GraphqlPlugin::new(schema).configure(|c| {
    c.limits.max_depth = 8;
    c.batching.enabled = true;
})
Source

pub fn config(self, config: GraphqlConfig) -> Self

Use config as the whole configuration: no autumn.toml section and no environment overrides are read. Fluent setters still apply on top.

Source

pub fn config_section(self, section: impl Into<String>) -> Self

Read a different top-level section than [graphql] — needed when an app mounts two endpoints with different settings.

Source

pub fn development(self, development: bool) -> Self

Force development (true) or production (false) defaults for the auto settings, instead of deriving them from the active profile.

Source

pub fn path(self, path: impl Into<String>) -> Self

Mount under a different path (for example /api/graphql).

Source

pub fn introspection(self, enabled: bool) -> Self

Allow or forbid introspection regardless of profile.

Source

pub fn without_sdl(self) -> Self

Do not serve GET {path}/sdl.

Source

pub fn mask_unexpected_errors(self, mask: bool) -> Self

Mask (or stop masking) uncoded resolver errors regardless of profile.

Source

pub fn timeout(self, timeout: Duration) -> Self

Per-operation execution timeout (Duration::ZERO = none).

Source

pub fn batching(self, max_operations: usize) -> Self

Accept batched requests of up to max_operations operations.

Source

pub fn persisted_queries(self, mode: PersistedQueryMode) -> Self

Set the persisted-query mode.

Source

pub fn trusted_documents(self, documents: TrustedDocuments) -> Self

Add trusted documents in code (merged with any manifest file).

Source

pub fn persisted_query_store(self, store: impl PersistedQueryStore) -> Self

Replace the in-memory APQ cache (e.g. with a Redis-backed store shared by every instance).

Source

pub fn context<F>(self, hook: F) -> Self
where F: for<'a> Fn(&'a mut Parts, &'a AppState, &'a mut Data) -> BoxFuture<'a, AutumnResult<()>> + Send + Sync + 'static,

Add a per-request ContextHook. Hooks run in registration order on every HTTP operation and on each WebSocket upgrade.

ⓘ
GraphqlPlugin::new(schema).context(|parts, state, data| {
    Box::pin(async move {
        let user = CurrentUser::from_request_parts(parts, state).await?;
        data.insert(user);
        Ok(())
    })
})
Source

pub fn context_hook(self, hook: impl ContextHook) -> Self

Add a ContextHook implemented as a type.

Source

pub fn on_ws_init(self, hook: impl WsInitHook) -> Self

Handle the WebSocket connection_init payload (see WsInitHook).

Source

pub fn guard<L>(self, layer: L, label: impl Into<String>) -> Self
where L: Layer<Route> + Clone + Send + Sync + 'static, L::Service: Service<Request> + Clone + Send + Sync + 'static, <L::Service as Service<Request>>::Response: IntoResponse + 'static, <L::Service as Service<Request>>::Error: Into<Infallible> + 'static, <L::Service as Service<Request>>::Future: Send + 'static,

Guard every route this plugin mounts with layer — for example RequireApiToken, so the endpoint needs Authorization: Bearer ….

This is the seam a nested router needs: AppBuilder::scoped(prefix, layer, routes![…]) wraps only the routes handed to it, never a raw router a plugin nests. The layer is the outermost on the plugin’s router, so it runs before any handler, including /sdl and the WebSocket upgrade. label is what autumn routes lists as the route’s middleware; the routes are then classified Gated.

Source

pub fn effective_config(&self) -> Result<&GraphqlConfig, &ConfigError>

The configuration this plugin will run with, after files, environment and code overrides.

§Errors

The ConfigError that will abort boot.

Source

pub fn route_infos(&self) -> Vec<RouteInfo>

The routes this plugin mounts, as autumn routes lists them.

Without a guard every route is classified RouteClassification::Public; with one, Gated with the guard’s label as middleware.

Trait Implementations§

Source§

impl<E: Executor> Plugin for GraphqlPlugin<E>

Source§

fn name(&self) -> Cow<'static, str>

Keyed by mount path, so two endpoints can coexist at different paths while a second plugin at the same path is refused as a duplicate.

Source§

fn contract(&self) -> Option<PluginContract>

Declares the supported autumn-web series (SUPPORTED_AUTUMN_WEB).

Source§

fn build(self, app: AppBuilder) -> AppBuilder

Apply this plugin’s configuration to the builder. Read more
Source§

fn route_group(&self) -> Cow<'static, str>

The route group of the routes this plugin adds (#3544). Read more

Auto Trait Implementations§

§

impl<E> !Freeze for GraphqlPlugin<E>

§

impl<E> !RefUnwindSafe for GraphqlPlugin<E>

§

impl<E> !Sync for GraphqlPlugin<E>

§

impl<E> !UnwindSafe for GraphqlPlugin<E>

§

impl<E> Forget for GraphqlPlugin<E>

§

impl<E> Send for GraphqlPlugin<E>
where Option<Source<E>>: Send,

§

impl<E> Unpin for GraphqlPlugin<E>
where Option<Source<E>>: Unpin,

§

impl<E> UnsafeUnpin for GraphqlPlugin<E>
where Option<Source<E>>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

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

fn in_current_span(self) -> Instrumented<Self> ⓘ

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

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P> Plugins for P
where P: Plugin,

Source§

fn apply(self, app: AppBuilder) -> AppBuilder

Apply every plugin in this bundle to the builder, in declaration order.
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more