Setting HTTP Request Headers
Set and forward headers in HTTP requests
Apollo Connectors support adding headers to HTTP requests with thehttp.headers
argument.Likewith URLs, you can define header values using a combination of fixed values and dynamicmapping expressions in curly braces ({}
).
Headers with static and dynamic values
This example uses a static value for thex-api-version
header and a dynamic value with the$config
variable for theAuthorization
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 thefrom
argument.This example forwards theAuthorization
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
, therouter configuration takes precedence.Shared headers with@source
You can use a source to share a set of headers with multiple Connectors:
1extend schema2@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)1213type 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 anAuthorization
header, theAuthorization
header from@source
is never set on requests to the/products
endpoint:
1extend schema2@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)1213type 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 anAuthorization
header to propagate.
Additional resources
To test header configurations, useConnector mode in theConnectors Mapping Playground.