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