Skip to content

Commit 5a0826e

Browse files
clenfesto0Ignition0ogarypen
authored
Add ability for router to deal with query plans with contextual rewrites (#5097)
Two main things that we're doing in this PR. 1. We've added a variable to FetchNode called `context_rewrites`. This is a vector of DataRewrite::KeyRenamer that are specifically taking data from their path (which will be relative and can traverse up the data path) and writes the data into an argument that is passed to the selection set. 2. There are two cases. In the most straightforward, the data that is passed to the selection set is the same for every entity. This case is pretty easy and doesn't require any special handling. In the second case, the value of the variable may be different per entity. If that is true, we need to use aliasing and duplication in our query in order to send it to subgraphs. Once graphql/composite-schemas-spec#25 is decided and has subgraph support, this query cloning will be able to go away. Co-authored-by: o0Ignition0o <[email protected]> Co-authored-by: Gary Pennington <[email protected]>
1 parent d34c70b commit 5a0826e

File tree

51 files changed

+2953
-150
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2953
-150
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### Add ability for router to deal with query plans with contextual rewrites ([PR #5097](https://github.com/apollographql/router/pull/5097))
2+
3+
Adds the ability for the router to execute query plans with context rewrites on them. These are generated by the @fromContext directive and are used to map a Value in the collected data JSON onto a variable which will in turn be used as an argument to a field resolver.
4+
5+
⚠️ This ships with a new version of federation, which means distributed caches will be repopulated.
6+
7+
By [@clenfest](https://github.com/clenfest) in https://github.com/apollographql/router/pull/5097

.config/nextest.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ path = "junit.xml"
1919
# Integration tests require more than one thread. The default setting of 1 will cause too many integration tests to run
2020
# at the same time and causes tests to fail where timing is involved.
2121
# This filter applies only to to the integration tests in the apollo-router package.
22-
[[profile.default.overrides]]
23-
filter = 'package(apollo-router) & kind(test)'
24-
threads-required = 2
22+
[[profile.ci.overrides]]
23+
filter = 'test(/^apollo-router::/)'
24+
threads-required = 4

Cargo.lock

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,9 +1374,9 @@ checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
13741374

13751375
[[package]]
13761376
name = "bytes"
1377-
version = "1.5.0"
1377+
version = "1.6.0"
13781378
source = "registry+https://github.com/rust-lang/crates.io-index"
1379-
checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223"
1379+
checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9"
13801380

13811381
[[package]]
13821382
name = "bytes-utils"
@@ -1940,9 +1940,9 @@ dependencies = [
19401940

19411941
[[package]]
19421942
name = "curve25519-dalek"
1943-
version = "4.0.0"
1943+
version = "4.1.2"
19441944
source = "registry+https://github.com/rust-lang/crates.io-index"
1945-
checksum = "f711ade317dd348950a9910f81c5947e3d8907ebd2b83f76203ff1807e6a2bc2"
1945+
checksum = "0a677b8922c94e01bdbb12126b0bc852f00447528dee1782229af9c720c3f348"
19461946
dependencies = [
19471947
"cfg-if",
19481948
"cpufeatures",
@@ -2419,7 +2419,7 @@ dependencies = [
24192419
"digest 0.10.7",
24202420
"elliptic-curve 0.13.8",
24212421
"rfc6979 0.4.0",
2422-
"signature 2.0.0",
2422+
"signature 2.2.0",
24232423
"spki 0.7.2",
24242424
]
24252425

@@ -2659,9 +2659,9 @@ dependencies = [
26592659

26602660
[[package]]
26612661
name = "fiat-crypto"
2662-
version = "0.1.20"
2662+
version = "0.2.9"
26632663
source = "registry+https://github.com/rust-lang/crates.io-index"
2664-
checksum = "e825f6987101665dea6ec934c09ec6d721de7bc1bf92248e1d5810c8cd636b77"
2664+
checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d"
26652665

26662666
[[package]]
26672667
name = "filetime"
@@ -3954,9 +3954,9 @@ dependencies = [
39543954

39553955
[[package]]
39563956
name = "libz-ng-sys"
3957-
version = "1.1.12"
3957+
version = "1.1.15"
39583958
source = "registry+https://github.com/rust-lang/crates.io-index"
3959-
checksum = "3dd9f43e75536a46ee0f92b758f6b63846e594e86638c61a9251338a65baea63"
3959+
checksum = "c6409efc61b12687963e602df8ecf70e8ddacf95bc6576bcf16e3ac6328083c5"
39603960
dependencies = [
39613961
"cmake",
39623962
"libc",
@@ -4150,9 +4150,9 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
41504150

41514151
[[package]]
41524152
name = "miniz_oxide"
4153-
version = "0.7.1"
4153+
version = "0.7.2"
41544154
source = "registry+https://github.com/rust-lang/crates.io-index"
4155-
checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7"
4155+
checksum = "9d811f3e15f28568be3407c8e7fdb6514c1cda3cb30683f15b6a1a1dc4ea14a7"
41564156
dependencies = [
41574157
"adler",
41584158
]
@@ -5195,7 +5195,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
51955195
checksum = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919"
51965196
dependencies = [
51975197
"once_cell",
5198-
"toml_edit 0.19.14",
5198+
"toml_edit 0.19.15",
51995199
]
52005200

52015201
[[package]]
@@ -5776,9 +5776,9 @@ dependencies = [
57765776

57775777
[[package]]
57785778
name = "router-bridge"
5779-
version = "0.5.21+v2.7.5"
5779+
version = "0.5.24+v2.8.0-alpha.1"
57805780
source = "registry+https://github.com/rust-lang/crates.io-index"
5781-
checksum = "b2142445fe3fe2aae7a3c3c5083d1211a448a0dabb489a14dd90d427cf6c0b13"
5781+
checksum = "4a4b92a40b68c797d2d624716d5671e80de578f00c5012af37f19c74b175566b"
57825782
dependencies = [
57835783
"anyhow",
57845784
"async-channel 1.9.0",
@@ -6149,9 +6149,9 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
61496149

61506150
[[package]]
61516151
name = "serde"
6152-
version = "1.0.197"
6152+
version = "1.0.199"
61536153
source = "registry+https://github.com/rust-lang/crates.io-index"
6154-
checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2"
6154+
checksum = "0c9f6e76df036c77cd94996771fb40db98187f096dd0b9af39c6c6e452ba966a"
61556155
dependencies = [
61566156
"serde_derive",
61576157
]
@@ -6167,9 +6167,9 @@ dependencies = [
61676167

61686168
[[package]]
61696169
name = "serde_derive"
6170-
version = "1.0.197"
6170+
version = "1.0.199"
61716171
source = "registry+https://github.com/rust-lang/crates.io-index"
6172-
checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b"
6172+
checksum = "11bd257a6541e141e42ca6d24ae26f7714887b47e89aa739099104c7e4d3b7fc"
61736173
dependencies = [
61746174
"proc-macro2 1.0.76",
61756175
"quote 1.0.35",
@@ -6201,9 +6201,9 @@ dependencies = [
62016201

62026202
[[package]]
62036203
name = "serde_json"
6204-
version = "1.0.114"
6204+
version = "1.0.116"
62056205
source = "registry+https://github.com/rust-lang/crates.io-index"
6206-
checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0"
6206+
checksum = "3e17db7126d17feb94eb3fad46bf1a96b034e8aacbc2e775fe81505f8b0b2813"
62076207
dependencies = [
62086208
"indexmap 2.2.3",
62096209
"itoa",
@@ -6385,9 +6385,9 @@ dependencies = [
63856385

63866386
[[package]]
63876387
name = "signature"
6388-
version = "2.0.0"
6388+
version = "2.2.0"
63896389
source = "registry+https://github.com/rust-lang/crates.io-index"
6390-
checksum = "8fe458c98333f9c8152221191a77e2a44e8325d0193484af2e9421a53019e57d"
6390+
checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de"
63916391
dependencies = [
63926392
"digest 0.10.7",
63936393
"rand_core 0.6.4",
@@ -7070,9 +7070,9 @@ dependencies = [
70707070

70717071
[[package]]
70727072
name = "toml_edit"
7073-
version = "0.19.14"
7073+
version = "0.19.15"
70747074
source = "registry+https://github.com/rust-lang/crates.io-index"
7075-
checksum = "f8123f27e969974a3dfba720fdb560be359f57b44302d280ba72e76a74480e8a"
7075+
checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421"
70767076
dependencies = [
70777077
"indexmap 2.2.3",
70787078
"toml_datetime",
@@ -7609,9 +7609,9 @@ checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460"
76097609

76107610
[[package]]
76117611
name = "unicode-id"
7612-
version = "0.3.3"
7612+
version = "0.3.4"
76137613
source = "registry+https://github.com/rust-lang/crates.io-index"
7614-
checksum = "d70b6494226b36008c8366c288d77190b3fad2eb4c10533139c1c1f461127f1a"
7614+
checksum = "b1b6def86329695390197b82c1e244a54a131ceb66c996f2088a3876e2ae083f"
76157615

76167616
[[package]]
76177617
name = "unicode-ident"
@@ -8205,7 +8205,7 @@ version = "2.0.0"
82058205
source = "registry+https://github.com/rust-lang/crates.io-index"
82068206
checksum = "fb66477291e7e8d2b0ff1bcb900bf29489a9692816d79874bea351e7a8b6de96"
82078207
dependencies = [
8208-
"curve25519-dalek 4.0.0",
8208+
"curve25519-dalek 4.1.2",
82098209
"rand_core 0.6.4",
82108210
"serde",
82118211
"zeroize",

apollo-federation/src/link/join_spec_definition.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,10 @@ lazy_static! {
336336
Version { major: 0, minor: 3 },
337337
Some(Version { major: 2, minor: 0 }),
338338
));
339+
definitions.add(JoinSpecDefinition::new(
340+
Version { major: 0, minor: 5 },
341+
Some(Version { major: 2, minor: 8 }),
342+
));
339343
definitions
340344
};
341345

apollo-federation/src/query_plan/display.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ impl FetchNode {
8383
operation_kind: _,
8484
input_rewrites: _,
8585
output_rewrites: _,
86+
context_rewrites: _,
8687
} = self;
8788
state.write(format_args!("Fetch(service: {subgraph_name:?}"))?;
8889
if let Some(id) = id {

apollo-federation/src/query_plan/fetch_dependency_graph.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,7 @@ impl FetchDependencyGraphNode {
13201320
operation_kind: self.root_kind.into(),
13211321
input_rewrites: self.input_rewrites.clone(),
13221322
output_rewrites,
1323+
context_rewrites: Default::default(),
13231324
}));
13241325

13251326
Ok(Some(if let Some(path) = self.merge_at.clone() {

apollo-federation/src/query_plan/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ pub struct FetchNode {
8686
/// Similar to `input_rewrites`, but for optional "rewrites" to apply to the data that is
8787
/// received from a fetch (and before it is applied to the current in-memory results).
8888
pub output_rewrites: Vec<Arc<FetchDataRewrite>>,
89+
/// Similar to the other kinds of rewrites. This is a mechanism to convert a contextual path into
90+
/// an argument to a resolver
91+
pub context_rewrites: Vec<Arc<FetchDataRewrite>>,
8992
}
9093

9194
#[derive(Debug, Clone)]

apollo-federation/src/query_plan/query_planner.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ impl QueryPlanner {
349349
requires: Default::default(),
350350
input_rewrites: Default::default(),
351351
output_rewrites: Default::default(),
352+
context_rewrites: Default::default(),
352353
};
353354

354355
return Ok(QueryPlan::new(node, statistics));

apollo-federation/tests/query_plan/build_query_plan_tests.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,6 @@ fn pick_keys_that_minimize_fetches() {
153153
/// (more precisely, this force the query planner to _consider_ type explosion; the generated
154154
/// query plan still ends up not type-exploding in practice since as it's not necessary).
155155
#[test]
156-
#[should_panic(expected = "snapshot assertion")]
157-
// TODO: investigate this failure
158156
fn field_covariance_and_type_explosion() {
159157
let planner = planner!(
160158
Subgraph1: r#"
@@ -195,23 +193,22 @@ fn field_covariance_and_type_explosion() {
195193
}
196194
"#,
197195
@r###"
198-
QueryPlan {
199-
Fetch(service: "Subgraph1") {
200-
{
201-
dummy {
196+
QueryPlan {
197+
Fetch(service: "Subgraph1") {
198+
{
199+
dummy {
200+
field {
201+
__typename
202+
... on Object {
203+
field {
202204
__typename
203-
field {
204-
__typename
205-
... on Object {
206-
field {
207-
__typename
208-
}
209-
}
210-
}
211205
}
212206
}
213-
},
207+
}
214208
}
215-
"###
209+
}
210+
},
211+
}
212+
"###
216213
);
217214
}

0 commit comments

Comments
 (0)