Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 58 additions & 4 deletions crates/magna-build/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@

use async_graphql::dynamic::{Field, Object, SchemaBuilder, Type};

use crate::plan_extension::{FieldArgs, PlanRegistry, PlanScope};
use magna_types::{FwGraphError, StepId};

/// Context passed to a [`SchemaExtension`] hook so it can register types and
/// add fields. Internals are private — all mutation is via methods.
///
Expand All @@ -53,6 +56,9 @@ pub struct ExtensionContext<'a> {
query: Option<&'a mut Object>,
/// In-progress Mutation root. `Some` only during `extend_mutation`.
mutation: Option<&'a mut Object>,
/// Plan-fn registry for plan-based execution. Available in every phase —
/// planning is orthogonal to which root the field hangs from.
plan_registry: &'a mut PlanRegistry,
}

impl<'a> ExtensionContext<'a> {
Expand Down Expand Up @@ -115,6 +121,30 @@ impl<'a> ExtensionContext<'a> {
replace_with(*q, |obj| obj.field(field));
}

/// Register a plan function for `type_name.field_name` — the field's data
/// needs, as steps in the operation's shared plan. Available in every
/// phase.
///
/// When at least one plan fn is registered, `build_schema` attaches the
/// plan extension to the schema: per request, the operation's planned
/// root fields register their steps into ONE plan, the optimizer
/// deduplicates steps with equal fingerprints across fields, the plan
/// executes before resolution begins, and each field's resolver reads
/// its output from [`crate::PlanResults`] by response key.
///
/// The field itself must still be registered (via
/// [`query_field`](Self::query_field)) with a resolver that reads
/// `ctx.data::<Arc<PlanResults>>()` — planning supplies the data, not
/// the schema entry.
pub fn plan_field(
&mut self,
type_name: impl Into<String>,
field_name: impl Into<String>,
f: impl Fn(&PlanScope<'_>, &FieldArgs) -> Result<StepId, FwGraphError> + Send + Sync + 'static,
) {
self.plan_registry.plan_field(type_name, field_name, f);
}

/// Add a field to the Mutation root. Only valid inside `extend_mutation`.
/// Panics if called from `register_types` or `extend_query`.
///
Expand Down Expand Up @@ -230,6 +260,7 @@ pub(crate) fn run_extension_phase(
builder: SchemaBuilder,
query: &mut Object,
mutation: Option<&mut Object>,
plan_registry: &mut PlanRegistry,
extensions: &[Box<dyn SchemaExtension>],
phase: Phase,
) -> SchemaBuilder {
Expand All @@ -254,6 +285,7 @@ pub(crate) fn run_extension_phase(
builder: current_builder.take(),
query: ctx_query,
mutation: ctx_mutation,
plan_registry: &mut *plan_registry,
};

match phase {
Expand Down Expand Up @@ -292,12 +324,16 @@ mod tests {
}
}

fn fresh_ctx<'a>(query: &'a mut Object) -> ExtensionContext<'a> {
fn fresh_ctx<'a>(
query: &'a mut Object,
plan_registry: &'a mut PlanRegistry,
) -> ExtensionContext<'a> {
let builder = async_graphql::dynamic::Schema::build("Query", None, None);
ExtensionContext {
builder: Some(builder),
query: Some(query),
mutation: None,
plan_registry,
}
}

Expand All @@ -312,14 +348,17 @@ mod tests {
let mut query = Object::new("Query");
// RegisterTypes phase: query/mutation are None.
let builder = async_graphql::dynamic::Schema::build("Query", None, None);
let mut plan_reg = PlanRegistry::default();
let mut ctx = ExtensionContext {
builder: Some(builder),
query: None,
mutation: None,
plan_registry: &mut plan_reg,
};
ext.register_types(&mut ctx);
// ExtendQuery phase: query is Some.
let mut ctx2 = fresh_ctx(&mut query);
let mut plan_reg2 = PlanRegistry::default();
let mut ctx2 = fresh_ctx(&mut query, &mut plan_reg2);
ext.extend_query(&mut ctx2);
ext.extend_mutation(&mut ctx2);
}
Expand All @@ -334,10 +373,12 @@ mod tests {
}
}
let builder = async_graphql::dynamic::Schema::build("Query", None, None);
let mut plan_reg = PlanRegistry::default();
let mut ctx = ExtensionContext {
builder: Some(builder),
query: None,
mutation: None,
plan_registry: &mut plan_reg,
};
TypeAdder.register_types(&mut ctx);
assert!(ctx.builder.is_some(), "builder should be restored");
Expand All @@ -359,7 +400,8 @@ mod tests {
}
}
let mut query = Object::new("Query");
let mut ctx = fresh_ctx(&mut query);
let mut plan_reg = PlanRegistry::default();
let mut ctx = fresh_ctx(&mut query, &mut plan_reg);
MultiFielder.extend_query(&mut ctx);
// The placeholder type name must never leak into the result.
assert_eq!(query.type_name(), "Query");
Expand All @@ -369,10 +411,12 @@ mod tests {
#[should_panic(expected = "outside extend_query phase")]
fn test_query_field_panics_outside_extend_query() {
let builder = async_graphql::dynamic::Schema::build("Query", None, None);
let mut plan_reg = PlanRegistry::default();
let mut ctx = ExtensionContext {
builder: Some(builder),
query: None,
mutation: None,
plan_registry: &mut plan_reg,
};
ctx.query_field(Field::new(
"x",
Expand All @@ -385,10 +429,12 @@ mod tests {
#[should_panic(expected = "outside extend_mutation phase")]
fn test_mutation_field_panics_outside_extend_mutation() {
let builder = async_graphql::dynamic::Schema::build("Query", None, None);
let mut plan_reg = PlanRegistry::default();
let mut ctx = ExtensionContext {
builder: Some(builder),
query: None,
mutation: None,
plan_registry: &mut plan_reg,
};
ctx.mutation_field(Field::new(
"x",
Expand Down Expand Up @@ -442,7 +488,15 @@ mod tests {
},
));
let exts: Vec<Box<dyn SchemaExtension>> = vec![Box::new(DataInjector)];
let builder = run_extension_phase(builder, &mut query, None, &exts, Phase::RegisterTypes);
let mut plan_reg = PlanRegistry::default();
let builder = run_extension_phase(
builder,
&mut query,
None,
&mut plan_reg,
&exts,
Phase::RegisterTypes,
);
let schema = builder.register(query).finish().expect("schema must finish");

let res = schema.execute("{ marker }").await;
Expand Down
19 changes: 19 additions & 0 deletions crates/magna-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub(crate) mod executor;
pub mod gather;
pub mod ir;
pub mod naming;
pub mod plan_extension;
pub mod plan_resolver;
pub mod register;
pub mod resolve;
Expand All @@ -17,6 +18,9 @@ pub use ir::{
BehaviorSet, GatherOutput, ResolvedColumn, ResolvedEnum, ResolvedRelation, ResolvedResource,
ResourceKind,
};
pub use plan_extension::{
FieldArgs, PlanExtensionFactory, PlanFn, PlanRegistry, PlanResults, PlanScope,
};
pub use plan_resolver::PlanContext;
pub use union_step::{PgUnionStep, TaggedRow};
pub use extension::{ExtensionContext, SchemaExtension};
Expand Down Expand Up @@ -218,12 +222,17 @@ pub fn build_schema(
// 11. Register condition types for all resources
builder = register_condition_types(builder, &output.resources);

// Plan-fn registry — populated by extensions via `plan_field` during any
// phase; if non-empty after all phases, the plan extension is attached.
let mut plan_registry = plan_extension::PlanRegistry::default();

// 11b. Let extensions register custom types BEFORE query/mutation field construction
// so extensions can reference auto-generated types in their field signatures.
builder = extension::run_extension_phase(
builder,
&mut query,
None,
&mut plan_registry,
extensions,
extension::Phase::RegisterTypes,
);
Expand All @@ -244,6 +253,7 @@ pub fn build_schema(
builder,
&mut query,
None,
&mut plan_registry,
extensions,
extension::Phase::ExtendQuery,
);
Expand Down Expand Up @@ -281,6 +291,7 @@ pub fn build_schema(
builder,
&mut query,
Some(&mut mutation),
&mut plan_registry,
extensions,
extension::Phase::ExtendMutation,
);
Expand All @@ -307,6 +318,14 @@ pub fn build_schema(
// 17. Apply limits and finish
builder = builder.limit_complexity(200).limit_depth(10);

// Attach plan-based execution only when an extension registered at least
// one plan fn — unplanned schemas pay zero per-request overhead.
if !plan_registry.is_empty() {
builder = builder.extension(plan_extension::PlanExtensionFactory::new(Arc::new(
plan_registry,
)));
}

builder
.finish()
.map_err(|e| BuildError::SchemaFinish(e.to_string()))
Expand Down
Loading
Loading