GraphQL
Serve a GraphQL schema over HTTP, with the schema's context built from the DI container once per request.
GraphQL in Ulo is an engine bound under one key and a module that serves it. The engine is async-graphql or juniper, the module is GraphqlModule, and the same engine can be served over HTTP, over a WebSocket gateway for subscriptions, or from an ordinary handler on another transport.
| Crate | Holds |
|---|---|
ulo-graphql | The Engine trait, GqlRequest and GqlResponse. No transport. |
ulo-graphql-http | GraphqlModule and GraphqlConfig: the endpoint, its status rules and the playground. |
ulo-graphql-ws | The graphql-transport-ws gateway, mounted by GraphqlModule when subscriptions are enabled. |
ulo-graphql-async-graphql | AsyncGraphql<Schema, Context>, the async-graphql engine. Needs Rust 1.89. |
ulo-graphql-juniper | Juniper<Query, Mutation, Subscription>, the juniper engine. |
A first endpoint
This is examples/31-graphql. The module binds the schema, the context type and the engine, and exports the engine so GraphqlModule can read it.
use async_graphql::{EmptySubscription, Schema};
use ulo::module;
use ulo_graphql::Engine;
use ulo_graphql_async_graphql::AsyncGraphql;
use ulo_graphql_http::{GraphqlConfig, GraphqlModule};
pub type Api = Schema<Query, Mutation, EmptySubscription>;
#[module(
providers = [
Notes,
Gql,
with = || -> Api { Schema::new(Query, Mutation, EmptySubscription) },
AsyncGraphql<Api, Gql> as dyn Engine,
],
exports = [dyn Engine, Notes, Gql],
)]
pub struct ApiModule;
#[module(imports = [GraphqlModule::for_root(GraphqlConfig::at("/graphql").playground(false).engine_from(ApiModule))])]
pub struct AppModule;engine_from(ApiModule) names the module that binds and exports dyn Engine. GraphqlModule imports it. A module sees its own bindings, its imports' exports and the globals' exports. AppModule, which imports GraphqlModule, is none of those, so without engine_from the engine would have to be exported from a global module.
cargo run -p ex-31-graphql
curl -H 'content-type: application/json' \
-d '{"query":"{ greeting(name: \"ulo\") }"}' http://127.0.0.1:3000/graphqlThe response is {"data":{"greeting":"hello ulo"}}.
The context
The second type parameter of AsyncGraphql<Api, Gql> is the schema's context. It is an ordinary injectable with execution scope, built once for each request:
use ulo::{Dep, injectable};
/// Built once per GraphQL execution, so every field of one request reads the same `id`.
pub struct Gql {
pub id: u64,
}
#[injectable(execution)]
impl Gql {
fn new(notes: Dep<Notes>) -> Self {
Gql { id: notes.executions.fetch_add(1, Ordering::SeqCst) + 1 }
}
}Two fields of one request read the same instance, and the next request gets a new one. A resolver reads the context with ctx.data::<Dep<Gql>>(), and any other provider with ulo_graphql_async_graphql::dep::<T>(ctx):
use async_graphql::Context;
use ulo_graphql_async_graphql::dep;
async fn notes(&self, ctx: &Context<'_>) -> async_graphql::Result<Vec<String>> {
Ok(dep::<Notes>(ctx).await?.texts.lock().expect("the notes lock").clone())
}The engine resolves the context in the module that binds the engine, so the context need not be exported. dep::<T> resolves with the visibility of the module that serves the endpoint. That is why Notes is in the exports list above. The context's binding is read at each call and not checked when the application wires: a context that is not bound fails every call with a logged error and a 500 answer.
A context built in the call's execution can read Option<Dep<RequestHead>>, which is Some over HTTP and None over the subscription gateway and RPC.