# Use a header as an object key in Apollo Connector Mapping?

**URL:** https://community.apollographql.com/t/use-a-header-as-an-object-key-in-apollo-connector-mapping/8853
**Category:** Connectors
**Tags:** connectors
**Created:** [April 4, 2025, 7:03pm UTC](https://community.apollographql.com/t/use-a-header-as-an-object-key-in-apollo-connector-mapping/8853 "2025-04-04T19:03:39Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![andywgarcia](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/andywgarcia/32/5651_2.png) [@andywgarcia](https://community.apollographql.com/u/andywgarcia)
#### Post date: [April 4, 2025, 7:03pm UTC](https://community.apollographql.com/t/use-a-header-as-an-object-key-in-apollo-connector-mapping/8853/1 "2025-04-04T19:03:39Z")

</div>

I need to use a header value `AcceptLanguage` as a reference for a key value in a mapping response.

Given the following REST response

```auto
{
  "data": {
    "item": {
      "details": {
        "latest": {
          "variation": {
            "id": "args id",
            "props": [
              {
                "key": "variant-name",
                "val": {
                  "en-US": "en us version of the val"
                }
              }
            ]
          }
        }
      }
    }
  }
}

```

And the following variables

```auto
{
  "$args": {
    "id": "arg id"
  },
  "$this": {
    "headers": {
      "AcceptLanguage": "en-US"
    }
  }
}

```

And the following mapping

```auto
$.data.item.details.latest.variation {
  id
  props -> first.val {
    key: $this.headers.AcceptLanguage
  }
}

```

I currently get the following response

```auto
{
  "id": "args id",
  "key": "en-US"
}

```

However, I am wanting it to be

```auto
{
  "id": "args id",
  "key": "en us version of the val"
}

```

How can I use the header as a reference to a specific field in the response object?

Things I have tried:

```auto
$.data.item.details.latest.variation {
  id
  props -> first.val {
    key: $($this.headers.AcceptLanguage)
  }
}

```

results in

```auto
{
  "id": "args id",
  "key": "en-US"
}

```

```auto
$.data.item.details.latest.variation {
  id
  props -> first.val {
    key: "$($this.headers.AcceptLanguage)"
  }
}

```

fails with error `Property ."$($this.headers.AcceptLanguage)" not found in object`

In a related question, I actually need to have the header be `Accept-Language`, but I haven’t been able to figure out how to escape the `-` character.

---

<div class="post-metadata">

### Author: ![Andrew\_McGivery](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/andrew_mcgivery/32/5926_2.png) [@Andrew\_McGivery](https://community.apollographql.com/u/Andrew_McGivery)
#### Post date: [April 4, 2025, 7:49pm UTC](https://community.apollographql.com/t/use-a-header-as-an-object-key-in-apollo-connector-mapping/8853/2 "2025-04-04T19:49:24Z")

</div>

Hey @andywgarcia !

So this is absolutely something we want you to be able to do and is on our roadmap! Specifically, a `get` method is on our proposed list of arrow methods:

```auto
key: $->get($this.headers.AcceptLanguage)

```

Or a more simple example:

```auto
$->echo({"a": "hello"})->get("a") 

```

Additionally, we’re looking at making a new `$request.headers.*` variable available as well which may also fit your use case better if you’d like to take request headers and use them for response mapping.

---

<div class="post-metadata">

### Author: ![Andrew\_McGivery](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/andrew_mcgivery/32/5926_2.png) [@Andrew\_McGivery](https://community.apollographql.com/u/Andrew_McGivery)
#### Post date: [April 4, 2025, 7:55pm UTC](https://community.apollographql.com/t/use-a-header-as-an-object-key-in-apollo-connector-mapping/8853/3 "2025-04-04T19:55:58Z")

</div>

Just replying to add that a possible workaround, if you know all the languages ahead of time, would be to use `match`. Not ideal but might get you through until we have better options!

```auto
$.data.item.details.latest.variation {
  id
  props -> first.val {
    key: $this.headers.AcceptLanguage->match(
      ["en-US", $.'en-US']
     # ... etc
    )
  }
}

```

---

<div class="post-metadata">

### Author: ![andywgarcia](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/andywgarcia/32/5651_2.png) [@andywgarcia](https://community.apollographql.com/u/andywgarcia)
#### Post date: [April 4, 2025, 8:01pm UTC](https://community.apollographql.com/t/use-a-header-as-an-object-key-in-apollo-connector-mapping/8853/4 "2025-04-04T20:01:33Z")

</div>

Thank you!

This workaround will do for now.

We are definitely looking forward to the -\>get function

For reference of the second part of the question of `AcceptLanguage` vs `Accept-Language`, it works by just wrapping it in quotes

`$this.headers."Accept-Language"`

---

<div class="post-metadata">

### Author: ![andywgarcia](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/andywgarcia/32/5651_2.png) [@andywgarcia](https://community.apollographql.com/u/andywgarcia)
#### Post date: [April 4, 2025, 8:31pm UTC](https://community.apollographql.com/t/use-a-header-as-an-object-key-in-apollo-connector-mapping/8853/5 "2025-04-04T20:31:17Z")

</div>

Actually, after putting this in the schema (rather than using it in the playground), there appears to be a discrepancy between the two.

`$this.headers` does not work in the live schema, but it does not work when composing with `rover dev`

---

<div class="post-metadata">

### Author: ![lennyburdette](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/lennyburdette/32/84_2.png) [@lennyburdette](https://community.apollographql.com/u/lennyburdette)
#### Post date: [April 8, 2025, 7:27pm UTC](https://community.apollographql.com/t/use-a-header-as-an-object-key-in-apollo-connector-mapping/8853/6 "2025-04-08T19:27:14Z")

</div>

Hey Andy, what is `$this.headers`? The `$this` variable refers to the type the connector is attached to — does your type have a `headers` field?

---

<div class="post-metadata">

### Author: ![andywgarcia](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/andywgarcia/32/5651_2.png) [@andywgarcia](https://community.apollographql.com/u/andywgarcia)
#### Post date: [April 8, 2025, 8:59pm UTC](https://community.apollographql.com/t/use-a-header-as-an-object-key-in-apollo-connector-mapping/8853/7 "2025-04-08T20:59:20Z")

</div>

This is both mine and ChatGPTs misunderstanding of the mapping. Before I learned that accessing headers from the selection was not a feature that is available yet, it was thought to be the place where I can access header values.

---

<div class="post-metadata">

### Author: ![lennyburdette](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/lennyburdette/32/84_2.png) [@lennyburdette](https://community.apollographql.com/u/lennyburdette)
#### Post date: [April 8, 2025, 9:46pm UTC](https://community.apollographql.com/t/use-a-header-as-an-object-key-in-apollo-connector-mapping/8853/8 "2025-04-08T21:46:01Z")

</div>

That explains it! As Andrew mentioned, we’re planning to support `$request` and `$response` variables in the mapping to provide access to headers. It’s partially implemented and should be available in the coming months!
