use std::{collections::VecDeque, sync::Arc};
use rustc_hash::FxHashMap;
use smallvec::SmallVec;
use crate::{
Location, Object, Value, ast,
error::GraphQLError,
schema::Schema,
types::{Field, InputValue, Name, ObjectType, Type, TypeRef, names_eq},
};
use super::{coerce, collect, selection::type_condition_matches};
const MAX_SOURCES: usize = 512;
const MAX_PLANS: usize = 256;
const MAX_CONDITIONAL_NODES: usize = 4;
pub(crate) struct PreparedPlan<'a> {
sources: Vec<Source<'a>>,
root: Option<u32>,
depth: usize,
}
struct Source<'a> {
nodes: Vec<Arc<ast::Field<'a>>>,
root_sets: Vec<Arc<ast::SelectionSet<'a>>>,
identity: Vec<usize>,
types: Vec<FieldPlan<'a>>,
unplannable: bool,
}
pub(crate) struct FieldPlan<'a> {
type_name: Name,
groups: Vec<FieldGroup<'a>>,
pub(crate) has_resolvers: bool,
pub(crate) identity_projection: bool,
}
pub(crate) struct Property {
pub(crate) name: Name,
pub(crate) key: Name,
pub(crate) kind: ScalarKind,
}
#[derive(Clone, Copy)]
pub(crate) enum ScalarKind {
String,
Int,
Boolean,
Float,
}
impl ScalarKind {
#[inline(always)]
pub(crate) fn accepts(self, value: &Value) -> bool {
match self {
Self::String => matches!(value, Value::String(_)),
Self::Int => matches!(value, Value::Int(_)),
Self::Boolean => matches!(value, Value::Boolean(_)),
Self::Float => matches!(value, Value::Float(number) if number.is_finite()),
}
}
}
pub(crate) struct FieldGroup<'a> {
response_key: &'a str,
alias_key: Option<Arc<str>>,
field_index: Option<u32>,
nodes: Vec<PlanNode<'a>>,
variants: Vec<Variant<'a>>,
args: Option<ArgsTemplate<'a>>,
pub(crate) property: Option<Property>,
}
struct PlanNode<'a> {
node: Arc<ast::Field<'a>>,
origin: usize,
requirements: Vec<Requirement<'a>>,
}
struct PlanNodeRef<'a> {
node: Arc<ast::Field<'a>>,
origin: usize,
included: bool,
}
#[derive(Clone, Copy)]
struct Requirement<'a> {
variable: &'a str,
expected: bool,
}
pub(crate) struct Variant<'a> {
pub(crate) nodes: Vec<Arc<ast::Field<'a>>>,
pub(crate) child: Option<u32>,
}
impl<'a> Variant<'a> {
#[inline]
pub(crate) fn selection_sets(&'a self) -> SmallVec<[&'a ast::SelectionSet<'a>; 4]> {
self.nodes
.iter()
.filter_map(|node| node.selection_set.as_ref())
.collect()
}
}
pub(crate) struct ArgsTemplate<'a> {
field_name: Name,
slots: Vec<ArgSlot<'a>>,
}
enum ArgSlot<'a> {
Literal { name: Name, value: Value },
Variable {
name: Name,
variable: &'a str,
ty: TypeRef,
location: Location,
default: Option<Value>,
},
}
#[derive(Clone, Copy)]
pub(crate) enum GroupNodes<'s, 'a> {
Borrowed(&'s [&'a ast::Field<'a>]),
Planned(&'a [Arc<ast::Field<'a>>]),
}
impl<'s, 'a> GroupNodes<'s, 'a> {
#[inline(always)]
pub(crate) fn first(&self) -> Option<&'a ast::Field<'a>> {
match self {
Self::Borrowed(nodes) => nodes.first().copied(),
Self::Planned(nodes) => nodes.first().map(|node| &**node),
}
}
pub(crate) fn field_node_refs(&self) -> SmallVec<[&'a ast::Field<'a>; 4]> {
match self {
Self::Borrowed(nodes) => nodes.iter().copied().collect(),
Self::Planned(nodes) => nodes.iter().map(|node| &**node).collect(),
}
}
#[inline]
pub(crate) fn selection_sets(&self) -> SmallVec<[&'a ast::SelectionSet<'a>; 4]> {
match self {
Self::Borrowed(nodes) => nodes
.iter()
.copied()
.filter_map(|node| node.selection_set.as_ref())
.collect(),
Self::Planned(nodes) => nodes
.iter()
.filter_map(|node| node.selection_set.as_ref())
.collect(),
}
}
}
pub(crate) struct GroupView<'s, 'a> {
pub(crate) response_key: &'a str,
pub(crate) alias_key: Option<&'s Arc<str>>,
pub(crate) nodes: GroupNodes<'s, 'a>,
pub(crate) field: Option<&'a Field>,
pub(crate) args: Option<&'s ArgsTemplate<'a>>,
pub(crate) child: Option<u32>,
pub(crate) planned: bool,
}
impl<'s, 'a> GroupView<'s, 'a> {
#[inline(always)]
pub(crate) fn key_arc(&self, first: &ast::Field<'_>, field: Option<&Field>) -> Arc<str> {
if let Some(key) = self.alias_key {
return Arc::clone(key);
}
match (self.planned, field, first.alias) {
(true, Some(field), _) => field.name.clone(),
(true, None, _) => Arc::from(self.response_key),
(false, Some(field), None) => field.name.clone(),
(false, _, _) => Arc::from(self.response_key),
}
}
}
impl<'a> PreparedPlan<'a> {
pub(crate) fn root(&self) -> Option<u32> {
self.root
}
pub(crate) fn depth(&self) -> usize {
self.depth
}
pub(crate) fn source_sets(
&self,
source: u32,
) -> impl Iterator<Item = &ast::SelectionSet<'a>> + '_ {
let source = self.sources.get(source as usize);
source.into_iter().flat_map(|source| {
source.root_sets.iter().map(|set| &**set).chain(
source
.nodes
.iter()
.filter_map(|node| node.selection_set.as_ref()),
)
})
}
#[inline]
pub(crate) fn field_plan(
&self,
source: u32,
object_type: &ObjectType,
) -> Option<&FieldPlan<'a>> {
self.sources
.get(source as usize)?
.types
.iter()
.find(|plan| names_eq(&plan.type_name, object_type.name.as_ref()))
}
}
impl<'a> FieldPlan<'a> {
pub(crate) fn len(&self) -> usize {
self.groups.len()
}
#[inline(always)]
pub(crate) fn groups(&'a self) -> &'a [FieldGroup<'a>] {
&self.groups
}
#[inline]
pub(crate) fn matches_parent(&self, parent: &Object) -> bool {
if !self.identity_projection || self.groups.len() != parent.len() {
return false;
}
let mut fields = parent.iter();
for group in &self.groups {
let Some((key, value)) = fields.next() else {
return false;
};
let Some(property) = &group.property else {
return false;
};
if !names_eq(group.response_key, key) || !property.kind.accepts(value) {
return false;
}
}
true
}
pub(crate) fn group_view(
&'a self,
index: usize,
variables: &Object,
object_type: &'a ObjectType,
) -> Option<GroupView<'a, 'a>> {
let group = self.groups.get(index)?;
let variant = group.variant(variables)?;
if variant.nodes.is_empty() {
return None;
}
Some(GroupView {
response_key: group.response_key,
alias_key: group.alias_key.as_ref(),
nodes: GroupNodes::Planned(&variant.nodes),
field: group
.field_index
.and_then(|index| object_type.fields.get_index(index as usize))
.map(|(_, field)| field),
args: group.args.as_ref(),
child: variant.child,
planned: true,
})
}
}
impl<'a> FieldGroup<'a> {
pub(crate) fn response_key(&self) -> &'a str {
self.response_key
}
pub(crate) fn field(&self, object_type: &'a ObjectType) -> Option<&'a Field> {
self.field_index
.and_then(|index| object_type.fields.get_index(index as usize))
.map(|(_, field)| field)
}
pub(crate) fn args(&self) -> Option<&ArgsTemplate<'a>> {
self.args.as_ref()
}
pub(crate) fn key(&self, field: Option<&Field>) -> Arc<str> {
if let Some(key) = &self.alias_key {
return Arc::clone(key);
}
match field {
Some(field) => field.name.clone(),
None => Arc::from(self.response_key),
}
}
#[inline(always)]
pub(crate) fn variant(&'a self, variables: &Object) -> Option<&'a Variant<'a>> {
if self.variants.len() == 1 {
return self.variants.first();
}
let mut mask = 0usize;
let mut bit = 0;
for node in &self.nodes {
if node.requirements.is_empty() {
continue;
}
if node.requirements.iter().all(|requirement| {
variables
.get(requirement.variable)
.and_then(Value::as_bool)
.unwrap_or(false)
== requirement.expected
}) {
mask |= 1 << bit;
}
bit += 1;
}
self.variants.get(mask)
}
}
impl<'a> ArgsTemplate<'a> {
pub(crate) fn fill(
&self,
variables: &Object,
coerced: &mut Object,
) -> Result<(), GraphQLError> {
for slot in &self.slots {
match slot {
ArgSlot::Literal { name, value } => {
coerced.push_unchecked(name.clone(), value.clone());
}
ArgSlot::Variable {
name,
variable,
ty,
location,
default,
} => match variables.get(variable) {
Some(value) => {
if value.is_null() && ty.is_non_null() {
return Err(GraphQLError::new(format!(
"Field \"{}\" argument \"{}\" of non-null type \"{}\" must not be null.",
self.field_name, name, ty
))
.at(*location));
}
coerced.push_unchecked(name.clone(), value.clone());
}
None => {
if let Some(value) = default {
coerced.push_unchecked(name.clone(), value.clone());
} else if ty.is_non_null() {
return Err(GraphQLError::new(format!(
"Field \"{}\" argument \"{}\" of required type \"{}\" was not provided because variable \"${}\" was not provided.",
self.field_name, name, ty, variable
))
.at(*location));
}
}
},
}
}
Ok(())
}
}
pub(crate) fn build_empty<'a>() -> PreparedPlan<'a> {
PreparedPlan {
sources: Vec::new(),
root: None,
depth: 0,
}
}
#[cfg_attr(
feature = "tracing",
tracing::instrument(
skip_all,
level = "debug",
name = "gql.plan",
fields(operation = operation.name.unwrap_or("<anonymous>"), root_type = root_type.name.as_ref())
)
)]
pub(crate) fn build<'a>(
schema: &Schema,
document: &ast::Document<'a>,
operation: &ast::OperationDefinition<'a>,
root_type: &ObjectType,
) -> PreparedPlan<'a> {
let fragments: Vec<&ast::FragmentDefinition<'a>> = document.fragments().collect();
let mut builder = Builder {
schema,
fragments,
sources: vec![Source {
nodes: Vec::new(),
root_sets: vec![Arc::new(operation.selection_set.clone())],
identity: vec![std::ptr::from_ref(&operation.selection_set) as usize],
types: Vec::new(),
unplannable: false,
}],
fields: FxHashMap::default(),
queue: VecDeque::new(),
plan_count: 0,
exhausted: false,
};
builder.queue.push_back((0, root_type.name.clone()));
while let Some((source, type_name)) = builder.queue.pop_front() {
if builder.exhausted || builder.plan_count >= MAX_PLANS {
break;
}
if builder.sources[source as usize].unplannable
|| builder.sources[source as usize]
.types
.iter()
.any(|plan| names_eq(&plan.type_name, &type_name))
{
continue;
}
let Some(object_type) = schema.object_type(type_name.as_ref()).cloned() else {
continue;
};
match builder.plan(source, &object_type) {
Ok(plan) => {
builder.sources[source as usize].types.push(plan);
builder.plan_count += 1;
}
Err(PlanError::Budget) => {
builder.exhausted = true;
}
Err(PlanError::Unsupported) => {
builder.sources[source as usize].unplannable = true;
}
}
}
PreparedPlan {
depth: plan_depth(&builder.sources),
sources: builder.sources,
root: Some(0),
}
}
fn plan_depth(sources: &[Source<'_>]) -> usize {
fn visit(sources: &[Source<'_>], id: u32, state: &mut [u8], memo: &mut [usize]) -> usize {
let index = id as usize;
if index >= sources.len() {
return 0;
}
match state[index] {
2 => return memo[index],
1 => return 1,
_ => {}
}
state[index] = 1;
let mut depth = 1;
for plan in &sources[index].types {
for group in &plan.groups {
for variant in &group.variants {
let Some(child) = variant.child else {
continue;
};
if child as usize == index {
continue;
}
depth = depth.max(1 + visit(sources, child, state, memo));
}
}
}
state[index] = 2;
memo[index] = depth;
depth
}
if sources.is_empty() {
return 0;
}
visit(
sources,
0,
&mut vec![0u8; sources.len()],
&mut vec![0usize; sources.len()],
)
}
enum PlanError {
Unsupported,
Budget,
}
struct Builder<'a, 's, 'f> {
schema: &'s Schema,
fragments: Vec<&'f ast::FragmentDefinition<'a>>,
sources: Vec<Source<'a>>,
fields: FxHashMap<usize, Arc<ast::Field<'a>>>,
queue: VecDeque<(u32, Name)>,
plan_count: usize,
exhausted: bool,
}
impl<'a> Builder<'a, '_, '_> {
fn plan(&mut self, source: u32, object_type: &ObjectType) -> Result<FieldPlan<'a>, PlanError> {
let (root_sets, source_nodes) = {
let source = &self.sources[source as usize];
(source.root_sets.clone(), source.nodes.clone())
};
let mut groups: Vec<FieldGroup<'a>> = Vec::new();
let mut visited: SmallVec<[&'a str; 8]> = SmallVec::new();
let mut work = 0u64;
let mut tasks: SmallVec<[(u32, Name); 4]> = SmallVec::new();
let schema = self.schema;
for selection_set in root_sets.iter().map(|set| set.as_ref()).chain(
source_nodes
.iter()
.filter_map(|node| node.selection_set.as_ref()),
) {
visit_selection_set(
schema,
&self.fragments,
&mut self.fields,
object_type,
selection_set,
&[],
&mut visited,
&mut groups,
0,
&mut work,
)?;
}
for group in &mut groups {
let field = group
.field_index
.and_then(|index| object_type.fields.get_index(index as usize))
.map(|(_, field)| field);
group.args = match field {
Some(field) => match compile_args(self.schema, field, &group.nodes) {
Ok(args) => args,
Err(()) => None,
},
None => None,
};
group.property = field
.filter(|_| group.alias_key.is_none())
.and_then(|field| property(self.schema, field));
let conditional = group
.nodes
.iter()
.filter(|node| !node.requirements.is_empty())
.count();
if conditional > MAX_CONDITIONAL_NODES {
return Err(PlanError::Unsupported);
}
let child_types = field.map(|field| child_type_names(self.schema, &field.ty));
if conditional == 0 {
let nodes: Vec<PlanNodeRef<'a>> = group
.nodes
.iter()
.map(|node| PlanNodeRef {
node: node.node.clone(),
origin: node.origin,
included: true,
})
.collect();
let child = self.intern_nodes(&nodes);
group.variants.push(Variant {
nodes: nodes.into_iter().map(|node| node.node).collect(),
child,
});
if let (Some(child), Some(types)) = (child, &child_types) {
for name in types {
tasks.push((child, name.clone()));
}
}
} else {
let combinations = 1usize << conditional;
for mask in 0..combinations {
let mut nodes: Vec<PlanNodeRef<'a>> = Vec::with_capacity(group.nodes.len());
let mut bit = 0;
for node in &group.nodes {
let included = if node.requirements.is_empty() {
true
} else {
let included = mask & (1 << bit) != 0;
bit += 1;
included
};
nodes.push(PlanNodeRef {
node: node.node.clone(),
origin: node.origin,
included,
});
}
let child = self.intern_nodes(&nodes);
group.variants.push(Variant {
nodes: nodes
.iter()
.filter(|node| node.included)
.map(|node| node.node.clone())
.collect(),
child,
});
if let (Some(child), Some(types)) = (child, &child_types) {
for name in types {
tasks.push((child, name.clone()));
}
}
}
}
}
let has_resolvers = groups.iter().any(|group| {
group
.field_index
.and_then(|index| object_type.fields.get_index(index as usize))
.is_some_and(|(_, field)| field.resolve.is_some())
});
let identity_projection = groups.iter().all(|group| {
group.property.is_some()
&& group.variants.len() == 1
&& group.variants[0].nodes.len() == 1
});
for (source, name) in tasks {
self.queue.push_back((source, name));
}
Ok(FieldPlan {
type_name: object_type.name.clone(),
groups,
has_resolvers,
identity_projection,
})
}
fn intern_nodes(&mut self, nodes: &[PlanNodeRef<'a>]) -> Option<u32> {
let selected: Vec<&PlanNodeRef<'a>> = nodes
.iter()
.filter(|node| node.included && node.node.selection_set.is_some())
.collect();
if selected.is_empty() {
return None;
}
let mut identity: Vec<usize> = selected.iter().map(|node| node.origin).collect();
identity.sort_unstable();
if let Some(index) = self
.sources
.iter()
.position(|source| !source.unplannable && source.identity == identity)
{
return Some(index as u32);
}
if self.sources.len() >= MAX_SOURCES {
return None;
}
self.sources.push(Source {
nodes: selected.iter().map(|node| node.node.clone()).collect(),
root_sets: Vec::new(),
identity,
types: Vec::new(),
unplannable: false,
});
Some((self.sources.len() - 1) as u32)
}
}
#[allow(clippy::too_many_arguments)]
fn visit_selection_set<'s, 'a>(
schema: &Schema,
fragments: &[&'s ast::FragmentDefinition<'a>],
fields: &mut FxHashMap<usize, Arc<ast::Field<'a>>>,
object_type: &ObjectType,
selection_set: &'s ast::SelectionSet<'a>,
inherited: &[Requirement<'a>],
visited: &mut SmallVec<[&'a str; 8]>,
groups: &mut Vec<FieldGroup<'a>>,
depth: usize,
work: &mut u64,
) -> Result<(), PlanError> {
for selection in &selection_set.items {
*work += 1;
if *work > collect::MAX_COLLECT_WORK {
return Err(PlanError::Budget);
}
let own = condition(selection.directives());
let Condition::Include(own) = own else {
continue;
};
match selection {
ast::Selection::Field(field) => {
let mut requirements: Vec<Requirement<'a>> = Vec::new();
requirements.extend(inherited.iter().copied());
requirements.extend(own.iter().copied());
add_field(groups, fields, object_type, field, requirements);
}
ast::Selection::InlineFragment(fragment) => {
if let Some(condition) = fragment.type_condition {
if !type_condition_matches(schema, condition, object_type.name.as_ref())
.map_err(|_| PlanError::Unsupported)?
{
continue;
}
}
let mut combined: SmallVec<[Requirement<'a>; 2]> =
inherited.iter().copied().collect();
combined.extend(own.iter().copied());
visit_selection_set(
schema,
fragments,
fields,
object_type,
&fragment.selection_set,
&combined,
visited,
groups,
depth,
work,
)?;
}
ast::Selection::FragmentSpread(spread) => {
if !own.is_empty() || !inherited.is_empty() {
return Err(PlanError::Unsupported);
}
if visited.iter().any(|name| names_eq(name, spread.name)) {
continue;
}
visited.push(spread.name);
let Some(fragment) = fragments
.iter()
.find(|fragment| names_eq(fragment.name, spread.name))
.copied()
else {
return Err(PlanError::Unsupported);
};
if depth >= collect::MAX_COLLECT_FRAGMENT_DEPTH {
return Err(PlanError::Unsupported);
}
if !type_condition_matches(
schema,
fragment.type_condition,
object_type.name.as_ref(),
)
.map_err(|_| PlanError::Unsupported)?
{
continue;
}
visit_selection_set(
schema,
fragments,
fields,
object_type,
&fragment.selection_set,
inherited,
visited,
groups,
depth + 1,
work,
)?;
}
}
}
Ok(())
}
fn add_field<'a>(
groups: &mut Vec<FieldGroup<'a>>,
fields: &mut FxHashMap<usize, Arc<ast::Field<'a>>>,
object_type: &ObjectType,
field: &ast::Field<'a>,
requirements: Vec<Requirement<'a>>,
) {
let response_key = field.response_key();
let index = match groups
.iter()
.position(|group| names_eq(group.response_key, response_key))
{
Some(index) => index,
None => {
let field_index = object_type
.fields
.get_index_of(field.name)
.and_then(|index| u32::try_from(index).ok());
groups.push(FieldGroup {
response_key,
alias_key: field.alias.map(|_| Arc::from(response_key)),
field_index,
nodes: Vec::new(),
variants: Vec::new(),
args: None,
property: None,
});
groups.len() - 1
}
};
groups[index].nodes.push(PlanNode {
node: intern_field(fields, field),
origin: std::ptr::from_ref(field) as usize,
requirements,
});
}
fn intern_field<'a>(
fields: &mut FxHashMap<usize, Arc<ast::Field<'a>>>,
field: &ast::Field<'a>,
) -> Arc<ast::Field<'a>> {
let address = std::ptr::from_ref(field) as usize;
if let Some(node) = fields.get(&address) {
return Arc::clone(node);
}
let node = Arc::new(field.clone());
fields.insert(address, Arc::clone(&node));
node
}
enum Condition<'a> {
Include(SmallVec<[Requirement<'a>; 1]>),
Exclude,
}
fn condition<'a>(directives: &[ast::Directive<'a>]) -> Condition<'a> {
let mut requirements = SmallVec::new();
for directive in directives {
match directive.name {
"skip" => {
if let Some(argument) = directive.argument("if") {
match argument.value {
ast::Value::Boolean(true) => return Condition::Exclude,
ast::Value::Variable(variable) => requirements.push(Requirement {
variable,
expected: false,
}),
_ => {}
}
}
}
"include" => {
if let Some(argument) = directive.argument("if") {
match argument.value {
ast::Value::Boolean(false) => return Condition::Exclude,
ast::Value::Variable(variable) => requirements.push(Requirement {
variable,
expected: true,
}),
_ => {}
}
}
}
_ => {}
}
}
Condition::Include(requirements)
}
fn property(schema: &Schema, field: &Field) -> Option<Property> {
if field.resolve.is_some() || field.sync_resolve.is_some() || !field.args.is_empty() {
return None;
}
let mut ty = &field.ty;
while let TypeRef::NonNull(inner) = ty {
ty = inner;
}
let TypeRef::Named(name) = ty else {
return None;
};
let Some(Type::Scalar(scalar)) = schema.get_type(name) else {
return None;
};
if !scalar.builtin {
return None;
}
let kind = match name.as_ref() {
"String" | "ID" => ScalarKind::String,
"Int" => ScalarKind::Int,
"Boolean" => ScalarKind::Boolean,
"Float" => ScalarKind::Float,
_ => return None,
};
Some(Property {
name: field.name.clone(),
key: field.name.clone(),
kind,
})
}
fn compile_args<'a>(
schema: &Schema,
field: &Field,
nodes: &[PlanNode<'a>],
) -> Result<Option<ArgsTemplate<'a>>, ()> {
if field.args.is_empty() {
return Ok(None);
}
let node = nodes.first().ok_or(())?.node.as_ref();
for argument in &node.arguments {
if !field.args.contains_key(argument.name) {
return Err(());
}
if !matches!(argument.value, ast::Value::Variable(_)) && contains_variable(&argument.value)
{
return Err(());
}
}
let mut slots: Vec<ArgSlot<'a>> = Vec::with_capacity(field.args.len());
for (name, definition) in &field.args {
match node.argument(name) {
Some(argument) => match &argument.value {
ast::Value::Variable(variable) => {
let default = match &definition.default_value {
Some(default) => Some(coerce_runtime(schema, default, definition)?),
None => None,
};
slots.push(ArgSlot::Variable {
name: name.clone(),
variable,
ty: definition.ty.clone(),
location: argument.location,
default,
});
}
value => {
let value = coerce_literal(schema, value, definition)?;
slots.push(ArgSlot::Literal {
name: name.clone(),
value,
});
}
},
None => {
let Some(default) = &definition.default_value else {
if definition.ty.is_non_null() {
return Err(());
}
continue;
};
let value = coerce_runtime(schema, default, definition)?;
slots.push(ArgSlot::Literal {
name: name.clone(),
value,
});
}
}
}
Ok(Some(ArgsTemplate {
field_name: field.name.clone(),
slots,
}))
}
fn coerce_literal(
schema: &Schema,
value: &ast::Value<'_>,
definition: &InputValue,
) -> Result<Value, ()> {
coerce::coerce_literal(schema, value, &definition.ty, None).map_err(|_| ())
}
fn coerce_runtime(schema: &Schema, value: &Value, definition: &InputValue) -> Result<Value, ()> {
let mut path = Vec::new();
coerce::coerce_input_value(schema, value, &definition.ty, &mut path).map_err(|_| ())
}
fn contains_variable(value: &ast::Value<'_>) -> bool {
match value {
ast::Value::Variable(_) => true,
ast::Value::List(items) => items.iter().any(contains_variable),
ast::Value::Object(fields) => fields.iter().any(|field| contains_variable(&field.value)),
_ => false,
}
}
fn child_type_names(schema: &Schema, ty: &TypeRef) -> Vec<Name> {
let mut current = ty;
loop {
current = match current {
TypeRef::NonNull(inner) => inner,
TypeRef::List(inner) => inner,
TypeRef::Named(_) => break,
};
}
let TypeRef::Named(name) = current else {
return Vec::new();
};
match schema.get_type(name) {
Some(Type::Object(object)) => vec![object.name.clone()],
Some(Type::Interface(_) | Type::Union(_)) => schema.possible_types(name),
_ => Vec::new(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
Field, Object, ObjectType, Schema, TypeRef, ValidationOptions, parser::parse_query,
};
fn schema() -> Schema {
let user = ObjectType::new("User")
.field(Field::new("id", TypeRef::id().non_null()))
.field(Field::new("name", TypeRef::string().non_null()))
.field(Field::new("age", TypeRef::int()))
.field(Field::new(
"friends",
TypeRef::named("User").non_null().list().non_null(),
))
.field(
Field::new("score", TypeRef::int().non_null())
.resolve(|_| Box::pin(async { Ok(Value::Int(1)) })),
);
let query = ObjectType::new("Query")
.field(
Field::new("user", TypeRef::named("User"))
.arg(InputValue::new("id", TypeRef::id()))
.resolve(|context| {
let parent = context.parent.clone();
Box::pin(async move { Ok(parent) })
}),
)
.field(
Field::new("health", TypeRef::string().non_null())
.resolve(|_| Box::pin(async { Ok(Value::from("ok")) })),
);
let mut builder = Schema::builder();
builder.add_type(user.into());
builder.add_type(query.into());
builder.query("Query").build().expect("valid schema")
}
fn build_plan<'a>(schema: &Schema, document: &'a ast::Document<'a>) -> PreparedPlan<'a> {
let errors = crate::validate(
schema,
document,
&ValidationOptions {
introspection: true,
max_depth: 0,
max_errors: 100,
},
);
assert!(errors.is_empty(), "{errors:?}");
let operation = document.operations().next().expect("operation");
build(schema, document, operation, schema.query_type())
}
fn variables(fields: impl IntoIterator<Item = (&'static str, Value)>) -> Object {
Object::from_fields(fields)
}
#[test]
fn concrete_paths_are_planned() {
let schema = schema();
let document = parse_query(
r#"
query {
user(id: "1") {
id
called: name
...UserFields
friends { id name }
}
health
}
fragment UserFields on User { age }
"#,
)
.expect("parses");
let plan = build_plan(&schema, &document);
let root = plan
.field_plan(0, schema.query_type())
.expect("root is planned");
assert!(root.has_resolvers);
let keys: Vec<_> = root.groups.iter().map(|group| group.response_key).collect();
assert_eq!(keys, ["user", "health"]);
let root_variant = root.groups[0].variant(&Object::new()).expect("variant");
let child = root_variant.child.expect("user has a child source");
let user = schema.object_type("User").expect("user type");
let user_plan = plan.field_plan(child, user).expect("user is planned");
assert!(!user_plan.has_resolvers);
let keys: Vec<_> = user_plan
.groups
.iter()
.map(|group| group.response_key)
.collect();
assert_eq!(keys, ["id", "called", "age", "friends"]);
assert!(user_plan.groups[1].alias_key.is_some());
assert_eq!(user_plan.groups[2].nodes.len(), 1);
assert!(user_plan.groups[0].property.is_some());
assert!(user_plan.groups[3].property.is_none());
}
#[test]
fn resolver_check_covers_only_selected_fields() {
let schema = schema();
let document = parse_query("query { user { id name } }").expect("parses");
let plan = build_plan(&schema, &document);
let root = plan.field_plan(0, schema.query_type()).expect("root");
assert!(root.has_resolvers);
let child = root.groups[0]
.variant(&Object::new())
.expect("variant")
.child
.expect("child source");
let user = schema.object_type("User").expect("user type");
assert!(
!plan
.field_plan(child, user)
.expect("user plan")
.has_resolvers
);
let document = parse_query("query { user { id score } }").expect("parses");
let plan = build_plan(&schema, &document);
let root = plan.field_plan(0, schema.query_type()).expect("root");
let child = root.groups[0]
.variant(&Object::new())
.expect("variant")
.child
.expect("child source");
assert!(
plan.field_plan(child, user)
.expect("user plan")
.has_resolvers
);
}
#[test]
fn exact_projection_reuses_the_parent_object() {
let schema = schema();
let document = parse_query(
r#"
query {
user { id name age }
}
"#,
)
.expect("parses");
let plan = build_plan(&schema, &document);
let root = plan.field_plan(0, schema.query_type()).expect("root");
let child = root.groups[0]
.variant(&Object::new())
.expect("variant")
.child
.expect("child source");
let user = schema.object_type("User").expect("user type");
let user_plan = plan.field_plan(child, user).expect("user plan");
assert!(user_plan.identity_projection);
let exact = Object::from_fields([
("id", Value::from("1")),
("name", Value::from("Alice")),
("age", Value::Int(30)),
]);
assert!(user_plan.matches_parent(&exact));
let reordered = Object::from_fields([
("name", Value::from("Alice")),
("id", Value::from("1")),
("age", Value::Int(30)),
]);
assert!(!user_plan.matches_parent(&reordered));
let extra = Object::from_fields([
("id", Value::from("1")),
("name", Value::from("Alice")),
("age", Value::Int(30)),
("friends", Value::List(Vec::new())),
]);
assert!(!user_plan.matches_parent(&extra));
let null_age = Object::from_fields([
("id", Value::from("1")),
("name", Value::from("Alice")),
("age", Value::Null),
]);
assert!(!user_plan.matches_parent(&null_age));
}
#[test]
fn conditional_groups_precompute_variants() {
let schema = schema();
let document = parse_query(
r#"
query ($show: Boolean!, $hide: Boolean!) {
user {
id
name @include(if: $show)
age @skip(if: $hide)
}
}
"#,
)
.expect("parses");
let plan = build_plan(&schema, &document);
let root = plan.field_plan(0, schema.query_type()).expect("root");
let child = root.groups[0]
.variant(&Object::new())
.expect("variant")
.child
.expect("child source");
let user = schema.object_type("User").expect("user type");
let user_plan = plan.field_plan(child, user).expect("user plan");
let name = &user_plan.groups[1];
assert_eq!(name.variants.len(), 2);
assert!(
name.variant(&Object::new())
.expect("mask 0")
.nodes
.is_empty()
);
assert_eq!(
name.variant(&variables([("show", Value::Boolean(true))]))
.expect("mask 1")
.nodes
.len(),
1
);
let age = &user_plan.groups[2];
assert_eq!(age.variants.len(), 2);
assert_eq!(
age.variant(&variables([("hide", Value::Boolean(false))]))
.expect("included")
.nodes
.len(),
1
);
assert!(
age.variant(&variables([("hide", Value::Boolean(true))]))
.expect("excluded")
.nodes
.is_empty()
);
}
#[test]
fn conditional_fragment_spread_stays_dynamic() {
let schema = schema();
let document = parse_query(
r#"
query ($on: Boolean!) {
user { ...Fields @include(if: $on) ...Fields }
}
fragment Fields on User { id name }
"#,
)
.expect("parses");
let plan = build_plan(&schema, &document);
let root = plan.field_plan(0, schema.query_type()).expect("root");
let child = root.groups[0]
.variant(&Object::new())
.expect("variant")
.child
.expect("child source");
let user = schema.object_type("User").expect("user type");
assert!(plan.field_plan(child, user).is_none());
assert!(plan.source_sets(child).next().is_some());
}
#[test]
fn argument_templates_are_compiled_only_when_static() {
let schema = schema();
let document = parse_query(
r#"
query ($id: ID) {
a: user(id: "1") { id }
b: user(id: $id) { id }
}
"#,
)
.expect("parses");
let plan = build_plan(&schema, &document);
let root = plan.field_plan(0, schema.query_type()).expect("root");
let template = root.groups[0].args.as_ref().expect("literal template");
let mut args = Object::new();
template.fill(&Object::new(), &mut args).expect("fills");
assert_eq!(args.get("id"), Some(&Value::from("1")));
let template = root.groups[1].args.as_ref().expect("variable template");
let mut args = Object::new();
template
.fill(&variables([("id", Value::from("7"))]), &mut args)
.expect("fills");
assert_eq!(args.get("id"), Some(&Value::from("7")));
let mut args = Object::new();
template.fill(&Object::new(), &mut args).expect("fills");
assert!(args.get("id").is_none());
}
}