Working with Router
Configuring GraphOS Router for Connectors
Overriding baseURL
for environment-specific API hosts
When using a self-hosted router, the baseURL
of a @source
directive can be overridden in the router configuration file .
For example, this configuration overrides the baseURL
for all @connect
directives in the subgraph that reference the v1
source:
1preview_connectors:
2 subgraphs:
3 example: # The name of the subgraph
4 sources:
5 v1: # Refers to @source(name: "v1")
6 override_url: 'https://api.example.com/v1/beta'
@connect
directive doesn't specify the source
attribute, its URL can't be overridden.Configuration and environment variables
You may want to use a configuration value or environment variable as part of a request. You can do this per
subgraph in the router configuration by using the $config
section of each
connectors subgraph:
1preview_connectors:
2 subgraphs:
3 name_of_the_subgraph:
4 $config:
5 name_of_the_variable: value
You can then access the value using the $config
variable in the schema, for example in the URL template or header of a
connector:
1type Query {
2 something: String!
3 @connect(
4 http: {
5 GET: "https://api.example.com/products/{$config.name_of_the_variable}"
6 headers: [
7 { name: "Authorization", value: "Bearer {$config.name_of_variable_containing_token}" }
8 ]
9 }
10 selection: ""
11 )
12}
You can use $config
in the same locations as $this
and $args
.
You can also access nested values using dot notation. For example $config.nested.value
would refer to:
1preview_connectors:
2 subgraphs:
3 name_of_the_subgraph:
4 $config:
5 nested:
6 value: 'some value'
Router configuration also lets you inject environment variables, like this:
1preview_connectors:
2 subgraphs:
3 name_of_the_subgraph:
4 $config:
5 name_of_the_variable: ${env.VARIABLE_NAME}
Request Limits
The maximum number of REST API requests for each GraphQL operation can be configured in the router. This can help
avoid overwhelming upstream services. Once the limit has been reached, a null
value will be returned in the
GraphQL result for any fields with @connect
directives whose requests were not executed. Additionally, a GraphQL
error will be returned in the errors
array of the response. Partial data may still be returned for portions of
the operation that were not affected by the limit.
The limit can be set generally in the router config YAML:
1preview_connectors:
2 max_requests_per_operation_per_source: 100
This will limit the number of requests made to each connector source for a given GraphQL operation. If a connector does not define a source, then this limit is applied at the connector level.
The limit can also be configured for each individual connector source:
1preview_connectors:
2 subgraphs:
3 subgraph_name:
4 sources:
5 source_name:
6 max_requests_per_operation: 50
Limits set on an individual source will override the general max_requests_per_operation_per_source
limit.
The limit can also be configured by setting the environment variable APOLLO_CONNECTORS_MAX_REQUESTS_PER_OPERATION
.
Any configuration in the YAML file will override the environment variable setting.