ulo
ulo
Documentation

Start here

Coming from NestJSNestJS topics, mapped

The model

Protocols

GraphQLGraphQL over HTTPSubscriptionsEngines

Integrations

Operating

Reference

GraphQL

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.

CrateHolds
ulo-graphqlThe Engine trait, GqlRequest and GqlResponse. No transport.
ulo-graphql-httpGraphqlModule and GraphqlConfig: the endpoint, its status rules and the playground.
ulo-graphql-wsThe graphql-transport-ws gateway, mounted by GraphqlModule when subscriptions are enabled.
ulo-graphql-async-graphqlAsyncGraphql<Schema, Context>, the async-graphql engine. Needs Rust 1.89.
ulo-graphql-juniperJuniper<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.

examples/31-graphql/src/lib.rs
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/graphql

The 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:

examples/31-graphql/src/lib.rs
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):

examples/31-graphql/src/lib.rs
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.

In this section

GraphQL over HTTP

Status codes, content negotiation, the playground, a second schema.

Subscriptions

graphql-transport-ws on a gateway beside the endpoint.

Engines

Where async-graphql and juniper differ.

Operations

Server settings, TLS, reflection, health checks, deadlines on the wire and graceful shutdown for gRPC.

GraphQL over HTTP

How the endpoint answers a request, which status each failure gets, and how to serve a second schema.

On this page

A first endpointThe contextIn this section