Skip to content

Commit

Permalink
Add the ability to use $args and $this in headers
Browse files Browse the repository at this point in the history
  • Loading branch information
pubmodmatt committed Oct 24, 2024
1 parent 82e262b commit c6e26e4
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
7 changes: 6 additions & 1 deletion apollo-federation/src/sources/connect/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ fn identifier(input: &str) -> IResult<&str, &str> {
}

fn namespace(input: &str) -> IResult<&str, &str> {
recognize(alt((tag("$config"), tag("$context"))))(input)
recognize(alt((
tag("$args"),
tag("$config"),
tag("$context"),
tag("$this"),
)))(input)
}

fn path(input: &str) -> IResult<&str, &str> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type Query
{
users: [User] @join__field(graph: CONNECTORS) @join__directive(graphs: [CONNECTORS], name: "connect", args: {source: "json", http: {GET: "/users", headers: [{name: "x-new-name", from: "x-rename-connect"}, {name: "x-insert-multi-value", value: "first,second"}, {name: "x-config-variable-connect", value: "before {$config.connect.val} after"}, {name: "x-context-value-connect", value: "before {$context.val} after"}]}, selection: "id name"})
me: User @join__field(graph: CONNECTORS) @join__directive(graphs: [CONNECTORS], name: "connect", args: {source: "json", http: {GET: "/users/{$config.id}"}, selection: "id\nname\nusername"})
user(id: ID!): User @join__field(graph: CONNECTORS) @join__directive(graphs: [CONNECTORS], name: "connect", args: {source: "json", http: {GET: "/users/{$args.id}"}, selection: "id\nname\nusername", entity: true})
user(id: ID!): User @join__field(graph: CONNECTORS) @join__directive(graphs: [CONNECTORS], name: "connect", args: {source: "json", http: {GET: "/users/{$args.id}", headers: [{name: "x-from-args", value: "before {$args.id} after"}]}, selection: "id\nname\nusername", entity: true})
posts: [Post] @join__field(graph: CONNECTORS) @join__directive(graphs: [CONNECTORS], name: "connect", args: {source: "json", http: {GET: "/posts"}, selection: "id title user: { id: userId }"})
}

Expand All @@ -84,6 +84,7 @@ type User
id: ID!
name: String @join__field(graph: CONNECTORS)
username: String @join__field(graph: CONNECTORS)
nickname: String @join__field(graph: CONNECTORS) @join__directive(graphs: [CONNECTORS], name: "connect", args: {source: "json", http: {GET: "/users/{$this.id}/nicknames", headers: [{name: "x-from-this", value: "before {$this.id} after"}]}, selection: "nickname: $.first"})
c: String @join__field(graph: CONNECTORS, external: true) @join__field(graph: GRAPHQL)
d: String @join__field(graph: CONNECTORS, requires: "c") @join__directive(graphs: [CONNECTORS], name: "connect", args: {source: "json", http: {GET: "/users/{$this.c}"}, selection: "$.phone"})
}
20 changes: 18 additions & 2 deletions apollo-router/src/plugins/connectors/testdata/steelthread.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# rover supergraph compose --config apollo-router/src/plugins/connectors/testdata/steelthread.yaml > apollo-router/src/plugins/connectors/testdata/steelthread.graphql
federation_version: =2.10.0-alpha.0
federation_version: =2.10.0-preview.0
subgraphs:
connectors:
routing_url: none
Expand Down Expand Up @@ -57,7 +57,12 @@ subgraphs:
user(id: ID!): User
@connect(
source: "json"
http: { GET: "/users/{$$args.id}" }
http: {
GET: "/users/{$$args.id}"
headers: [
{name: "x-from-args" value: "before {$$args.id} after"}
]
}
selection: """
id
name
Expand All @@ -78,6 +83,17 @@ subgraphs:
id: ID!
name: String
username: String
nickname: String
@connect(
source: "json"
http: {
GET: "/users/{$$this.id}/nicknames"
headers: [
{name: "x-from-this" value: "before {$$this.id} after"}
]
}
selection: "$.first"
)
c: String @external
d: String
@requires(fields: "c")
Expand Down
45 changes: 45 additions & 0 deletions apollo-router/src/plugins/connectors/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ pub(crate) mod mock_api {
)
}

pub(crate) fn user_2_nicknames() -> Mock {
Mock::given(method("GET"))
.and(path("/users/2/nicknames"))
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!(["cat"])))
}

pub(crate) fn users_error() -> Mock {
Mock::given(method("GET")).and(path("/users")).respond_with(
ResponseTemplate::new(404).set_body_json(serde_json::json!([
Expand Down Expand Up @@ -668,6 +674,45 @@ async fn test_headers() {
);
}

#[tokio::test]
async fn test_args_and_this_in_header() {
let mock_server = MockServer::start().await;
mock_api::user_2().mount(&mock_server).await;
mock_api::user_2_nicknames().mount(&mock_server).await;

execute(
STEEL_THREAD_SCHEMA,
&mock_server.uri(),
"query { user(id: 2){ id nickname } }",
Default::default(),
None,
|_| {},
)
.await;

req_asserts::matches(
&mock_server.received_requests().await.unwrap(),
vec![
Matcher::new()
.method("GET")
.header(
HeaderName::from_str("x-from-args").unwrap(),
HeaderValue::from_str("before 2 after").unwrap(),
)
.path("/users/2")
.build(),
Matcher::new()
.method("GET")
.header(
HeaderName::from_str("x-from-this").unwrap(),
HeaderValue::from_str("before 2 after").unwrap(),
)
.path("/users/2/nicknames")
.build(),
],
);
}

mock! {
Subscriber {}
impl tracing_core::Subscriber for Subscriber {
Expand Down

0 comments on commit c6e26e4

Please sign in to comment.