Engines
What async-graphql and juniper share under the Engine trait, and the places they differ.
An engine adapter implements ulo_graphql::Engine for a schema and a context type, and is bound under the key dyn Engine. Everything that uses it, the endpoint, the gateway and the playground, is the same for both.
ulo-graphql-async-graphql | ulo-graphql-juniper | |
|---|---|---|
| Library | async-graphql 7 | juniper 0.16 |
| Adapter | AsyncGraphql<Schema<Q, M, Sub>, Context> | Juniper<Q, M, Sub> |
| Rust floor | 1.89 | the workspace's 1.88 |
| Context type | A parameter of the adapter, inserted into the request as Dep<Context> | Q::Context, the type the schema's root types name |
| Reading a provider in a resolver | ulo_graphql_async_graphql::dep::<T>(ctx) | ulo_graphql_juniper::dep::<T>(&context) |
| Playground | GraphiQL | juniper's graphiql_source |
| Wrong-typed variable | An executed result whose error carries the field's path | A request error with no data |
async-graphql
The context is a type parameter of the adapter, because async-graphql accepts any D as request data and the adapter cannot know the application's type by convention. Per call the adapter resolves the context in the call's execution, inserts it as Dep<Context>, and inserts the call's ExecutionRef as a second value that dep reads. A resolver reads the context with ctx.data::<Dep<Context>>()?. The bound schema is a provider like any other, here built by a with factory:
with = || -> Api { Schema::new(Query, Mutation, EmptySubscription) },
AsyncGraphql<Api, Gql> as dyn Engine,examples/31-graphql and examples/32-graphql-subscriptions both use this engine.
juniper
juniper's Context is a type the schema's root types name, so Juniper<Q, M, Sub> resolves Q::Context in the call's execution and hands it to juniper as the context. The context is an execution-scoped provider, as with async-graphql. dep reads through the context's ExecutionRef, so the context type implements AsRef<ExecutionRef>.
The crate's tests bind the pieces directly, the schema as a value and the context and the engine as providers:
fn register(&self, m: &mut ModuleDef<'_>) {
m.value(schema());
m.provide::<Ctx>();
m.provide::<Juniper<Query, Mutation, Subscriptions>>().also_as::<dyn Engine>(|engine| engine);
}Subscriptions are built on juniper's resolve_into_stream, and a query or mutation sent to subscribe answers one response. No example in examples/ uses juniper.
Where the two disagree
A required variable that is absent or null is a request error in both engines, answered before any field runs. A variable of the wrong type, an Int given a string, is not the same:
- async-graphql coerces a variable when the field reading it resolves. The request executes and the response is an executed result, 200, whose error carries the
pathof that field. - juniper answers a request error with no
data, 422.
A client that branches on a request error sees the fault from juniper only. The difference is async-graphql's, and the adapter does not close it.
How a request error is classified for the HTTP status also differs in detail. async-graphql's adapter names Syntax, Operation, Coercion and Validation. juniper's maps parse errors to Syntax, validation errors to Validation and the operation errors to Operation, and reports a missing required variable as Validation because juniper raises it as one. The HTTP binding answers 422 for Validation and Coercion alike.
A context that fails to build
Neither adapter declares the context as a dependency, because declaring it would make the engine itself execution-scoped. The binding is read at each call. A context that is not bound fails each call with a request error and an error log line, and the HTTP binding answers 500.