Subgraph Request Headers Operations - WunderGraph

Forward HTTP headers to subgraphs

Forwarding specific client headers (request headers) to your subgraphs is a straightforward process. By default, no headers are forwarded for security reasons. To enable header forwarding, insert the following snippet into your config.yaml file and adjust it according to your needs.

# config.yaml

# See https://cosmo-docs.wundergraph.com/router/configuration#config-file
# for the full list of configuration options.

headers:
  all: # Header rules for all subgraph requests.
    request:
      - op: "propagate"            # Forward a client header
        named: X-Test-Header       # Exact match (Use the canonicalized version)

# Regex match (Case insensitive)
      - op: "propagate"
        matching: (?i)^X-Custom-.*

- op: "propagate"
        matching: ^(Header-1|Key)$
        negate_match: true         # Ensure that all headers except the Header-1 and Key are propagated

# Sets the value when the header was not set
      - op: "propagate"
        named: "X-User-Id"
        default: "123"

# Rewrites the header from X-Test-1 to X-Test
      - op: "propagate"
        named: "X-Test-1"
        rename: "X-Test"

- op: "set"
        name: "X-Custom-Header"
        value: "my-required-key"

# Dynamic value using template expression
      - op: "set"
        name: "X-User-ID"
        expression: "request.auth.isAuthenticated ? request.auth.claims.sub : ''"

# Source the value from a file (re-read on the configured interval)
      - op: "set"
        name: "X-Service-Token"
        from_file:
          path: "/etc/secrets/service-token"
          refresh_interval: "1m"

subgraphs:
    products: # Will only affect the Subgraph named "products"
      request:
        - op: "propagate"
          named: "Subgraph-Secret"
          default: "some-secret"

What does the snippet do?

With all we address all subgraph requests. Next, we can define several rules on the client’s request. The operation propagate forwards all matching client request headers to the subgraphs. The operation set sets a new header which is forward to the subgraphs. The subgraphs section allows to propagate headers for specific subgraphs. The name must match with the subgraph name in the Studio.

Supported header rules

Currently, we support the following header rules:

Use this for mounted secrets that rotate on disk (Kubernetes Secrets, Vault Agent sidecars, externally rotated API keys) without restarting the router.

Go canonicalizes headers by default e.g. x-my-header to X-My-Header. Write your rule accordingly or use (?i)^X-Test-.*``` flags to make your regex case insensitive.

Default value

You can also define a value when the client header is not set by the client. Can only be used with the named matcher.

headers:
  all:
    request:
      - op: "propagate"
        named: "X-User-Id"
        default: "123"

gRPC services

When using Cosmo Connect, headers are forwarded to gRPC services as metadata according to the rules defined in the config. Metadata keys match header names but in lowercase. To access X-User-Id you need to use x-user-id as the key.

Rule ordering

All rules are evaluated in the order they are defined.

Please inform us if you have more advanced use cases that cannot be accommodated with the current feature set. You can still use Custom Modules to implement any logic yourself. You can aggregate, remove, or add headers as you like.