Skip to main content

apollo_federation/composition/
satisfiability.rs

1mod conditions_validation;
2mod satisfiability_error;
3mod validation_context;
4mod validation_state;
5mod validation_traversal;
6
7use std::sync::Arc;
8
9use tracing::instrument;
10use tracing::trace;
11
12use crate::api_schema;
13use crate::composition::CompositionFailure;
14use crate::composition::CompositionOptions;
15use crate::composition::satisfiability::validation_traversal::ValidationTraversal;
16use crate::error::CompositionError;
17use crate::error::FederationError;
18use crate::query_graph::QueryGraph;
19use crate::query_graph::build_federated_query_graph;
20use crate::query_graph::build_supergraph_api_query_graph;
21use crate::schema::ValidFederationSchema;
22use crate::supergraph::CompositionHint;
23use crate::supergraph::Merged;
24use crate::supergraph::Satisfiable;
25use crate::supergraph::Supergraph;
26
27#[instrument(skip(supergraph, options))]
28pub fn validate_satisfiability(
29    mut supergraph: Supergraph<Merged>,
30    options: &CompositionOptions,
31) -> Result<Supergraph<Satisfiable>, CompositionFailure> {
32    let supergraph_schema = supergraph.schema().clone();
33    let mut errors = vec![];
34    let mut hints = supergraph.hints_mut().drain(..).collect();
35    if let Err(e) = validate_satisfiability_inner(supergraph, options, &mut errors, &mut hints) {
36        return Err(CompositionFailure {
37            errors: vec![CompositionError::InternalError {
38                message: e.to_string(),
39            }],
40            hints,
41        });
42    }
43    if !errors.is_empty() {
44        return Err(CompositionFailure { errors, hints });
45    }
46    Ok(Supergraph::<Satisfiable>::new(supergraph_schema, hints))
47}
48
49fn validate_satisfiability_inner(
50    supergraph: Supergraph<Merged>,
51    options: &CompositionOptions,
52    errors: &mut Vec<CompositionError>,
53    hints: &mut Vec<CompositionHint>,
54) -> Result<(), FederationError> {
55    let supergraph_schema = supergraph.schema();
56    let api_schema = api_schema::to_api_schema(supergraph_schema.clone(), Default::default())?;
57
58    trace!("Building API query graph");
59    let api_schema_query_graph =
60        build_supergraph_api_query_graph(supergraph_schema.clone(), api_schema.clone())?;
61    trace!("Building federated query graph");
62    let federated_query_graph = build_federated_query_graph(
63        supergraph_schema.clone(),
64        api_schema.clone(),
65        Some(true),
66        Some(false),
67    )?;
68    trace!("Validating graph composition");
69    validate_graph_composition(
70        supergraph_schema.clone(),
71        Arc::new(api_schema_query_graph),
72        Arc::new(federated_query_graph),
73        options,
74        errors,
75        hints,
76    )?;
77    Ok(())
78}
79
80/// Validates that all the queries expressible on the API schema resulting from the composition of
81/// a set of subgraphs can be executed on those subgraphs.
82fn validate_graph_composition(
83    // The supergraph schema generated by composition of the subgraph schemas.
84    supergraph_schema: ValidFederationSchema,
85    // The query graph of the API schema generated by the supergraph schema.
86    api_schema_query_graph: Arc<QueryGraph>,
87    // The federated query graph corresponding to the composed subgraphs.
88    federated_query_graph: Arc<QueryGraph>,
89    composition_options: &CompositionOptions,
90    errors: &mut Vec<CompositionError>,
91    hints: &mut Vec<CompositionHint>,
92) -> Result<(), FederationError> {
93    ValidationTraversal::new(
94        supergraph_schema,
95        api_schema_query_graph,
96        federated_query_graph,
97        composition_options,
98    )?
99    .validate(errors, hints)
100}
101
102#[cfg(test)]
103mod tests {
104    use super::*;
105
106    const TEST_SUPERGRAPH: &str = r#"
107schema
108  @link(url: "https://specs.apollo.dev/link/v1.0")
109  @link(url: "https://specs.apollo.dev/join/v0.5", for: EXECUTION)
110  @link(url: "https://specs.apollo.dev/context/v0.1", for: SECURITY)
111{
112  query: Query
113}
114
115directive @context(name: String!) repeatable on INTERFACE | OBJECT | UNION
116
117directive @context__fromContext(field: context__ContextFieldValue) on ARGUMENT_DEFINITION
118
119directive @join__directive(graphs: [join__Graph!], name: String!, args: join__DirectiveArguments) repeatable on SCHEMA | OBJECT | INTERFACE | FIELD_DEFINITION
120
121directive @join__enumValue(graph: join__Graph!) repeatable on ENUM_VALUE
122
123directive @join__field(graph: join__Graph, requires: join__FieldSet, provides: join__FieldSet, type: String, external: Boolean, override: String, usedOverridden: Boolean, overrideLabel: String, contextArguments: [join__ContextArgument!]) repeatable on FIELD_DEFINITION | INPUT_FIELD_DEFINITION
124
125directive @join__graph(name: String!, url: String!) on ENUM_VALUE
126
127directive @join__implements(graph: join__Graph!, interface: String!) repeatable on OBJECT | INTERFACE
128
129directive @join__type(graph: join__Graph!, key: join__FieldSet, extension: Boolean! = false, resolvable: Boolean! = true, isInterfaceObject: Boolean! = false) repeatable on OBJECT | INTERFACE | UNION | ENUM | INPUT_OBJECT | SCALAR
130
131directive @join__unionMember(graph: join__Graph!, member: String!) repeatable on UNION
132
133directive @link(url: String, as: String, for: link__Purpose, import: [link__Import]) repeatable on SCHEMA
134
135scalar context__ContextFieldValue
136
137interface I
138  @join__type(graph: A, key: "id")
139  @join__type(graph: B, key: "id")
140  @context(name: "A__contextI")
141{
142  id: ID!
143  value: Int! @join__field(graph: A)
144}
145
146input join__ContextArgument {
147  name: String!
148  type: String!
149  context: String!
150  selection: join__FieldValue!
151}
152
153scalar join__DirectiveArguments
154
155scalar join__FieldSet
156
157scalar join__FieldValue
158
159enum join__Graph {
160  A @join__graph(name: "A", url: "http://A")
161  B @join__graph(name: "B", url: "http://B")
162}
163
164scalar link__Import
165
166enum link__Purpose {
167  """
168  `SECURITY` features provide metadata necessary to securely resolve fields.
169  """
170  SECURITY
171
172  """
173  `EXECUTION` features provide metadata necessary for operation execution.
174  """
175  EXECUTION
176}
177
178type P
179  @join__type(graph: A, key: "id")
180  @join__type(graph: B, key: "id")
181{
182  id: ID!
183  data: String! @join__field(graph: A, contextArguments: [{context: "A__contextI", name: "onlyInA", type: "Int", selection: " { value }"}])
184}
185
186type Query
187  @join__type(graph: A)
188  @join__type(graph: B)
189{
190  start: I! @join__field(graph: B)
191}
192
193type T implements I
194  @join__implements(graph: A, interface: "I")
195  @join__implements(graph: B, interface: "I")
196  @join__type(graph: A, key: "id")
197  @join__type(graph: B, key: "id")
198{
199  id: ID!
200  value: Int! @join__field(graph: A)
201  onlyInA: Int! @join__field(graph: A)
202  p: P! @join__field(graph: A)
203  sharedField: Int!
204  onlyInB: Int! @join__field(graph: B)
205}
206    "#;
207
208    #[test]
209    fn test_satisfiability_basic() {
210        let supergraph = Supergraph::parse(TEST_SUPERGRAPH).unwrap();
211        _ = validate_satisfiability(supergraph, &CompositionOptions::default())
212            .expect("Supergraph should be satisfiable");
213    }
214}