Skip to content

Conversation

@chdanielmueller
Copy link

Adding an optional function which can modify the headers before they are being sent.
It allows to add additional headers or modify existing headers like Authorization.

It can be used like this:

new window.EmbeddedSandbox({
  target: '#sandbox',
  initialEndpoint: window.location.href,
  initialState: {
    sharedHeaders: {
      'X-SpecialHeader': 'WILL_BE_REPLACED_AUTOMATICALLY'
    }
  },
  modifyHeaders: async (url, headers) => {
    // Add Authorization header if not provided
    if (!headers.Authorization) {
      const accessToken = await window.getAccessToken();
      headers.Authorization = 'Bearer ' + accessToken;
    }
    // Modify special header if shared header is not unchecked
    if (headers['X-SpecialHeader'] === 'WILL_BE_REPLACED_AUTOMATICALLY') {
      const special = await window.getTheValue();
      headers['X-SpecialHeader'] = special;
    }
    return headers;
  }
});

closes #308
replaces #307

@chdanielmueller chdanielmueller requested a review from a team as a code owner December 10, 2025 12:19
@changeset-bot
Copy link

changeset-bot bot commented Dec 10, 2025

🦋 Changeset detected

Latest commit: eb1b869

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@apollo/sandbox Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit eb1b869:

Sandbox Source
maya-test-embed-next Configuration
maya-test-embed-next Configuration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add or modify headers async before query, mutation or subscription is being sent

1 participant