Setting HTTP Request Headers
Set and forward headers in HTTP requests
Apollo Connectors support adding headers to HTTP requests with the http.headers
argument.
Like with URLs, you can define header values using a combination of fixed values and dynamic mapping expressions in curly braces ({}
).
Headers with static and dynamic values
This example uses a static value for the x-api-version
header and a dynamic value with the $config
variable for the Authorization
header:
1type Query {
2 products: [Product]
3 @connect(
4 http: {
5 GET: "https://myapi.dev/products"
6 headers: [
7 { name: "x-api-version", value: "2024-01-01" }
8 { name: "Authorization", value: "Bearer {$config.token}" }
9 ]
10 }
11 selection: "id"
12 )
13}
Header propagation
You can propagate headers from an incoming client request using the from
argument.
This example forwards the Authorization
header value from a client request to the connected HTTP endpoint.
1type Query {
2 products: [Product]
3 @connect(
4 http: { GET: "https://myapi.dev/products",
5 headers: [{ name: "Authorization", from: "Authorization" }] }
6 selection: "id"
7 )
8}
@connect
or @source
, the router configuration takes precedence.Shared headers with @source
You can use a source to share a set of headers with multiple Connectors:
1extend schema
2@source(
3 name: "ecomm"
4 http: {
5 baseURL: "https://myapi.dev"
6 headers: [
7 { name: "x-api-version", value: "2024-01-01" }
8 { name: "Authorization", value: "{$config.token}" }
9 ]
10 }
11)
12
13type Query {
14 products: [Product]
15 @connect(
16 source: "ecomm"
17 http: { GET: "/products" }
18 selection: "id"
19 )
20}
Overriding headers
Headers in @connect
override headers in @source
; they are not combined.
In this example, because @connect
includes an Authorization
header, the Authorization
header from @source
is never set on requests to the /products
endpoint:
1extend schema
2@source(
3 name: "ecomm"
4 http: {
5 baseURL: "https://myapi.dev"
6 headers: [
7 { name: "x-api-version", value: "2024-01-01" }
8 { name: "Authorization", value: "{$config.token}" }
9 ]
10 }
11)
12
13type Query {
14 products: [Product]
15 @connect(
16 source: "ecomm"
17 http: {
18 GET: "/products"
19 headers: [
20 { name: "Authorization", from: "Authorization" }
21 ]
22 }
23 selection: "id"
24 )
25}
The @source
header setting is overridden even if the incoming client request doesn't include an Authorization
header to propagate.
Additional resources
To test header configurations, use Connector mode in the Connectors Mapping Playground.