Skip to content

Add optional function to modify headers - #347

Open
chdanielmueller wants to merge 1 commit into
apollographql:mainfrom
chdanielmueller:main
Open

Add optional function to modify headers#347
chdanielmueller wants to merge 1 commit into
apollographql:mainfrom
chdanielmueller:main

Conversation

@chdanielmueller

Copy link
Copy Markdown

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

changeset-bot Bot commented Dec 10, 2025

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4e113e7

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

codesandbox-ci Bot commented Dec 10, 2025

Copy link
Copy Markdown

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 4e113e7:

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