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