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>>
impl<Q, M, S> GraphqlPlugin<Schema<Q, M, S>>
Sourcepub fn new(schema: Schema<Q, M, S>) -> Self
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.
Sourcepub fn from_builder(builder: SchemaBuilder<Q, M, S>) -> Selfwhere
SchemaBuilder<Q, M, S>: Send,
pub fn from_builder(builder: SchemaBuilder<Q, M, S>) -> Selfwhere
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>
impl<E: Executor> GraphqlPlugin<E>
Sourcepub fn from_executor(executor: E) -> Self
pub fn from_executor(executor: E) -> Self
Sourcepub fn drain_handle(&self) -> DrainHandle
pub fn drain_handle(&self) -> DrainHandle
A handle that closes every open WebSocket and SSE stream of this
endpoint on demand (see DrainHandle).
Sourcepub fn sdl_text(self, sdl: impl Into<String>) -> Self
pub fn sdl_text(self, sdl: impl Into<String>) -> Self
Supply the SDL served at GET {path}/sdl (for
from_executor mounts).
Sourcepub fn configure(
self,
apply: impl Fn(&mut GraphqlConfig) + Send + Sync + 'static,
) -> Self
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;
})Sourcepub fn config(self, config: GraphqlConfig) -> Self
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.
Sourcepub fn config_section(self, section: impl Into<String>) -> Self
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.
Sourcepub fn development(self, development: bool) -> Self
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.
Sourcepub fn path(self, path: impl Into<String>) -> Self
pub fn path(self, path: impl Into<String>) -> Self
Mount under a different path (for example /api/graphql).
Sourcepub fn introspection(self, enabled: bool) -> Self
pub fn introspection(self, enabled: bool) -> Self
Allow or forbid introspection regardless of profile.
Sourcepub fn without_sdl(self) -> Self
pub fn without_sdl(self) -> Self
Do not serve GET {path}/sdl.
Sourcepub fn mask_unexpected_errors(self, mask: bool) -> Self
pub fn mask_unexpected_errors(self, mask: bool) -> Self
Mask (or stop masking) uncoded resolver errors regardless of profile.
Sourcepub fn timeout(self, timeout: Duration) -> Self
pub fn timeout(self, timeout: Duration) -> Self
Per-operation execution timeout (Duration::ZERO = none).
Sourcepub fn batching(self, max_operations: usize) -> Self
pub fn batching(self, max_operations: usize) -> Self
Accept batched requests of up to max_operations operations.
Sourcepub fn persisted_queries(self, mode: PersistedQueryMode) -> Self
pub fn persisted_queries(self, mode: PersistedQueryMode) -> Self
Set the persisted-query mode.
Sourcepub fn trusted_documents(self, documents: TrustedDocuments) -> Self
pub fn trusted_documents(self, documents: TrustedDocuments) -> Self
Add trusted documents in code (merged with any manifest file).
Sourcepub fn persisted_query_store(self, store: impl PersistedQueryStore) -> Self
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).
Sourcepub fn context<F>(self, hook: F) -> Self
pub fn context<F>(self, hook: F) -> Self
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(())
})
})Sourcepub fn context_hook(self, hook: impl ContextHook) -> Self
pub fn context_hook(self, hook: impl ContextHook) -> Self
Add a ContextHook implemented as a type.
Sourcepub fn on_ws_init(self, hook: impl WsInitHook) -> Self
pub fn on_ws_init(self, hook: impl WsInitHook) -> Self
Handle the WebSocket connection_init payload (see WsInitHook).
Sourcepub fn guard<L>(self, layer: L, label: impl Into<String>) -> Selfwhere
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,
pub fn guard<L>(self, layer: L, label: impl Into<String>) -> Selfwhere
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.
Sourcepub fn effective_config(&self) -> Result<&GraphqlConfig, &ConfigError>
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.
Sourcepub fn route_infos(&self) -> Vec<RouteInfo>
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>
impl<E: Executor> Plugin for GraphqlPlugin<E>
Source§fn name(&self) -> Cow<'static, str>
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>
fn contract(&self) -> Option<PluginContract>
Declares the supported autumn-web series (SUPPORTED_AUTUMN_WEB).