Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move preview_connectors config to connectors for GA #6699

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apollo-router/src/configuration/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub(crate) struct ConnectorConfiguration<T>
where
T: Serialize + JsonSchema,
{
// Map of subgraph_name.connector_source_name to configuration
/// Map of subgraph_name.connector_source_name to configuration
#[serde(default)]
pub(crate) sources: HashMap<String, T>,
}
2 changes: 1 addition & 1 deletion apollo-router/src/configuration/expansion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ fn dev_mode_defaults() -> Vec<Override> {
.value_type(ValueType::Bool)
.build(),
Override::builder()
.config_path("preview_connectors.debug_extensions")
.config_path("connectors.debug_extensions")
.value(true)
.value_type(ValueType::Bool)
.build(),
Expand Down
2 changes: 1 addition & 1 deletion apollo-router/src/configuration/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ impl InstrumentData {

populate_config_instrument!(
apollo.router.config.connectors,
"$.preview_connectors",
"$.connectors",
opt.debug_extensions,
"$[?(@.debug_extensions == true)]",
opt.expose_sources_in_context,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
description: Apollo Connectors is no longer in preview
actions:
- type: move
from: preview_connectors
to: connectors
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
description: Apollo Connectors GA has replaced `subgraphs` configuration with `sources`
actions:
- type: log
level: warn
path: connectors.subgraphs
# TODO: add doc link about how to migrate subgraphs and $config
log: "In the General Availability (GA) release of Apollo Connectors, `subgraphs` has been replaced by `sources`. Please update your configuration."
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
source: apollo-router/src/configuration/expansion.rs
expression: value
---
connectors:
debug_extensions: true
homepage:
enabled: false
some_other_config: should remain
include_subgraph_errors:
all: true
plugins:
experimental.expose_query_plan: true
preview_connectors:
debug_extensions: true
sandbox:
enabled: true
supergraph:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,7 @@ expression: "&schema"
"description": "#/definitions/AuthConfig"
},
"default": {},
"description": "Map of subgraph_name.connector_source_name to configuration",
"type": "object"
}
},
Expand Down Expand Up @@ -2087,12 +2088,22 @@ expression: "&schema"
"nullable": true,
"type": "integer"
},
"sources": {
"additionalProperties": {
"$ref": "#/definitions/SourceConfiguration",
"description": "#/definitions/SourceConfiguration"
},
"default": {},
"description": "Map of subgraph_name.connector_source_name to source configuration",
"type": "object"
},
"subgraphs": {
"additionalProperties": {
"$ref": "#/definitions/SubgraphConnectorConfiguration",
"description": "#/definitions/SubgraphConnectorConfiguration"
},
"default": {},
"deprecated": true,
"description": "A map of subgraph name to connectors config for that subgraph",
"type": "object"
}
Expand Down Expand Up @@ -5622,6 +5633,12 @@ expression: "&schema"
"additionalProperties": false,
"description": "Configuration for a `@source` directive",
"properties": {
"$config": {
"additionalProperties": true,
"default": {},
"description": "Other values that can be used by connectors via `{$config.<key>}`",
"type": "object"
},
"max_requests_per_operation": {
"default": null,
"description": "The maximum number of requests for this source",
Expand Down Expand Up @@ -8653,6 +8670,10 @@ expression: "&schema"
"$ref": "#/definitions/Batching",
"description": "#/definitions/Batching"
},
"connectors": {
"$ref": "#/definitions/ConnectorsConfig",
"description": "#/definitions/ConnectorsConfig"
},
"coprocessor": {
"$ref": "#/definitions/Conf4",
"description": "#/definitions/Conf4"
Expand Down Expand Up @@ -8718,10 +8739,6 @@ expression: "&schema"
"$ref": "#/definitions/Plugins",
"description": "#/definitions/Plugins"
},
"preview_connectors": {
"$ref": "#/definitions/ConnectorsConfig",
"description": "#/definitions/ConnectorsConfig"
},
"preview_entity_cache": {
"$ref": "#/definitions/Config7",
"description": "#/definitions/Config7"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: apollo-router/src/configuration/tests.rs
expression: new_config
---
---
connectors:
debug_extensions: true
expose_sources_in_context: true
max_requests_per_operation_per_source: 50
subgraphs:
subgraph_name:
sources:
source_name:
override_url: "http://localhost:5280"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
preview_connectors:
connectors:
debug_extensions: true
expose_sources_in_context: true
max_requests_per_operation_per_source: 100
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
preview_connectors:
debug_extensions: true
expose_sources_in_context: true
max_requests_per_operation_per_source: 50
# If support for subgraphs is eventually removed, the following will need to be removed
subgraphs:
subgraph_name:
sources:
source_name:
override_url: http://localhost:5280
28 changes: 26 additions & 2 deletions apollo-router/src/plugins/connectors/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ use url::Url;

use super::incompatible::warn_incompatible_plugins;
use crate::plugins::connectors::plugin::PLUGIN_NAME;
use crate::services::connector_service::ConnectorSourceRef;
use crate::Configuration;

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub(crate) struct ConnectorsConfig {
/// A map of subgraph name to connectors config for that subgraph
#[serde(default)]
#[deprecated(note = "use `sources`")]
pub(crate) subgraphs: HashMap<String, SubgraphConnectorConfiguration>,

/// Map of subgraph_name.connector_source_name to source configuration
#[serde(default)]
pub(crate) sources: HashMap<String, SourceConfiguration>,

/// Enables connector debugging information on response extensions if the feature is enabled
#[serde(default)]
pub(crate) debug_extensions: bool,
Expand All @@ -43,6 +49,7 @@ pub(crate) struct ConnectorsConfig {
pub(crate) expose_sources_in_context: bool,
}

// TODO: remove this after deprecation period
/// Configuration for a connector subgraph
#[derive(Clone, Debug, Default, Deserialize, JsonSchema, Serialize)]
#[serde(deny_unknown_fields, default)]
Expand All @@ -64,6 +71,10 @@ pub(crate) struct SourceConfiguration {

/// The maximum number of requests for this source
pub(crate) max_requests_per_operation: Option<usize>,

/// Other values that can be used by connectors via `{$config.<key>}`
#[serde(rename = "$config")]
pub(crate) custom: CustomConfiguration,
}

/// Modifies connectors with values from the configuration
Expand All @@ -72,7 +83,7 @@ pub(crate) fn apply_config(
mut connectors: Connectors,
) -> Connectors {
// Enabling connectors might end up interfering with other router features, so we insert warnings
// into the logs for any incompatibilites found.
// into the logs for any incompatibilities found.
warn_incompatible_plugins(router_config, &connectors);

let Some(config) = router_config.apollo_plugins.plugins.get(PLUGIN_NAME) else {
Expand All @@ -83,6 +94,20 @@ pub(crate) fn apply_config(
};

for connector in Arc::make_mut(&mut connectors.by_service_name).values_mut() {
if let Ok(source_ref) = ConnectorSourceRef::try_from(&mut *connector) {
if let Some(source_config) = config.sources.get(&source_ref.to_string()) {
if let Some(url) = source_config.override_url.as_ref() {
connector.transport.source_url = Some(url.clone());
}
if let Some(max_requests) = source_config.max_requests_per_operation {
connector.max_requests = Some(max_requests);
}
connector.config = Some(source_config.custom.clone());
}
}

// TODO: remove this after deprecation period
#[allow(deprecated)]
let Some(subgraph_config) = config.subgraphs.get(&connector.id.subgraph_name) else {
continue;
};
Expand All @@ -99,7 +124,6 @@ pub(crate) fn apply_config(
connector.max_requests = Some(max_requests);
}
}

connector.config = Some(subgraph_config.custom.clone());
}
connectors
Expand Down
2 changes: 1 addition & 1 deletion apollo-router/src/plugins/connectors/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,6 @@ impl Plugin for Connectors {
}
}

pub(crate) const PLUGIN_NAME: &str = "preview_connectors";
pub(crate) const PLUGIN_NAME: &str = "connectors";

register_plugin!("apollo", PLUGIN_NAME, Connectors);
28 changes: 12 additions & 16 deletions apollo-router/src/plugins/connectors/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ async fn value_from_config() {
"query { me { id name username} }",
Default::default(),
Some(json!({
"preview_connectors": {
"subgraphs": {
"connectors": {
"connectors": {
"sources": {
"connectors.json": {
"$config": {
"id": 1,
}
Expand Down Expand Up @@ -108,7 +108,7 @@ async fn max_requests() {
"query { users { id name username } }",
Default::default(),
Some(json!({
"preview_connectors": {
"connectors": {
"max_requests_per_operation_per_source": 2
}
})),
Expand Down Expand Up @@ -173,7 +173,7 @@ async fn source_max_requests() {
"query { users { id name username } }",
Default::default(),
Some(json!({
"preview_connectors": {
"connectors": {
"subgraphs": {
"connectors": {
"sources": {
Expand Down Expand Up @@ -570,7 +570,7 @@ async fn test_headers() {
"query { users { id } }",
Default::default(),
Some(json!({
"preview_connectors": {
"connectors": {
"subgraphs": {
"connectors": {
"$config": {
Expand Down Expand Up @@ -1450,7 +1450,7 @@ async fn test_sources_in_context() {
"query Posts { posts { id body title author { name username } } }",
Default::default(),
Some(json!({
"preview_connectors": {
"connectors": {
"expose_sources_in_context": true
},
"coprocessor": {
Expand Down Expand Up @@ -1518,7 +1518,7 @@ async fn test_variables() {
"{ f(arg: \"arg\") { arg context config sibling status extra f(arg: \"arg\") { arg context config sibling status } } }",
Default::default(),
Some(json!({
"preview_connectors": {
"connectors": {
"subgraphs": {
"connectors": {
"$config": {
Expand Down Expand Up @@ -1804,14 +1804,10 @@ async fn execute(
let common_config = json!({
"include_subgraph_errors": { "all": true },
"override_subgraph_url": {"graphql": subgraph_uri},
"preview_connectors": {
"subgraphs": {
"connectors": {
"sources": {
"json": {
"override_url": connector_uri
}
}
"connectors": {
"sources": {
"connectors.json": {
"override_url": connector_uri
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion apollo-router/src/router_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ pub(crate) async fn create_plugins(
add_optional_apollo_plugin!("demand_control");

// This relative ordering is documented in `docs/source/customizations/native.mdx`:
add_optional_apollo_plugin!("preview_connectors");
add_optional_apollo_plugin!("connectors");
add_optional_apollo_plugin!("rhai");
add_optional_apollo_plugin!("coprocessor");
add_user_plugins!();
Expand Down
18 changes: 18 additions & 0 deletions apollo-router/src/services/connector_service.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Tower service for connectors.

use std::fmt::Display;
use std::str::FromStr;
use std::sync::Arc;
use std::task::Poll;
Expand Down Expand Up @@ -100,6 +101,23 @@ impl TryFrom<&Connector> for ConnectorSourceRef {
}
}

impl TryFrom<&mut Connector> for ConnectorSourceRef {
type Error = ();

fn try_from(value: &mut Connector) -> Result<Self, Self::Error> {
Ok(Self {
subgraph_name: value.id.subgraph_name.to_string(),
source_name: value.id.source_name.clone().ok_or(())?,
})
}
}

impl Display for ConnectorSourceRef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}.{}", self.subgraph_name, self.source_name)
}
}

impl tower::Service<ConnectRequest> for ConnectorService {
type Response = ConnectResponse;
type Error = BoxError;
Expand Down
6 changes: 1 addition & 5 deletions apollo-router/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1158,10 +1158,6 @@ fn merge_overrides(
}
if let Some(sources) = config
.as_object_mut()
.and_then(|o| o.get_mut("preview_connectors"))
.and_then(|o| o.as_object_mut())
.and_then(|o| o.get_mut("subgraphs"))
.and_then(|o| o.as_object_mut())
.and_then(|o| o.get_mut("connectors"))
.and_then(|o| o.as_object_mut())
.and_then(|o| o.get_mut("sources"))
Expand All @@ -1170,7 +1166,7 @@ fn merge_overrides(
for (name, url) in overrides2 {
let mut obj = serde_json::Map::new();
obj.insert("override_url".to_string(), url.clone());
sources.insert(name.to_string(), Value::Object(obj));
sources.insert(format!("connectors.{}", name), Value::Object(obj));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
include_subgraph_errors:
all: true

preview_connectors:
connectors:
debug_extensions: true
subgraphs:
connectors:
sources:
jsonPlaceholder:
override_url: http://localhost:4007
sources:
connectors.jsonPlaceholder:
override_url: http://localhost:4007

telemetry:
exporters:
Expand Down
Loading