Skip to content

Commit

Permalink
Update exiting variables test to also use request headers and respons…
Browse files Browse the repository at this point in the history
…e headers
  • Loading branch information
andrewmcgivery committed Jan 22, 2025
1 parent 1c1dc53 commit 665ebb1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions apollo-federation/src/sources/connect/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl<'schema> VariableContext<'schema> {
Namespace::Context,
Namespace::Status,
Namespace::This,
Namespace::Request,
Namespace::Response,
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.10")
@link(
url: "https://specs.apollo.dev/connect/v0.1"
import: ["@connect", "@source"]
)
@link(url: "https://specs.apollo.dev/connect/v0.1", import: ["@connect", "@source"])
@source(
name: "v1"
http: {
Expand All @@ -20,7 +17,7 @@ type Query {
@connect(
source: "v1"
http: {
POST: "/f?arg={$args.arg->slice(1)}&context={$context.value}&config={$config.value}"
POST: "/f?arg={$args.arg->slice(1)}&context={$context.value}&config={$config.value}&header={$request.headers.value}"
headers: [
{ name: "x-connect-context", value: "{$context.value}" }
{ name: "x-connect-config", value: "{$config.value}" }
Expand All @@ -30,6 +27,7 @@ type Query {
arg: $args.arg
context: $context.value
config: $config.value
request: $request.headers.value
"""
}
selection: """
Expand All @@ -39,6 +37,8 @@ type Query {
status: $status
sibling: $("D")
extra: $->echo({ arg: $args.arg, context: $context.value, config: $config.value, status: $status })
request: $request.headers.value
response: $response.headers.value
"""
)
}
Expand All @@ -50,6 +50,8 @@ type T {
status: Int
sibling: String
extra: JSON
request: String
response: String
f(arg: String): U
@connect(
source: "v1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ enum link__Purpose {
type Query
@join__type(graph: CONNECTORS)
{
f(arg: String!): T @join__directive(graphs: [CONNECTORS], name: "connect", args: {source: "v1", http: {POST: "/f?arg={$args.arg->slice(1)}&context={$context.value}&config={$config.value}", headers: [{name: "x-connect-context", value: "{$context.value}"}, {name: "x-connect-config", value: "{$config.value}"}, {name: "x-connect-arg", value: "{$args.arg->last}"}], body: "arg: $args.arg\ncontext: $context.value\nconfig: $config.value"}, selection: "arg: $args.arg\ncontext: $context.value\nconfig: $config.value\nstatus: $status\nsibling: $(\"D\")\nextra: $->echo({ arg: $args.arg, context: $context.value, config: $config.value, status: $status })"})
f(arg: String!): T @join__directive(graphs: [CONNECTORS], name: "connect", args: {source: "v1", http: {POST: "/f?arg={$args.arg->slice(1)}&context={$context.value}&config={$config.value}&header={$request.headers.value}", headers: [{name: "x-connect-context", value: "{$context.value}"}, {name: "x-connect-config", value: "{$config.value}"}, {name: "x-connect-arg", value: "{$args.arg->last}"}], body: "arg: $args.arg\ncontext: $context.value\nconfig: $config.value\nrequest: $request.headers.value"}, selection: "arg: $args.arg\ncontext: $context.value\nconfig: $config.value\nstatus: $status\nsibling: $(\"D\")\nextra: $->echo({ arg: $args.arg, context: $context.value, config: $config.value, status: $status })\nrequest: $request.headers.value\nresponse: $response.headers.value"})
}

type T
Expand All @@ -73,6 +73,8 @@ type T
status: Int
sibling: String
extra: JSON
request: String
response: String
f(arg: String): U @join__directive(graphs: [CONNECTORS], name: "connect", args: {source: "v1", http: {POST: "/f?arg={$args.arg->slice(2)}&context={$context.value}&config={$config.value}&sibling={$this.sibling}", headers: [{name: "x-connect-context", value: "{$context.value}"}, {name: "x-connect-config", value: "{$config.value}"}, {name: "x-connect-arg", value: "{$args.arg->first}"}, {name: "x-connect-sibling", value: "{$this.sibling}"}], body: "arg: $args.arg\ncontext: $context.value\nconfig: $config.value\nsibling: $this.sibling"}, selection: "arg: $args.arg\ncontext: $context.value\nconfig: $config.value\nsibling: $this.sibling\nstatus: $status"})
}

Expand Down
19 changes: 13 additions & 6 deletions apollo-router/src/plugins/connectors/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,11 @@ async fn test_variables() {
.await;
Mock::given(method("POST"))
.and(path("/f"))
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({})))
.respond_with(
ResponseTemplate::new(200)
.set_body_json(serde_json::json!({}))
.insert_header("value", "myothercoolheader"),
)
.mount(&mock_server)
.await;
let uri = mock_server.uri();
Expand Down Expand Up @@ -1490,11 +1494,14 @@ async fn test_variables() {
}
}
})),
|_| {},
|request| {
let headers = request.router_request.headers_mut();
headers.insert("value", "coolheader".parse().unwrap());
},
)
.await;

insta::assert_json_snapshot!(response, @r###"
insta::assert_json_snapshot!(response, @r#"
{
"data": {
"f": {
Expand All @@ -1519,7 +1526,7 @@ async fn test_variables() {
}
}
}
"###);
"#);

req_asserts::matches(
&mock_server.received_requests().await.unwrap(),
Expand All @@ -1528,13 +1535,13 @@ async fn test_variables() {
Matcher::new()
.method("POST")
.path("/f")
.query("arg=rg&context=B&config=C")
.query("arg=rg&context=B&config=C&header=coolheader")
.header("x-source-context".into(), "B".try_into().unwrap())
.header("x-source-config".into(), "C".try_into().unwrap())
.header("x-connect-arg".into(), "g".try_into().unwrap())
.header("x-connect-context".into(), "B".try_into().unwrap())
.header("x-connect-config".into(), "C".try_into().unwrap())
.body(serde_json::json!({ "arg": "arg", "context": "B", "config": "C" }))
.body(serde_json::json!({ "arg": "arg", "context": "B", "config": "C", "request": "coolheader" }))
,
Matcher::new()
.method("POST")
Expand Down

0 comments on commit 665ebb1

Please sign in to comment.