Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(graphiql-toolkit): accept HeadersInit #3854

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jasonkuhrt
Copy link

This PR enhances graphiql-toolkit package fetcher utility to accept HeadersInit type instead of Record<string, string> in places where headers are accepted.

This is a backwards compatible change because Record<string, string> is a sub-type of HeadersInit. As well, care in this PR has been taken to preserve the way in which same-name headers from different sources would overwrite, not merge, together.

The benefit of this change is that consumers can now pass values of Headers or [string,string][] type which permits the possibility of a header repeated multiple, which HTTP supports.

In practice a place this issue became relevant was in my work on Hive Console's Laboratory Preflight Script feature:

graphql-hive/console#6378 (comment)

Copy link

changeset-bot bot commented Jan 17, 2025

⚠️ No Changeset found

Latest commit: dc3dca7

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@jasonkuhrt
Copy link
Author

I will add a changeset once I receive feedback that the changes will be accepted.

const fetcherOptsHeadersEntries: [string, string][] = [
...fetcherOptsHeaders.entries(),
];
// todo: If there are headers with multiple values, they will be lost. Is this a problem?
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not familiar with graphql-ws and have not investigated what it does with these key-values yet.

Can someone advise what the correct logic would be here? I am currently apply a lossy approach, but I do not know if that is ok here.

Comment on lines +213 to +220
const fetcherOptsHeaders = new Headers(fetcherOpts?.headers ?? {});
// @ts-expect-error: Current TS Config target does not support `Headers.entries()` method.
// However it is reported as "widely available" and so should be fine to use. This could
// would be more complicated without it.
// @see https://developer.mozilla.org/en-US/docs/Web/API/Headers/entries
const fetcherOptsHeadersEntries: [string, string][] = [
...fetcherOptsHeaders.entries(),
];
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this isn't needed. However I don't want to open a pandora's box by changing the tsconfig.json file.

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.

1 participant